Skip to content

Commit

Permalink
fix(options)!: rename extrasKey to extras.key, which is an object
Browse files Browse the repository at this point in the history
Allow for future expansion of the `extras` option by moving the `key` to an object.
  • Loading branch information
jdbruijn committed Mar 25, 2022
1 parent 6c6aad0 commit c638413
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ class Formatter {
return parsed;
}

const extras = is.undefined(this._options.extrasKey)
const extras = is.undefined(this._options.extras.key)
? {}
: parsed.details[this._options.extrasKey];
if (this._options.extrasKey !== undefined && is.nonEmptyObject(extras)) {
const extrasKey = this._options.extrasKey;
: parsed.details[this._options.extras.key];
if (this._options.extras.key !== undefined && is.nonEmptyObject(extras)) {
const extrasKey = this._options.extras.key;
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) {
} else if (this._options.extras.key === undefined) {
Object.entries(parsed.details).forEach(([key, value]) => {
if (this.isExtra(value)) {
parsed.extras[key] = value;
Expand Down
1 change: 0 additions & 1 deletion src/formatter.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Formatter', () => {
source: false,
extras: false,
},
extrasKey: '',
indent: {
details: 4,
json: 2,
Expand Down
15 changes: 10 additions & 5 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ const options = z
})
.strict()
.default({}),
extrasKey: z
.string()
.min(1)
.regex(new RegExp(`^((?!(${bunyanCoreFields().join('|')})).)*$`))
.optional(), // Exlcude bunyan core fields.
extras: z
.object({
key: z
.string()
.min(1)
.regex(new RegExp(`^((?!(${bunyanCoreFields().join('|')})).)*$`))
.optional(),
})
.strict()
.default({}),
indent: z
.object({
details: z.number().int().nonnegative().default(4),
Expand Down
7 changes: 3 additions & 4 deletions src/options.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('schema', () => {
source: false,
extras: false,
},
extrasKey: 'extras',
indent: {
details: 4,
json: 2,
Expand Down Expand Up @@ -58,7 +57,7 @@ describe('schema', () => {
['show.pid', 'boolean', false],
['show.source', 'boolean', false],
['show.extras', 'boolean', true],
['extrasKey', 'string', undefined],
['extras.key', 'string', undefined],
['indent.details', 'number', 4],
['indent.json', 'number', 2],
['basePath', 'string', '/'],
Expand Down Expand Up @@ -173,13 +172,13 @@ describe('schema', () => {
});
});

describe('extrasKey', () => {
describe('extras.key', () => {
it.each([
...coreFields().map((e) => ['Bunyan core field', e]),
['an empty value', ''],
])('disallows %s "%s"', (_, value: string) => {
const options = clone(defaults.options);
dotProp.set(options, 'extrasKey', value);
dotProp.set(options, 'extras.key', value);
const parsed = schema.safeParse(options);
// const validation = schema.validate(options);

Expand Down

0 comments on commit c638413

Please sign in to comment.