diff --git a/source/index.ts b/source/index.ts index caa4ab8..1e6e544 100644 --- a/source/index.ts +++ b/source/index.ts @@ -21,6 +21,10 @@ const createPlainObject = (): T => { return Object.create(null); }; +const isExist = (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]; @@ -242,7 +246,7 @@ class Conf = Record> implements I */ reset(...keys: Key[]): void { for (const key of keys) { - if (this.#defaultValues[key]) { + if (isExist(this.#defaultValues[key])) { this.set(key, this.#defaultValues[key]); } } diff --git a/test/index.ts b/test/index.ts index 0acbcdb..f913bed 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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(),