From 83861fdbf2648f88406665b3be68da5d62d1f981 Mon Sep 17 00:00:00 2001 From: Dan Rasmussen Date: Mon, 16 Nov 2015 12:24:12 +0100 Subject: [PATCH] fix bug for false value --- index.js | 8 ++++---- package.json | 2 +- test/dot-prop-immutable.spec.js | 13 ++++++------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 129c4ad..3957a9c 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/package.json b/package.json index cc39d59..b8eedca 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/dot-prop-immutable.spec.js b/test/dot-prop-immutable.spec.js index 97b9736..a0eb6c5 100644 --- a/test/dot-prop-immutable.spec.js +++ b/test/dot-prop-immutable.spec.js @@ -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; @@ -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}] ); }); @@ -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); }); - }); }); @@ -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); }); }); @@ -411,7 +410,7 @@ describe('dot-prop-immutable.spec.js', function() { function arrInvariant() { expect(arr).to.eql( - [1, {b: 2}] + [1, {a: false}] ); } });