Просмотр исходного кода

chore: adds namespace prefix for eache line

e22m4u 7 месяцев назад
Родитель
Сommit
2ea6f1e5ef
3 измененных файлов с 23 добавлено и 6 удалено
  1. 5 2
      dist/cjs/index.cjs
  2. 5 2
      src/create-debugger.js
  3. 13 2
      src/create-debugger.spec.js

+ 5 - 2
dist/cjs/index.cjs

@@ -255,8 +255,11 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
     if (!isDebuggerEnabled()) return;
     const prefix = getPrefix();
     if (typeof messageOrData === "string") {
-      const message = (0, import_js_format2.format)(messageOrData, ...args);
-      prefix ? console.log(`${prefix} ${message}`) : console.log(message);
+      const multiString2 = (0, import_js_format2.format)(messageOrData, ...args);
+      const rows2 = multiString2.split("\n");
+      rows2.forEach((message) => {
+        prefix ? console.log(`${prefix} ${message}`) : console.log(message);
+      });
       return;
     }
     const multiString = createColorizedDump(messageOrData);

+ 5 - 2
src/create-debugger.js

@@ -214,8 +214,11 @@ export function createDebugger(
     if (!isDebuggerEnabled()) return;
     const prefix = getPrefix();
     if (typeof messageOrData === 'string') {
-      const message = format(messageOrData, ...args);
-      prefix ? console.log(`${prefix} ${message}`) : console.log(message);
+      const multiString = format(messageOrData, ...args);
+      const rows = multiString.split('\n');
+      rows.forEach(message => {
+        prefix ? console.log(`${prefix} ${message}`) : console.log(message);
+      });
       return;
     }
     const multiString = createColorizedDump(messageOrData);

+ 13 - 2
src/create-debugger.spec.js

@@ -156,7 +156,6 @@ describe('createDebugger', function () {
   });
 
   describe('namespaces', function () {
-    // --- 1. Базовое создание (createDebugger) ---
     it('should use namespace provided in createDebugger', function () {
       process.env.DEBUG = 'app';
       const debug = createDebugger('app');
@@ -296,7 +295,19 @@ describe('createDebugger', function () {
       );
     });
 
-    // --- 5. Обработка ошибок ---
+    it('should add namespace prefix for each line if the given message is multiline', function () {
+      process.env.DEBUG = 'app:service';
+      const debug = createDebugger('app', 'service');
+      debug('firstLine\nsecondLine');
+      expect(consoleLogSpy.calledTwice).to.be.true;
+      expect(stripAnsi(consoleLogSpy.firstCall.args[0])).to.be.eq(
+        'app:service firstLine',
+      );
+      expect(stripAnsi(consoleLogSpy.secondCall.args[0])).to.be.eq(
+        'app:service secondLine',
+      );
+    });
+
     it('should throw error if createDebugger is called with invalid subsequent segment type', function () {
       expect(() => createDebugger('app', 'valid', 123)).to.throw(
         /Namespace segment must be a non-empty String/,