Skip to content

Commit

Permalink
Fix .reset() to correctly handle falsy default values (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamontat committed Feb 7, 2021
1 parent 4f77f08 commit 735225e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/index.ts
Expand Up @@ -21,6 +21,10 @@ const createPlainObject = <T = unknown>(): T => {
return Object.create(null);
};

const isExist = <T = unknown>(data: T): boolean => {
return data !== undefined && data !== null;
};

// Prevent caching of this module so module.parent is always accurate
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[__filename];
Expand Down Expand Up @@ -242,7 +246,7 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
*/
reset<Key extends keyof T>(...keys: Key[]): void {
for (const key of keys) {
if (this.#defaultValues[key]) {
if (isExist(this.#defaultValues[key])) {
this.set(key, this.#defaultValues[key]);
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/index.ts
Expand Up @@ -123,6 +123,36 @@ test('.reset() - `defaults` option', t => {
t.is(store.get('bar'), 99);
});

test('.reset() - falsy `defaults` option', t => {
const defaultsValue: {
foo: number;
bar: string;
fox: boolean;
bax: boolean;
} = {
foo: 0,
bar: '',
fox: false,
bax: true
};
const store = new Conf({
cwd: tempy.directory(),
defaults: defaultsValue
});

store.set('foo', 5);
store.set('bar', 'exist');
store.set('fox', true);
store.set('fox', false);

store.reset('foo', 'bar', 'fox', 'bax');

t.is(store.get('foo'), 0);
t.is(store.get('bar'), '');
t.is(store.get('fox'), false);
t.is(store.get('bax'), true);
});

test('.reset() - `schema` option', t => {
const store = new Conf({
cwd: tempy.directory(),
Expand Down

0 comments on commit 735225e

Please sign in to comment.