Skip to content

Commit

Permalink
Make .delete() return a boolean (#66)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
Richienb and sindresorhus committed Sep 6, 2020
1 parent be84f79 commit 24916ff
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ declare const dotProp: {
@param object - Object to set the `path` value.
@param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
@param value - Value to set at `path`.
@returns The object.
@example
```
Expand Down Expand Up @@ -77,6 +78,7 @@ declare const dotProp: {
/**
@param object - Object to delete the `path` value.
@param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
@returns A boolean of whether the property existed before being deleted.
@example
```
Expand All @@ -93,7 +95,7 @@ declare const dotProp: {
//=> {foo: {bar: {y: 'x'}}}
```
*/
delete(object: {[key: string]: any}, path: string): void;
delete(object: {[key: string]: any}, path: string): boolean;
};

export = dotProp;
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports = {

delete(object, path) {
if (!isObj(object) || typeof path !== 'string') {
return;
return false;
}

const pathArray = getPathSegments(path);
Expand All @@ -103,13 +103,13 @@ module.exports = {

if (i === pathArray.length - 1) {
delete object[p];
return;
return true;
}

object = object[p];

if (!isObj(object)) {
return;
return false;
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ expectType<typeof object>(dotProp.set(object, 'foo.bar', 'b'));

expectType<boolean>(dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar'));

expectType<void>(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar'));
expectType<boolean>(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar'));
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Returns the object.

### delete(object, path)

Returns a boolean of whether the property existed before being deleted.

#### object

Type: `object`
Expand Down
16 changes: 8 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,30 @@ test('delete', t => {
};

t.is(fixture1.foo.bar.baz.c, 'c');
dotProp.delete(fixture1, 'foo.bar.baz.c');
t.true(dotProp.delete(fixture1, 'foo.bar.baz.c'));
t.is(fixture1.foo.bar.baz.c, undefined);

t.is(fixture1.top.dog, 'sindre');
dotProp.delete(fixture1, 'top');
t.true(dotProp.delete(fixture1, 'top'));
t.is(fixture1.top, undefined);

t.is(fixture1.foo.bar.baz.func.foo, 'bar');
dotProp.delete(fixture1, 'foo.bar.baz.func.foo');
t.true(dotProp.delete(fixture1, 'foo.bar.baz.func.foo'));
t.is(fixture1.foo.bar.baz.func.foo, undefined);

t.is(fixture1.foo.bar.baz.func, func);
dotProp.delete(fixture1, 'foo.bar.baz.func');
t.true(dotProp.delete(fixture1, 'foo.bar.baz.func'));
t.is(fixture1.foo.bar.baz.func, undefined);

dotProp.set(fixture1, 'foo\\.bar.baz', true);
t.true(fixture1['foo.bar'].baz);
dotProp.delete(fixture1, 'foo\\.bar.baz');
t.true(dotProp.delete(fixture1, 'foo\\.bar.baz'));
t.is(fixture1['foo.bar'].baz, undefined);

const fixture2 = {};
dotProp.set(fixture2, 'foo.bar\\.baz', true);
t.true(fixture2.foo['bar.baz']);
dotProp.delete(fixture2, 'foo.bar\\.baz');
t.true(dotProp.delete(fixture2, 'foo.bar\\.baz'));
t.is(fixture2.foo['bar.baz'], undefined);

fixture2.dotted = {
Expand All @@ -167,12 +167,12 @@ test('delete', t => {
other: 'prop'
}
};
dotProp.delete(fixture2, 'dotted.sub.dotted\\.prop');
t.true(dotProp.delete(fixture2, 'dotted.sub.dotted\\.prop'));
t.is(fixture2.dotted.sub['dotted.prop'], undefined);
t.is(fixture2.dotted.sub.other, 'prop');

const fixture3 = {foo: null};
dotProp.delete(fixture3, 'foo.bar');
t.false(dotProp.delete(fixture3, 'foo.bar'));
t.deepEqual(fixture3, {foo: null});
});

Expand Down

0 comments on commit 24916ff

Please sign in to comment.