Skip to content

Commit

Permalink
fix bug for false value
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Rasmussen committed Nov 16, 2015
1 parent e5a1199 commit 83861fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ module.exports.set = function(obj, prop, value) {
}
if (/^\+?\d+$/.test(head) && obj.length > head) {
head = parseInt(head);
return [].concat(obj.slice(0, head), setPropImmutableRec(obj[head] || {}, prop, value, i + 1), obj.slice(head + 1));
return [].concat(obj.slice(0, head), setPropImmutableRec(obj[head] !== undefined ? obj[head] : {}, prop, value, i + 1), obj.slice(head + 1));
} else {
let clone = obj.slice();
clone[head] = setPropImmutableRec(obj[head] || {}, prop, value, i + 1);
clone[head] = setPropImmutableRec(obj[head] !== undefined ? obj[head] : {}, prop, value, i + 1);
return clone;
}

} else {
return Object.assign({}, obj, {[head]: setPropImmutableRec(obj[head] || {}, prop, value, i + 1)});
return Object.assign({}, obj, {[head]: setPropImmutableRec(obj[head] !== undefined ? obj[head] : {}, prop, value, i + 1)});
}
}

return typeof value === 'function' ? value(obj) : value;
};

return setPropImmutableRec(obj, prop, value, 0);
return setPropImmutableRec(obj, prop, value, 0, null);
};

module.exports.get = function(obj, prop) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dot-prop-immutable",
"version": "1.0.0",
"version": "1.0.1",
"description": "Immutable version of dot-prop with some extensions",
"main": "index.js",
"engines": {
Expand Down
13 changes: 6 additions & 7 deletions test/dot-prop-immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('dot-prop-immutable.spec.js', function() {
'b.x': 10
};

var arr = [1, { b: 2}];
var arr = [1, { a: false}];

var result;

Expand Down Expand Up @@ -271,7 +271,7 @@ describe('dot-prop-immutable.spec.js', function() {

it('should replace prop', () => {
expect(result).to.eql(
[3, {b: 2}]
[3, {a: false}]
);
});

Expand All @@ -281,18 +281,17 @@ describe('dot-prop-immutable.spec.js', function() {
describe('when set array[index] deep prop', () => {

before(function () {
result = dotProp.set(arr, '1.b', 3);
result = dotProp.set(arr, '1.a', v => !v);
});

it('should replace prop', () => {
expect(result).to.eql(
[1, {b: 3}]
[1, {a: true}]
);
});

it('invariant', arrInvariant);
});

});
});

Expand Down Expand Up @@ -390,7 +389,7 @@ describe('dot-prop-immutable.spec.js', function() {
describe('when get array[index] deep prop', () => {

it('should replace prop', () => {
expect(dotProp.get(arr, '1.b')).to.equal(2);
expect(dotProp.get(arr, '1.a')).to.equal(false);
});
});

Expand All @@ -411,7 +410,7 @@ describe('dot-prop-immutable.spec.js', function() {

function arrInvariant() {
expect(arr).to.eql(
[1, {b: 2}]
[1, {a: false}]
);
}
});
Expand Down

0 comments on commit 83861fd

Please sign in to comment.