From 9c75217b96b11541ab331747b06ae7024c1bb20d Mon Sep 17 00:00:00 2001 From: Flamenco Date: Thu, 29 Dec 2016 05:58:50 -0500 Subject: [PATCH] Allow number for path (#4) * Allow number for path * Fix typos * Add more tests --- README.md | 2 +- index.js | 6 +-- test/dot-prop-immutable-number.spec.js | 62 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/dot-prop-immutable-number.spec.js diff --git a/README.md b/README.md index 8352d4f..99a6d3a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ set(object, path, value) --> object delete(object, path) --> object ``` -None of the function mutate the input object. For efficiency the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path. +None of the functions mutate the input object. For efficiency the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path. ## Usage diff --git a/index.js b/index.js index 41c9a1d..5e255a8 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; module.exports.set = function(obj, prop, value) { - prop = typeof prop === 'string' ? propToArray(prop) : prop; + prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop; var setPropImmutableRec = function(obj, prop, value, i) { var clone, head = prop[i]; @@ -24,7 +24,7 @@ module.exports.set = function(obj, prop, value) { }; module.exports.get = function(obj, prop) { - prop = typeof prop === 'string' ? propToArray(prop) : prop; + prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop; for (var i = 0; i < prop.length; i++) { if (typeof obj !== 'object') { @@ -41,7 +41,7 @@ module.exports.get = function(obj, prop) { }; module.exports.delete = function(obj, prop) { - prop = typeof prop === 'string' ? propToArray(prop) : prop; + prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop; var deletePropImmutableRec = function(obj, prop, i) { var clone, head = prop[i]; diff --git a/test/dot-prop-immutable-number.spec.js b/test/dot-prop-immutable-number.spec.js new file mode 100644 index 0000000..50f0815 --- /dev/null +++ b/test/dot-prop-immutable-number.spec.js @@ -0,0 +1,62 @@ +var dotProp = require('..'); + +describe('dot-prop-immutable.number.spec.js', function () { + + var arr = [1, {a: false}]; + + var result; + describe('when have an array', () => { + + describe('when set prop using number as path', () => { + + before(function () { + result = dotProp.set(arr, 1, 3); + }); + + it('should replace prop', () => { + expect(result).to.eql([ + 1, + 3 + ]); + }); + + it('invariant', arrInvariant); + }); + + + describe('when get prop using number as path', () => { + + before(function () { + result = dotProp.get(arr, 1); + }); + + it('should get prop', () => { + expect(result).to.eql({a: false}); + }); + + it('invariant', arrInvariant); + }); + + describe('when delete prop using number as path', () => { + + before(function () { + result = dotProp.delete(arr, 1); + }); + + it('should remove prop', () => { + expect(result).to.eql([ + 1 + ]); + }); + it('invariant', arrInvariant); + }); + }); + + function arrInvariant() { + expect(arr).to.eql( + [1, {a: false}] + ); + } +}); + +