Browse Source

chore: renames method withoutGlobalNs() to withoutEnvNs()

e22m4u 6 months ago
parent
commit
bba703260a
5 changed files with 23 additions and 22 deletions
  1. 1 1
      README.md
  2. 6 6
      dist/cjs/index.cjs
  3. 3 3
      src/create-debugger.d.ts
  4. 11 10
      src/create-debugger.js
  5. 2 2
      src/create-debugger.spec.js

+ 1 - 1
README.md

@@ -125,7 +125,7 @@ import {createDebugger} from '@e22m4u/js-debug';
 process.env['DEBUGGER_NAMESPACE'] = 'myApp';
 
 const debug1 = createDebugger();
-const debug2 = debug1.withoutGlobalNs();
+const debug2 = debug1.withoutEnvNs();
 debug1('Hello world');
 debug2('Hello world');
 // myApp Hello world

+ 6 - 6
dist/cjs/index.cjs

@@ -202,7 +202,7 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
   }
   const withCustomState = isNonArrayObject(namespaceOrOptions);
   const state = withCustomState ? namespaceOrOptions : {};
-  state.globalNsSegments = Array.isArray(state.globalNsSegments) ? state.globalNsSegments : [];
+  state.envNsSegments = Array.isArray(state.envNsSegments) ? state.envNsSegments : [];
   state.nsSegments = Array.isArray(state.nsSegments) ? state.nsSegments : [];
   state.pattern = typeof state.pattern === "string" ? state.pattern : "";
   state.hash = typeof state.hash === "string" ? state.hash : "";
