Skip to content

Commit 24916ff

Browse files
Make .delete() return a boolean (#66)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent be84f79 commit 24916ff

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ declare const dotProp: {
3535
@param object - Object to set the `path` value.
3636
@param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
3737
@param value - Value to set at `path`.
38+
@returns The object.
3839
3940
@example
4041
```
@@ -77,6 +78,7 @@ declare const dotProp: {
7778
/**
7879
@param object - Object to delete the `path` value.
7980
@param path - Path of the property in the object, using `.` to separate each nested key. Use `\\.` if you have a `.` in the key.
81+
@returns A boolean of whether the property existed before being deleted.
8082
8183
@example
8284
```
@@ -93,7 +95,7 @@ declare const dotProp: {
9395
//=> {foo: {bar: {y: 'x'}}}
9496
```
9597
*/
96-
delete(object: {[key: string]: any}, path: string): void;
98+
delete(object: {[key: string]: any}, path: string): boolean;
9799
};
98100

99101
export = dotProp;

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module.exports = {
9393

9494
delete(object, path) {
9595
if (!isObj(object) || typeof path !== 'string') {
96-
return;
96+
return false;
9797
}
9898

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

104104
if (i === pathArray.length - 1) {
105105
delete object[p];
106-
return;
106+
return true;
107107
}
108108

109109
object = object[p];
110110

111111
if (!isObj(object)) {
112-
return;
112+
return false;
113113
}
114114
}
115115
},

index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ expectType<typeof object>(dotProp.set(object, 'foo.bar', 'b'));
1616

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

19-
expectType<void>(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar'));
19+
expectType<boolean>(dotProp.delete({foo: {bar: 'a'}}, 'foo.bar'));

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Returns the object.
7171

7272
### delete(object, path)
7373

74+
Returns a boolean of whether the property existed before being deleted.
75+
7476
#### object
7577

7678
Type: `object`

test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,30 +135,30 @@ test('delete', t => {
135135
};
136136

137137
t.is(fixture1.foo.bar.baz.c, 'c');
138-
dotProp.delete(fixture1, 'foo.bar.baz.c');
138+
t.true(dotProp.delete(fixture1, 'foo.bar.baz.c'));
139139
t.is(fixture1.foo.bar.baz.c, undefined);
140140

141141
t.is(fixture1.top.dog, 'sindre');
142-
dotProp.delete(fixture1, 'top');
142+
t.true(dotProp.delete(fixture1, 'top'));
143143
t.is(fixture1.top, undefined);
144144

145145
t.is(fixture1.foo.bar.baz.func.foo, 'bar');
146-
dotProp.delete(fixture1, 'foo.bar.baz.func.foo');
146+
t.true(dotProp.delete(fixture1, 'foo.bar.baz.func.foo'));
147147
t.is(fixture1.foo.bar.baz.func.foo, undefined);
148148

149149
t.is(fixture1.foo.bar.baz.func, func);
150-
dotProp.delete(fixture1, 'foo.bar.baz.func');
150+
t.true(dotProp.delete(fixture1, 'foo.bar.baz.func'));
151151
t.is(fixture1.foo.bar.baz.func, undefined);
152152

153153
dotProp.set(fixture1, 'foo\\.bar.baz', true);
154154
t.true(fixture1['foo.bar'].baz);
155-
dotProp.delete(fixture1, 'foo\\.bar.baz');
155+
t.true(dotProp.delete(fixture1, 'foo\\.bar.baz'));
156156
t.is(fixture1['foo.bar'].baz, undefined);
157157

158158
const fixture2 = {};
159159
dotProp.set(fixture2, 'foo.bar\\.baz', true);
160160
t.true(fixture2.foo['bar.baz']);
161-
dotProp.delete(fixture2, 'foo.bar\\.baz');
161+
t.true(dotProp.delete(fixture2, 'foo.bar\\.baz'));
162162
t.is(fixture2.foo['bar.baz'], undefined);
163163

164164
fixture2.dotted = {
@@ -167,12 +167,12 @@ test('delete', t => {
167167
other: 'prop'
168168
}
169169
};
170-
dotProp.delete(fixture2, 'dotted.sub.dotted\\.prop');
170+
t.true(dotProp.delete(fixture2, 'dotted.sub.dotted\\.prop'));
171171
t.is(fixture2.dotted.sub['dotted.prop'], undefined);
172172
t.is(fixture2.dotted.sub.other, 'prop');
173173

174174
const fixture3 = {foo: null};
175-
dotProp.delete(fixture3, 'foo.bar');
175+
t.false(dotProp.delete(fixture3, 'foo.bar'));
176176
t.deepEqual(fixture3, {foo: null});
177177
});
178178

0 commit comments

Comments
 (0)