-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move record source validation to its own file
- Loading branch information
Showing
10 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters