Skip to content

Commit

Permalink
Fix .clear() behavior (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 7, 2021
1 parent 43c644f commit 259ba9b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) => {}`
Expand Down
8 changes: 8 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> 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<Key extends keyof T>(...keys: Key[]): void {
Expand Down Expand Up @@ -267,9 +269,15 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> 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);
}
}

/**
Expand Down
78 changes: 57 additions & 21 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 259ba9b

Please sign in to comment.