Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setUndefined option #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 33 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ set(obj, 'foo.bar.fez', 'zzz', { merge: true });
//=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } }
```

### options.setUndefined

Set the value to undefined instead of deleting the property when the value is undefined.

**Type**: `boolean`

**Default**: `false`

**Example**

```js
console.log(set({foo: 'bar', baz: 'foo'}, 'foo', undefined, { setUndefined: false }));
//=> { baz: 'foo' }

console.log(set({foo: 'bar', baz: 'foo'}, 'foo', undefined, { setUndefined: true }));
//=> { foo: undefined, baz: 'foo' }
```


## Benchmarks

Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3.
Expand Down Expand Up @@ -271,18 +290,19 @@ You might also be interested in these projects:

### Contributors

| **Commits** | **Contributor** |
| --- | --- |
| 87 | [jonschlinkert](https://github.com/jonschlinkert) |
| 4 | [doowb](https://github.com/doowb) |
| 2 | [mbelsky](https://github.com/mbelsky) |
| 1 | [dkebler](https://github.com/dkebler) |
| 1 | [GlennKintscher](https://github.com/GlennKintscher) |
| 1 | [petermorlion](https://github.com/petermorlion) |
| 1 | [abetomo](https://github.com/abetomo) |
| 1 | [zeidoo](https://github.com/zeidoo) |
| 1 | [ready-research](https://github.com/ready-research) |
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
| **Commits** | **Contributor** |
| --- | --- |
| 87 | [jonschlinkert](https://github.com/jonschlinkert) |
| 4 | [doowb](https://github.com/doowb) |
| 2 | [mbelsky](https://github.com/mbelsky) |
| 1 | [dkebler](https://github.com/dkebler) |
| 1 | [GlennKintscher](https://github.com/GlennKintscher) |
| 1 | [petermorlion](https://github.com/petermorlion) |
| 1 | [abetomo](https://github.com/abetomo) |
| 1 | [zeidoo](https://github.com/zeidoo) |
| 1 | [ready-research](https://github.com/ready-research) |
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
| 1 | [snowbldr](https://github.com/snowbldr) |

### Author

Expand All @@ -299,4 +319,4 @@ Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 12, 2021._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 12, 2021._
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const split = (input, options) => {
const assignProp = (obj, prop, value, options) => {
validateKey(prop);

// Delete property when "value" is undefined
if (value === undefined) {
// Delete property when "value" is undefined and setUndefined is falsey
if (value === undefined && (!options || !options.setUndefined)) {
deleteProperty(obj, prop);

} else if (options && options.merge) {
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ describe('set-value', () => {
assert.deepEqual(set({ a: 'a', b: { c: 'd' } }, 'b.c'), { a: 'a', b: {} });
});

it('should delete the property undefined when setUndefined is false', () => {
const fixture = {};
assert.deepEqual(set(fixture, 'a.locals.name', undefined, { setUndefined: false }), { a: { locals: {} } });
assert.deepEqual(set(fixture, 'b.locals.name', undefined, { setUndefined: false }), { b: { locals: {} }, a: { locals: {} } });
assert.deepEqual(set({ a: 'a', b: { c: 'd' } }, 'b.c', undefined, { setUndefined: false }), { a: 'a', b: {} });
});

it('should set the property undefined when setUndefined is true', () => {
const fixture = {};
assert.deepEqual(
set(fixture, 'a.locals.name', undefined, { setUndefined: true }),
{ a: { locals: {name: undefined} } }
);
assert.deepEqual(
set(fixture, 'b.locals.name', undefined, { setUndefined: true }),
{ b: { locals: {name: undefined} }, a: { locals: {name: undefined} } }
);
assert.deepEqual(
set({ a: 'a', b: { c: 'd' } }, 'b.c', undefined, { setUndefined: true }),
{ a: 'a', b: { c: undefined } }
);
});

it('should return the entire object if no property is passed.', () => {
assert.deepEqual(set({ a: 'a', b: { c: 'd' } }), { a: 'a', b: { c: 'd' } });
});
Expand Down