@@ -211,7 +211,7 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
   state.delimiter = state.delimiter && typeof state.delimiter === "string" ? state.delimiter : ":";
   if (!withCustomState) {
     if (typeof process !== "undefined" && process.env && process.env["DEBUGGER_NAMESPACE"]) {
-      state.globalNsSegments.push(process.env.DEBUGGER_NAMESPACE);
+      state.envNsSegments.push(process.env.DEBUGGER_NAMESPACE);
     }
     if (typeof namespaceOrOptions === "string")
       state.nsSegments.push(namespaceOrOptions);
@@ -230,7 +230,7 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
     state.pattern = localStorage.getItem("debug");
   }
   const isDebuggerEnabled = /* @__PURE__ */ __name(() => {
-    const nsStr = [...state.globalNsSegments, ...state.nsSegments].join(
+    const nsStr = [...state.envNsSegments, ...state.nsSegments].join(
       state.delimiter
     );
     const patterns = state.pattern.split(/[\s,]+/).filter((p) => p.length > 0);
@@ -242,7 +242,7 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
   }, "isDebuggerEnabled");
   const getPrefix = /* @__PURE__ */ __name(() => {
     let tokens = [];
-    [...state.globalNsSegments, ...state.nsSegments, state.hash].filter(Boolean).forEach((token) => {
+    [...state.envNsSegments, ...state.nsSegments, state.hash].filter(Boolean).forEach((token) => {
       const extractedTokens = token.split(state.delimiter).filter(Boolean);
       tokens = [...tokens, ...extractedTokens];
     });
@@ -318,9 +318,9 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
     stateCopy.offsetSize = offsetSize;
     return createDebugger(stateCopy);
   };
-  debugFn.withoutGlobalNs = function() {
+  debugFn.withoutEnvNs = function() {
     const stateCopy = JSON.parse(JSON.stringify(state));
-    stateCopy.globalNsSegments = [];
+    stateCopy.envNsSegments = [];
     return createDebugger(stateCopy);
   };
   return debugFn;

+ 3 - 3
src/create-debugger.d.ts

@@ -50,12 +50,12 @@ export interface Debugger {
   withOffset(offsetSize: number): Debugger;
 
   /**
-   * Создает новый экземпляр отладчика без глобальных пространств имен
-   * (исключает пространство имен из переменной окружения DEBUGGER_NAMESPACE).
+   * Создает новый экземпляр отладчика без пространства имен
+   * из переменной окружения DEBUGGER_NAMESPACE.
    *
    * @returns Новый экземпляр Debugger.
    */
-  withoutGlobalNs(): Debugger;
+  withoutEnvNs(): Debugger;
 }
 
 /**

+ 11 - 10
src/create-debugger.js

@@ -132,8 +132,8 @@ export function createDebugger(
   // для хранения текущих настроек
   const withCustomState = isNonArrayObject(namespaceOrOptions);
   const state = withCustomState ? namespaceOrOptions : {};
-  state.globalNsSegments = Array.isArray(state.globalNsSegments)
-    ? state.globalNsSegments
+  state.envNsSegments = Array.isArray(state.envNsSegments)
+    ? state.envNsSegments
     : [];
   state.nsSegments = Array.isArray(state.nsSegments) ? state.nsSegments : [];
   state.pattern = typeof state.pattern === 'string' ? state.pattern : '';
@@ -152,15 +152,15 @@ export function createDebugger(
   // то дополнительно проверяется наличие сегмента пространства
   // имен в переменной окружения, и сегмент из первого аргумента
   if (!withCustomState) {
-    // если переменная окружения содержит пространство
-    // имен, то значение переменной добавляется
+    // если переменная окружения DEBUGGER_NAMESPACE содержит
+    // пространство имен, то значение переменной добавляется
     // в общий список
     if (
       typeof process !== 'undefined' &&
       process.env &&
       process.env['DEBUGGER_NAMESPACE']
     ) {
-      state.globalNsSegments.push(process.env.DEBUGGER_NAMESPACE);
+      state.envNsSegments.push(process.env.DEBUGGER_NAMESPACE);
     }
     // если первый аргумент содержит значение,
     // то оно используется как пространство имен
@@ -194,7 +194,7 @@ export function createDebugger(
   // формирование функции для проверки
   // активности текущего отладчика
   const isDebuggerEnabled = () => {
-    const nsStr = [...state.globalNsSegments, ...state.nsSegments].join(
+    const nsStr = [...state.envNsSegments, ...state.nsSegments].join(
       state.delimiter,
     );
     const patterns = state.pattern.split(/[\s,]+/).filter(p => p.length > 0);
@@ -208,7 +208,7 @@ export function createDebugger(
   // для сообщений отладки
   const getPrefix = () => {
     let tokens = [];
-    [...state.globalNsSegments, ...state.nsSegments, state.hash]
+    [...state.envNsSegments, ...state.nsSegments, state.hash]
       .filter(Boolean)
       .forEach(token => {
         const extractedTokens = token.split(state.delimiter).filter(Boolean);
@@ -298,10 +298,11 @@ export function createDebugger(
     return createDebugger(stateCopy);
   };
   // создание новой функции логирования
-  // без глобальных пространств имен
-  debugFn.withoutGlobalNs = function () {
+  // без пространства имен из переменной
+  // окружения DEBUGGER_NAMESPACE
+  debugFn.withoutEnvNs = function () {
     const stateCopy = JSON.parse(JSON.stringify(state));
-    stateCopy.globalNsSegments = [];
+    stateCopy.envNsSegments = [];
     return createDebugger(stateCopy);
   };
   return debugFn;

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

@@ -480,11 +480,11 @@ describe('createDebugger', function () {
       );
     });
 
-    it('should exclude namespace of DEBUGGER_NAMESPACE in withoutGlobalNs()', function () {
+    it('should exclude namespace of DEBUGGER_NAMESPACE in withoutEnvNs()', function () {
       process.env.DEBUG = '*';
       process.env.DEBUGGER_NAMESPACE = 'myApp';
       const debug1 = createDebugger();
-      const debug2 = debug1.withoutGlobalNs();
+      const debug2 = debug1.withoutEnvNs();
       debug1('message 1');
       expect(consoleLogSpy.callCount).to.equal(1);
       expect(stripAnsi(consoleLogSpy.getCall(0).args[0])).to.equal(