Skip to content

Commit

Permalink
feat: avoid unnecessary argument iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Dec 24, 2020
1 parent 94b427d commit 9aa73f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"globalthis": "^1.0.1",
"json-stringify-safe": "^5.0.1",
"semver-compare": "^1.0.0",
"sprintf-js": "^1.1.2"
"sprintfit": "^1.0.1"
},
"description": "JSON logger for Node.js and browser.",
"devDependencies": {
Expand Down
24 changes: 11 additions & 13 deletions src/factories/createLogger.js
Expand Up @@ -5,7 +5,7 @@ import createGlobalThis from 'globalthis';
import stringify from 'json-stringify-safe';
import {
sprintf,
} from 'sprintf-js';
} from 'sprintfit';
import {
logLevels,
} from '../constants';
Expand Down Expand Up @@ -96,22 +96,20 @@ const createLogger = (onMessage: MessageEventHandlerType, parentContext?: Messag
let context;
let message;

if (typeof a === 'string') {
if (typeof a === 'string' && b === undefined) {
context = {
...getFirstParentDomainContext(),
...parentContext || {},
};
// eslint-disable-next-line id-length, object-property-newline
const {...args} = {a, b, c, d, e, f, g, h, i, k};
const values = Object.keys(args).map((key) => {
return args[key];
});
// eslint-disable-next-line unicorn/no-reduce
const hasOnlyOneParameterValued = 1 === values.reduce((accumulator, value) => {
// eslint-disable-next-line no-return-assign, no-param-reassign
return accumulator += typeof value === 'undefined' ? 0 : 1;
}, 0);
message = hasOnlyOneParameterValued ? sprintf('%s', a) : sprintf(a, b, c, d, e, f, g, h, i, k);

message = a;
} else if (typeof a === 'string') {
context = {
...getFirstParentDomainContext(),
...parentContext || {},
};

message = sprintf(a, b, c, d, e, f, g, h, i, k);
} else {
if (typeof b !== 'string') {
throw new TypeError('Message must be a string.');
Expand Down

0 comments on commit 9aa73f3

Please sign in to comment.