diff --git a/readme.md b/readme.md index 0e85b5a..3c7fe15 100644 --- a/readme.md +++ b/readme.md @@ -292,6 +292,8 @@ Get an item or `defaultValue` if the item does not exist. Reset items to their default values, as defined by the `defaults` or `schema` option. +Use `.clear()` to reset all items. + #### .has(key) Check if an item exists. @@ -304,6 +306,8 @@ Delete an item. Delete all items. +This resets known items to their default values, if defined by the `defaults` or `schema` option. + #### .onDidChange(key, callback) `callback`: `(newValue, oldValue) => {}` diff --git a/source/index.ts b/source/index.ts index 3db36d7..1910206 100644 --- a/source/index.ts +++ b/source/index.ts @@ -238,6 +238,8 @@ class Conf = Record> implements I /** Reset items to their default values, as defined by the `defaults` or `schema` option. + @see `clear()` to reset all items. + @param keys - The keys of the items to reset. */ reset(...keys: Key[]): void { @@ -267,9 +269,15 @@ class Conf = Record> implements I /** Delete all items. + + This resets known items to their default values, if defined by the `defaults` or `schema` option. */ clear(): void { this.store = createPlainObject(); + + for (const key of Object.keys(this.#defaultValues)) { + this.reset(key); + } } /** diff --git a/test/index.ts b/test/index.ts index bfee6a2..424b567 100644 --- a/test/index.ts +++ b/test/index.ts @@ -107,8 +107,24 @@ test('.has()', t => { t.false(t.context.config.has('missing')); }); -test('.reset()', t => { - t.context.configWithSchema = new Conf({ +test('.reset() - `defaults` option', t => { + const store = new Conf({ + cwd: tempy.directory(), + defaults: { + foo: 42, + bar: 99 + } + }); + + store.set('foo', 77); + store.set('bar', 0); + store.reset('foo', 'bar'); + t.is(store.get('foo'), 42); + t.is(store.get('bar'), 99); +}); + +test('.reset() - `schema` option', t => { + const store = new Conf({ cwd: tempy.directory(), schema: { foo: { @@ -120,25 +136,11 @@ test('.reset()', t => { } }); - t.context.configWithDefaults = new Conf({ - cwd: tempy.directory(), - defaults: { - foo: 42, - bar: 99 - } - }); - - t.context.configWithSchema.set('foo', 77); - t.context.configWithSchema.set('bar', 0); - t.context.configWithSchema.reset('foo', 'bar'); - t.is(t.context.configWithSchema.get('foo'), 42); - t.is(t.context.configWithSchema.get('bar'), 99); - - t.context.configWithDefaults.set('foo', 77); - t.context.configWithDefaults.set('bar', 0); - t.context.configWithDefaults.reset('foo', 'bar'); - t.is(t.context.configWithDefaults.get('foo'), 42); - t.is(t.context.configWithDefaults.get('bar'), 99); + store.set('foo', 77); + store.set('bar', 0); + store.reset('foo', 'bar'); + t.is(store.get('foo'), 42); + t.is(store.get('bar'), 99); }); test('.delete()', t => { @@ -166,6 +168,40 @@ test('.clear()', t => { t.is(t.context.config.size, 0); }); +test('.clear() - `defaults` option', t => { + const store = new Conf({ + cwd: tempy.directory(), + defaults: { + foo: 42, + bar: 99 + } + }); + + store.set('foo', 2); + store.clear(); + t.is(store.get('foo'), 42); + t.is(store.get('bar'), 99); +}); + +test('.clear() - `schema` option', t => { + const store = new Conf({ + cwd: tempy.directory(), + schema: { + foo: { + default: 42 + }, + bar: { + default: 99 + } + } + }); + + store.set('foo', 2); + store.clear(); + t.is(store.get('foo'), 42); + t.is(store.get('bar'), 99); +}); + test('.size', t => { t.context.config.set('foo', 'bar'); t.is(t.context.config.size, 1);