From 24916ff494d84ce3af1cf5a62cd8c9b37507e492 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Mon, 7 Sep 2020 02:13:10 +1200 Subject: [PATCH] Make `.delete()` return a boolean (#66) Co-authored-by: Sindre Sorhus --- index.d.ts | 4 +++- index.js | 6 +++--- index.test-d.ts | 2 +- readme.md | 2 ++ test.js | 16 ++++++++-------- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index 21b0545..861f533 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 ``` @@ -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 ``` @@ -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; diff --git a/index.js b/index.js index 03099a4..a90f599 100644 --- a/index.js +++ b/index.js @@ -93,7 +93,7 @@ module.exports = { delete(object, path) { if (!isObj(object) || typeof path !== 'string') { - return; + return false; } const pathArray = getPathSegments(path); @@ -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; } } }, diff --git a/index.test-d.ts b/index.test-d.ts index b20181e..65af7de 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -16,4 +16,4 @@ expectType(dotProp.set(object, 'foo.bar', 'b')); expectType(dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar')); -expectType(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar')); +expectType(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar')); diff --git a/readme.md b/readme.md index 735b0a2..4170565 100644 --- a/readme.md +++ b/readme.md @@ -71,6 +71,8 @@ Returns the object. ### delete(object, path) +Returns a boolean of whether the property existed before being deleted. + #### object Type: `object` diff --git a/test.js b/test.js index bed3fbe..a33361f 100644 --- a/test.js +++ b/test.js @@ -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 = { @@ -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}); });