Skip to content

Commit

Permalink
refactor: replace Joi with Zod and make use of type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruijn committed Mar 25, 2022
1 parent 2383e9f commit 52ebba6
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 335 deletions.
108 changes: 22 additions & 86 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
"dependencies": {
"@sindresorhus/is": "4.6.0",
"chalk": "4.1.2",
"joi": "17.4.2",
"json-stringify-pretty-compact": "3.0.0",
"moment": "2.29.1"
"moment": "2.29.1",
"zod": "3.11.6"
}
}
10 changes: 2 additions & 8 deletions src/bunyan-pretty-stream.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { MergedOptions, Options, schema } from './options';
import { Options, schema } from './options';
import { Transform, TransformCallback } from 'stream';
import { fromString, isBunyanRecord } from './bunyan-record';
import { Formatter } from './formatter';
import is from '@sindresorhus/is';
import { joi } from './helpers';

class PrettyStream extends Transform {
private _formatter: Formatter;

constructor(options: Options = {}) {
super({ objectMode: true });

const validation = schema.validate(options);
if (!joi.isValid<MergedOptions>(validation, validation.value)) {
throw validation.error;
}

this._formatter = new Formatter(validation.value);
this._formatter = new Formatter(schema.parse(options));
}

_transform(
Expand Down
23 changes: 12 additions & 11 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ class Formatter {
return parsed;
}

const extras = parsed.details[this._options.extrasKey];
if (this._options.extrasKey !== '' && is.nonEmptyObject(extras)) {
Object.entries(parsed.details[this._options.extrasKey]).forEach(
([key, value]) => {
if (this.isExtra(value)) {
parsed.extras[key] = value;
delete parsed.details[this._options.extrasKey][key];
}
},
);
} else if (this._options.extrasKey === '') {
const extras = is.undefined(this._options.extrasKey)
? {}
: parsed.details[this._options.extrasKey];
if (this._options.extrasKey !== undefined && is.nonEmptyObject(extras)) {
const extrasKey = this._options.extrasKey;
Object.entries(parsed.details[extrasKey]).forEach(([key, value]) => {
if (this.isExtra(value)) {
parsed.extras[key] = value;
delete parsed.details[extrasKey][key];
}
});
} else if (this._options.extrasKey === undefined) {
Object.entries(parsed.details).forEach(([key, value]) => {
if (this.isExtra(value)) {
parsed.extras[key] = value;
Expand Down
3 changes: 0 additions & 3 deletions src/helpers/index.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/helpers/joi.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/helpers/joi.unit.test.ts

This file was deleted.

Loading

0 comments on commit 52ebba6

Please sign in to comment.