Skip to content

Commit

Permalink
feat: replace printf implementation (fixes #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jan 3, 2021
1 parent a09b427 commit 5e264c9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"boolean": "^3.0.2",
"detect-node": "^2.0.4",
"fast-json-stringify": "^2.3.0",
"fast-printf": "^1.3.0",
"globalthis": "^1.0.1",
"is-circular": "^1.0.2",
"json-stringify-safe": "^5.0.1",
"semver-compare": "^1.0.0",
"sprintfit": "^1.0.1"
"semver-compare": "^1.0.0"
},
"description": "JSON logger for Node.js and browser.",
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/factories/createLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
approximateTime,
} from 'approximate-now';
import environmentIsNode from 'detect-node';
import {
printf,
} from 'fast-printf';
import createGlobalThis from 'globalthis';
import isCircular from 'is-circular';
import stringify from 'json-stringify-safe';
import {
sprintf,
} from 'sprintfit';
import {
logLevels,
} from '../constants';
Expand Down Expand Up @@ -134,7 +134,7 @@ const createLogger = (
if (typeof a === 'string' && b === undefined) {
message = a;
} else if (typeof a === 'string') {
message = sprintf(
message = printf(
a,
b,
c,
Expand All @@ -151,7 +151,7 @@ const createLogger = (
throw new TypeError('Message must be a string.');
}

message = sprintf(
message = printf(
b,
c,
d,
Expand Down
32 changes: 32 additions & 0 deletions test/roarr/roarr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ test('formats message using sprintf', (t) => {
]);
});

test('formats message using sprintf (multiple variables)', (t) => {
const log = createLoggerWithHistory();

log('foo %s %s %s', 'bar', 'baz', 'qux');

t.deepEqual(log.messages, [
{
context: {},
message: 'foo bar baz qux',
sequence,
time,
version,
},
]);
});

test('formats message using sprintf (digit variables)', (t) => {
const log = createLoggerWithHistory();

log('foo %d %d %d', '1', '2', '3');

t.deepEqual(log.messages, [
{
context: {},
message: 'foo 1 2 3',
sequence,
time,
version,
},
]);
});

test('creates message with a context', (t) => {
const log = createLoggerWithHistory();

Expand Down

0 comments on commit 5e264c9

Please sign in to comment.