Skip to content

Commit

Permalink
fix: scaffold over <null> targets in set
Browse files Browse the repository at this point in the history
This ensures `null` targets are treated as if the value
does not exist during scaffolding.

```javascript
set({ a: null }, [ 'a', 'b', 'c' ], 'C')
```
  • Loading branch information
nikku authored and fake-join[bot] committed Mar 15, 2021
1 parent 4421573 commit ebca59f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { forEach } from './collection';
import {
isObject,
isUndefined,
isDefined
isDefined,
isNil
} from './lang';


Expand Down Expand Up @@ -41,7 +42,7 @@ export function set(target, path, value) {
var nextKey = path[idx + 1];
var nextTarget = currentTarget[key];

if (isDefined(nextKey) && !nextTarget) {
if (isDefined(nextKey) && isNil(nextTarget)) {
nextTarget = currentTarget[key] = isNaN(+nextKey) ? {} : [];
}

Expand Down
10 changes: 10 additions & 0 deletions test/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ describe('object', function() {
}
}
});

expect(
set({ a: null }, [ 'a', 'b', 'c' ], 'C')
).to.eql({
a: {
b: {
c: 'C'
}
}
});
});


Expand Down

0 comments on commit ebca59f

Please # to comment.