Repository: sindresorhus/dot-prop
Pull request #66 (2020-08-26 03:53:09)

Make `.delete()` return a boolean
Fixes: #65 

// @sindresorhus 

sindresorhus: I misunderstood your request. I thought you mean to return whether it was deleted or not. I think that makes more sense. Like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete
Richienb: @sindresorhus I've changed the logic to implement that. Travis CI is not working right now.

Patch:
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/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`


Test patch:
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<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'));
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});
 });