From 509d3ddf0eb4b697d2fd1b7e4c0e097271b5f792 Mon Sep 17 00:00:00 2001 From: "Anderson. J" Date: Fri, 1 Apr 2022 09:58:36 -0400 Subject: [PATCH] feat(modifiers): add support for namespace modifier to allow rest parameters (#115) feat #105 --- demo_funcs.js | 2 ++ src/log/BaseLog.ts | 12 ++++++++---- test/browser/modifiers/identifying.ts | 10 ++++++++++ test/node/modifiers/identifying.ts | 10 ++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/demo_funcs.js b/demo_funcs.js index 3e3b1f96..3a983140 100644 --- a/demo_funcs.js +++ b/demo_funcs.js @@ -319,6 +319,8 @@ function withNamespace({ adze }) { function withMultiNamespace({ adze }) { console.log('\n----- Default Multiple Namespace Log w/ No Store -----\n'); adze().ns(['foo', 'bar']).info('This log has multiple namespaces.'); + adze().namespace('foo', 'bar', 'baz').info('Testing multiple namespaces using rest parameters.'); + adze().ns('foo', 'bar', 'baz').info('Testing multiple namespaces using rest parameters with the ns() alias.'); } function withTime({ adze }) { diff --git a/src/log/BaseLog.ts b/src/log/BaseLog.ts index a42dfbe3..d2284402 100644 --- a/src/log/BaseLog.ts +++ b/src/log/BaseLog.ts @@ -586,9 +586,11 @@ export class BaseLog { * * This is a non-standard API. */ - public namespace(ns: string | string[]): this { + public namespace(ns: string[]): this; + public namespace(...rest: string[]): this; + public namespace(ns: string | string[], ...rest: string[]): this { return this.modifier((ctxt) => { - ctxt._namespaceVal = isString(ns) ? [ns] : ns; + ctxt._namespaceVal = isString(ns) ? [ns, ...rest] : ns; }); } @@ -597,8 +599,10 @@ export class BaseLog { * * This is a non-standard API. */ - public ns(ns: string | string[]): this { - return this.namespace(ns); + public ns(ns: string[]): this; + public ns(...rest: string[]): this; + public ns(ns: string | string[], ...rest: string[]): this { + return this.namespace(ns as string, ...rest); } /** diff --git a/test/browser/modifiers/identifying.ts b/test/browser/modifiers/identifying.ts index d2977a96..c9778f15 100644 --- a/test/browser/modifiers/identifying.ts +++ b/test/browser/modifiers/identifying.ts @@ -37,3 +37,13 @@ test('log with multiple namespaces prints correctly', (t) => { t.fail(); } }); + +test('log with multiple namespaces using rest parameters prints correctly', (t) => { + const { render } = adze().ns('test', 'test2').log('This log has a label.'); + if (render) { + const [_, args] = render; + t.is(args[2], '#test #test2 '); + } else { + t.fail(); + } +}); diff --git a/test/node/modifiers/identifying.ts b/test/node/modifiers/identifying.ts index cf4d88e7..a0324240 100644 --- a/test/node/modifiers/identifying.ts +++ b/test/node/modifiers/identifying.ts @@ -32,3 +32,13 @@ test('log with multiple namespaces prints correctly', (t) => { t.fail(); } }); + +test('log with multiple namespaces using rest parameters prints correctly', (t) => { + const { render } = adze().ns('test', 'test2').log('This log has a label.'); + if (render) { + const [_, args] = render; + t.is(args[1], '#test #test2 '); + } else { + t.fail(); + } +});