Skip to content

Commit

Permalink
feat(modifiers): add support for namespace modifier to allow rest par…
Browse files Browse the repository at this point in the history
…ameters (#115)

feat #105
  • Loading branch information
andersonjoseph authored Apr 1, 2022
1 parent 6c7efb1 commit 509d3dd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions demo_funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
12 changes: 8 additions & 4 deletions src/log/BaseLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

Expand All @@ -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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test/browser/modifiers/identifying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
10 changes: 10 additions & 0 deletions test/node/modifiers/identifying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

0 comments on commit 509d3dd

Please sign in to comment.