Skip to content

Commit

Permalink
Merge pull request #52 from vidavidorra/feat/zod
Browse files Browse the repository at this point in the history
Feat/zod
  • Loading branch information
jdbruijn authored Mar 25, 2022
2 parents 2383e9f + 87fe6f8 commit 4556954
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 424 deletions.
112 changes: 24 additions & 88 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 { Transform, TransformCallback } from 'stream';
import { fromString, isBunyanRecord } from './bunyan-record';
import { Formatter } from './formatter';
import { Options } from './options';
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(options);
}

_transform(
Expand Down
119 changes: 119 additions & 0 deletions src/formatter/extras.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { ParsedOptions } from '../options';

class Extras {
readonly options = {
keyValueSeparator: '=',
separator: ', ',
start: '(',
end: ')',
} as const;

private readonly _maxLength: ParsedOptions['extras']['maxLength'];
private _extras: string[];
private _length: number;

constructor(maxLength: ParsedOptions['extras']['maxLength']) {
this._maxLength = maxLength;
this._length = 0;
this._extras = [];
}

get length() {
return this._length;
}

get extras() {
return this._extras;
}

/**
* Parse a key-value pair and add to the extras if it is a valid extra.
*
* @param key
* @param value
* @returns `true` if the key-value was valid and is added, false otherwise.
*/
parseAndAdd(key: string, value: unknown): boolean {
if (typeof value === 'object' || typeof value === 'function') {
return false;
}

const extra = this.formatExtra(key, value);
if (
extra.key.length > this._maxLength.key ||
extra.value.length > this._maxLength.value ||
this.lengthAfterAdding(extra.formatted) > this._maxLength.total
) {
return false;
}

this.add(extra.formatted);
return true;
}

format(): string {
if (this._extras.length === 0) {
return '';
}

return [
this.options.start,
this._extras.join(this.options.separator),
this.options.end,
].join('');
}

formatExtra(
key: string,
value: unknown,
): { formatted: string; key: string; value: string } {
const stringifiedKey = this.stringify(key);
const stringifiedValue = this.stringify(value);
const formatted = [
stringifiedKey,
this.options.keyValueSeparator,
stringifiedValue,
].join('');

return {
formatted,
key: stringifiedKey,
value: stringifiedValue,
};
}

private lengthAfterAdding(formattedExtra: string): number {
let length = this._length + formattedExtra.length;
if (this._length === 0) {
length += this.options.start.length + this.options.end.length;
} else if (this._extras.length >= 1) {
length += this.options.separator.length;
}

return length;
}

private add(formattedExtra: string): void {
this._extras.push(formattedExtra);
this._length = this.lengthAfterAdding(formattedExtra);
}

private stringify(value: unknown): string {
if (
typeof value === 'string' &&
value.length > 0 &&
!this.containsWhitespace(value) &&
!value.includes(this.options.keyValueSeparator)
) {
return value;
}

return JSON.stringify(value);
}

private containsWhitespace(value: string): boolean {
return /\s/.test(value);
}
}

export { Extras };
Loading

0 comments on commit 4556954

Please sign in to comment.