Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove $lib/styletext.ts and rework the logger #36

Merged
merged 21 commits into from
Sep 15, 2024
5 changes: 5 additions & 0 deletions .changeset/hip-lions-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/belt': minor
---

rework the `Logger` API
5 changes: 5 additions & 0 deletions .changeset/small-sloths-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/belt': minor
---

remove `$lib/styletext.ts`
5 changes: 5 additions & 0 deletions .changeset/three-eels-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ryanatkn/belt': minor
---

move `print_log_label` to `$lib/print.ts` from `$lib/log.ts`
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: ['20.12']
node-version: ['20.17']

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"type": "module",
"engines": {
"node": ">=20.12"
"node": ">=20.17"
},
"keywords": [
"js",
Expand Down Expand Up @@ -186,10 +186,6 @@
"types": "./dist/string.d.ts",
"default": "./dist/string.js"
},
"./styletext.js": {
"types": "./dist/styletext.d.ts",
"default": "./dist/styletext.js"
},
"./timings.js": {
"types": "./dist/timings.d.ts",
"default": "./dist/timings.js"
Expand Down
70 changes: 34 additions & 36 deletions src/lib/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,41 @@ import * as assert from 'uvu/assert';

import {Logger, type Logger_State} from '$lib/log.js';

/* test__Logger */
interface Test_Logger_Context {
logged_args: any;
logger_state: Logger_State;
}
const create_test_logger_context = (): Test_Logger_Context => {
const collect_args = (...log_args: any[]) => {
ctx.logged_args = log_args;
};
const ctx: Test_Logger_Context = {
logged_args: undefined, // stores the result of the latest log call
logger_state: {
level: 'debug',
log: (...log_args: any[]) => {
ctx.logged_args = log_args;
},
prefixes: ['pre'],
suffixes: ['post'],
error: {
prefixes: ['errorP1', 'errorP2'],
suffixes: ['errorS1', 'errorS2'],
},
warn: {
prefixes: ['warnP1', 'warnP2'],
suffixes: ['warnS1', 'warnS2'],
},
info: {
prefixes: ['infoP1', 'infoP2'],
suffixes: ['infoS1', 'infoS2'],
},
debug: {
prefixes: ['debugP1', 'debugP2'],
suffixes: ['debugS1', 'debugS2'],
st: (_, s) => s,
console: {
error: collect_args,
warn: collect_args,
log: collect_args,
},
prefixes: () => ['pre'],
suffixes: () => ['post'],
error_prefixes: () => ['errorP1', 'errorP2'],
error_suffixes: () => ['errorS1', 'errorS2'],
warn_prefixes: () => ['warnP1', 'warnP2'],
warn_suffixes: () => ['warnS1', 'warnS2'],
info_prefixes: () => ['infoP1', 'infoP2'],
info_suffixes: () => ['infoS1', 'infoS2'],
debug_prefixes: () => ['debugP1', 'debugP2'],
debug_suffixes: () => ['debugS1', 'debugS2'],
},
};
return ctx;
};
const test__Logger = suite('Logger', create_test_logger_context());
const test = suite('Logger', create_test_logger_context());

test__Logger('prefixes and suffixes', (ctx) => {
test('prefixes and suffixes', (ctx) => {
const log = new Logger(['p1', 'p2'], ['s1', 's2'], ctx.logger_state);

log.error('foo', 36);
Expand Down Expand Up @@ -112,32 +109,34 @@ test__Logger('prefixes and suffixes', (ctx) => {
ctx.logged_args = undefined;
});

test__Logger('mutate logger state to change prefix and suffix', (ctx) => {
test('mutate logger state to change prefix and suffix', (ctx) => {
const info_prefixes = ['p1', 'p2'];
const info_suffixes = ['s1', 's2'];
const log = new Logger(undefined, undefined, {
...ctx.logger_state,
info: {
prefixes: ['p1', 'p2'],
suffixes: ['s1', 's2'],
},
info_prefixes: () => info_prefixes,
info_suffixes: () => info_suffixes,
});
log.info('foo', 36);
assert.equal(ctx.logged_args, ['pre', 'p1', 'p2', 'foo', 36, 's1', 's2', 'post']);
ctx.logged_args = undefined;

// mutate the prefixes and suffixes
log.state.info.prefixes.pop();
log.state.info.suffixes.shift();
info_prefixes.pop();
info_suffixes.shift();

log.info('foo', 36);
assert.equal(ctx.logged_args, ['pre', 'p1', 'foo', 36, 's2', 'post']);
ctx.logged_args = undefined;
});

test__Logger('mutate logger state to change log level', (ctx) => {
const state = {
test('mutate logger state to change log level', (ctx) => {
const state: Logger_State = {
...ctx.logger_state,
info: {prefixes: [], suffixes: []},
warn: {prefixes: [], suffixes: []},
info_prefixes: () => [],
info_suffixes: () => [],
warn_prefixes: () => [],
warn_suffixes: () => [],
};
const log = new Logger(undefined, undefined, state);

Expand All @@ -157,5 +156,4 @@ test__Logger('mutate logger state to change log level', (ctx) => {
ctx.logged_args = undefined;
});

test__Logger.run();
/* test__Logger */
test.run();
Loading
Loading