Skip to content

Commit

Permalink
refactor: move record source validation to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruijn committed Jun 1, 2023
1 parent 7ef5b0f commit 38486f0
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bunyan/from-json-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import {deleteProperty} from 'dot-prop';
import fromJsonString from './from-json-string.js';
import type BunyanRecord from './record.js';
import {type BunyanRecord} from './record.js';

const bunyanRecord: BunyanRecord = {
v: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/bunyan/from-json-string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isBunyanRecord from './is-record.js';
import type BunyanRecord from './record.js';
import {type BunyanRecord} from './record.js';

function fromJsonString(json: string): BunyanRecord {
const record: unknown = JSON.parse(json, (key, value) => {
Expand Down
3 changes: 2 additions & 1 deletion src/bunyan/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {default as isSource} from './is-source.js';
export {default as isBunyanRecord} from './is-record.js';
export {type default as BunyanRecord} from './record.js';
export type {BunyanRecord, Source} from './record.js';
export {default as coreFields} from './core-fields.js';
export {default as fromJsonString} from './from-json-string.js';
export {default as levels} from './levels.js';
2 changes: 1 addition & 1 deletion src/bunyan/is-record.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import {deleteProperty} from 'dot-prop';
import type BunyanRecord from './record.js';
import {type BunyanRecord} from './record.js';
import isBunyanRecord from './is-record.js';

const bunyanRecord: Readonly<BunyanRecord> = {
Expand Down
8 changes: 3 additions & 5 deletions src/bunyan/is-record.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import type BunyanRecord from './record.js';
import type {BunyanRecord} from './record.js';
import isSource from './is-source.js';

function isBunyanRecord(value: unknown): value is BunyanRecord {
return (
Expand All @@ -11,10 +12,7 @@ function isBunyanRecord(value: unknown): value is BunyanRecord {
is.number(value.pid) &&
is.date(value.time) &&
is.string(value.msg) &&
(is.undefined(value.src) ||
(is.plainObject(value.src) &&
is.number(value.src.line) &&
(is.undefined(value.src.func) || is.string(value.src.func))))
(is.undefined(value.src) || isSource(value.src))
);
}

Expand Down
40 changes: 40 additions & 0 deletions src/bunyan/is-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import test from 'ava';
import {deleteProperty} from 'dot-prop';
import {type BunyanRecord, type Source} from './record.js';
import isSource from './is-source.js';

const source: Readonly<BunyanRecord['src']> = {
file: '',
line: 0,
func: '',
};

const returnsWithout = test.macro<[boolean, keyof Source]>({
exec(t, expected, key) {
const value = structuredClone(source);
deleteProperty(value, key);
(expected ? t.true : t.false)(isSource(value));
},
title(_, expected, key) {
const expectedString = expected ? 'true' : 'false';
return `returns "${expectedString}" for a source without "${key}"`;
},
});

test(returnsWithout, false, 'line');
test(returnsWithout, true, 'func');

test('returns "true" for a source with all values', (t) => {
t.true(isSource(source));
});

test('returns "false" for an empty object', (t) => {
t.false(isSource({}));
});

test('narrows the type to "Source"', (t) => {
const value: unknown = structuredClone(source);
if (isSource(value)) {
t.is(value.file, source.file);
}
});
12 changes: 12 additions & 0 deletions src/bunyan/is-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import is from '@sindresorhus/is';
import type {Source} from './record.js';

function isSource(value: unknown): value is Source {
return (
is.plainObject(value) &&
is.number(value.line) &&
(is.undefined(value.func) || is.string(value.func))
);
}

export default isSource;
14 changes: 8 additions & 6 deletions src/bunyan/record.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
type Source = {
file: string;
line: number;
func?: string;
};

type BunyanRecord = {
[key: string]: unknown;
v: number;
Expand All @@ -7,11 +13,7 @@ type BunyanRecord = {
pid: number;
time: Date;
msg: string;
src?: {
file: string;
line: number;
func?: string;
};
src?: Source;
};

export default BunyanRecord;
export type {Source, BunyanRecord};
2 changes: 1 addition & 1 deletion src/formatter/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import bunyan from 'bunyan';
import type {ChalkInstance as Chalk} from 'chalk';
import chalk from 'chalk';
import stringify from 'json-stringify-pretty-compact';
import type BunyanRecord from '../bunyan/record.js';
import {type BunyanRecord} from '../bunyan/record.js';
import type {PublicOptions, Options} from '../options.js';
import {schema} from '../options.js';
import type {ParsedRecord} from '../parser/parser.js';
Expand Down
3 changes: 1 addition & 2 deletions src/parser/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import test from 'ava';
import type BunyanRecord from '../bunyan/record.js';
import type {ParsedRecord} from './parser.js';
import {type BunyanRecord} from '../bunyan/record.js';
import Parser from './parser.js';

const options = {
Expand Down

0 comments on commit 38486f0

Please sign in to comment.