Skip to content

Commit

Permalink
refactor(options): replace internal options interface with private fo…
Browse files Browse the repository at this point in the history
…rmatter class variable
  • Loading branch information
jdbruijn committed Mar 25, 2022
1 parent c638413 commit 2d5a629
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 58 deletions.
19 changes: 13 additions & 6 deletions src/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BunyanRecord, coreFields } from './bunyan-record';
import { MergedOptions } from './options';
import { ParsedOptions } from './options';
import bunyan from 'bunyan';
import chalk from 'chalk';
import is from '@sindresorhus/is';
Expand Down Expand Up @@ -34,14 +34,21 @@ interface ParsedRecord
}

class Formatter {
private readonly _options: Readonly<MergedOptions>;
private readonly _options: Readonly<ParsedOptions>;
private readonly _regex = {
newLine: /\r\n|\r|\n/,
whitespace: /\s/,
} as const;
private readonly _internalOptions = {
maxExtrasValueLength: 50,
timeFormat: {
short: 'HH:mm:ss.SSS',
long: 'YYYY-MM-DD[T]HH:mm:ss.SSS',
},
} as const;
private readonly _levels: Readonly<Record<number, string>>;

constructor(options: MergedOptions) {
constructor(options: ParsedOptions) {
options.basePath = path.normalize(options.basePath);
this._options = options;

Expand Down Expand Up @@ -127,9 +134,9 @@ class Formatter {

let format = this._options.time.format;
if (this._options.time.type === 'short') {
format = this._options.time.formats.short;
format = this._internalOptions.timeFormat.short;
} else if (this._options.time.type === 'long') {
format = this._options.time.formats.long;
format = this._internalOptions.timeFormat.long;
}

if (!this._options.time.local) {
Expand Down Expand Up @@ -258,7 +265,7 @@ class Formatter {

return (
this.isSingleLine(stringifiedValue) &&
stringifiedValue.length <= this._options.extrasMaxValueLength
stringifiedValue.length <= this._internalOptions.maxExtrasValueLength
);
}

Expand Down
10 changes: 3 additions & 7 deletions src/formatter.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, it } from '@jest/globals';
import { BunyanRecord } from './bunyan-record';
import { Formatter } from './formatter';
import { MergedOptions } from './options';
import { ParsedOptions } from './options';
import stripAnsi from 'strip-ansi';

describe('Formatter', () => {
const options: Readonly<MergedOptions> = {
const options: Readonly<ParsedOptions> = {
show: {
time: true,
name: false,
Expand All @@ -14,21 +14,17 @@ describe('Formatter', () => {
source: false,
extras: false,
},
extras: {},
indent: {
details: 4,
json: 2,
},
basePath: '/',
newLineCharacter: '\n',
extrasMaxValueLength: 50,
time: {
local: false,
type: 'long',
format: 'YYYY-MM-DD[T]HH:mm:ss.SSS',
formats: {
short: 'HH:mm:ss.SSS',
long: 'YYYY-MM-DD[T]HH:mm:ss.SSS',
},
},
};

Expand Down
32 changes: 4 additions & 28 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { coreFields as bunyanCoreFields } from './bunyan-record';
import { z } from 'zod';

const options = z
const schema = z
.object({
show: z
.object({
Expand Down Expand Up @@ -56,31 +56,7 @@ const options = z
})
.strict();

const mergedOptions = options.merge(
z
.object({
extrasMaxValueLength: z.number().int().positive().default(50),
time: z
.intersection(
options.shape.time,
z.object({
formats: z
.object({
short: z.string().default('HH:mm:ss.SSS'),
long: z.string().default('YYYY-MM-DD[T]HH:mm:ss.SSS'),
})
.strict()
.default({}),
}),
)
.default({}),
})
.strict(),
);
type Options = z.input<typeof schema>;
type ParsedOptions = z.infer<typeof schema>;

type Options = z.input<typeof options>;
type MergedOptions = z.infer<typeof mergedOptions>;

const schema = mergedOptions;

export { Options, MergedOptions, schema };
export { Options, ParsedOptions, schema };
20 changes: 3 additions & 17 deletions src/options.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Options, schema } from './options';
import { ParsedOptions, schema } from './options';
import { describe, expect, it } from '@jest/globals';
import clone from 'clone';
import { coreFields } from './bunyan-record';
Expand All @@ -15,9 +15,7 @@ function stringify(value: unknown): string {
}

describe('schema', () => {
const defaults: Readonly<{
options: Options;
}> = {
const defaults: Readonly<{ options: ParsedOptions }> = {
options: {
show: {
time: true,
Expand All @@ -27,6 +25,7 @@ describe('schema', () => {
source: false,
extras: false,
},
extras: {},
indent: {
details: 4,
json: 2,
Expand All @@ -39,15 +38,6 @@ describe('schema', () => {
format: 'YYYY-MM-DD[T]HH:mm:ss.SSS',
},
},
// internalOptions: {
// extrasMaxValueLength: 50,
// time: {
// formats: {
// short: 'HH:mm:ss.SSS',
// long: 'YYYY-MM-DD[T]HH:mm:ss.SSS',
// },
// },
// },
};

describe.each([
Expand All @@ -62,12 +52,9 @@ describe('schema', () => {
['indent.json', 'number', 2],
['basePath', 'string', '/'],
['newLineCharacter', '\r | \n | \r\n', '\n'],
['extrasMaxValueLength', 'number', 50],
['time.local', 'boolean', false],
['time.type', 'one of [short, long, format]', 'long'],
['time.format', 'string', 'YYYY-MM-DD[T]HH:mm:ss.SSS'],
['time.formats.short', 'string', 'HH:mm:ss.SSS'],
['time.formats.long', 'string', 'YYYY-MM-DD[T]HH:mm:ss.SSS'],
])('%s', (path: string, type: string, defaultValue: unknown) => {
it(`MUST be ${stringify(type)}`, () => {
const options = clone(defaults.options);
Expand Down Expand Up @@ -116,7 +103,6 @@ describe('schema', () => {
describe.each([
['indent.details', 'greater than or equal to 0'],
['indent.json', 'greater than or equal to 0'],
['extrasMaxValueLength', 'a positive number'],
])('%s', (path: string, type: string) => {
it('MUST be an integer', () => {
const options = clone(defaults.options);
Expand Down

0 comments on commit 2d5a629

Please sign in to comment.