object-path-immutable
Advanced tools
Comparing version 0.3.0 to 0.4.0
17
index.js
@@ -139,2 +139,19 @@ (function (root, factory){ | ||
} | ||
objectPathImmutable.insert = function (obj, path, value, at) { | ||
at = ~~at; | ||
return changeImmutable(obj, path, function(clonedObj, finalPath) { | ||
var arr = clonedObj[finalPath]; | ||
if (!isArray(arr)) { | ||
if (arr != null && typeof arr !== 'undefined') | ||
throw new Error('Expected ' + path + 'to be an array. Instead got ' + typeof path); | ||
arr = []; | ||
} | ||
var first = arr.slice(0, at) | ||
first.push(value) | ||
clonedObj[finalPath] = first.concat(arr.slice(at)) | ||
return clonedObj | ||
}) | ||
} | ||
@@ -141,0 +158,0 @@ objectPathImmutable.del = function (obj, path, value, at){ |
{ | ||
"name": "object-path-immutable", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.", | ||
@@ -5,0 +5,0 @@ "author": "Mario Casciaro <mariocasciaro@gmail.com>", |
73
test.js
@@ -66,3 +66,76 @@ 'use strict'; | ||
describe('insert', function(){ | ||
it('should insert value into existing array without modifying the object', function(){ | ||
var obj = { | ||
a: ['a'], | ||
c: {} | ||
} | ||
var newObj = op.insert(obj, 'a', 'b', 0) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.a).not.to.be.equal(obj.a) | ||
expect(newObj.c).to.be.equal(obj.c) | ||
expect(newObj.a).to.be.eql(['b', 'a']) | ||
}); | ||
it('should create intermediary array', function(){ | ||
var obj = { | ||
a: [], | ||
c: {} | ||
} | ||
var newObj = op.insert(obj, 'a.0.1', 'b') | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.a).not.to.be.equal(obj.a) | ||
expect(newObj.c).to.be.equal(obj.c) | ||
expect(newObj.a).to.be.eql([[, ['b']]]) | ||
}) | ||
it('should insert in another index', function(){ | ||
var obj = { | ||
a: 'b', | ||
b: { | ||
c: [], | ||
d: ['a', 'b'], | ||
e: [{},{f: 'g'}], | ||
f: 'i' | ||
} | ||
} | ||
var newObj = op.insert(obj, 'b.d', 'asdf', 1) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.b.d).to.be.eql(['a', 'asdf', 'b']) | ||
}); | ||
it('should handle sparse array', function(){ | ||
var obj = { | ||
a: 'b', | ||
b: { | ||
c: [], | ||
d: ['a', 'b'], | ||
e: [{},{f: 'g'}], | ||
f: 'i' | ||
} | ||
} | ||
obj.b.d = new Array(4) | ||
obj.b.d[0] = 'a' | ||
obj.b.d[1] = 'b' | ||
var newObj = op.insert(obj, 'b.d', 'asdf', 3) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.b.d).to.be.eql([ | ||
'a', | ||
'b', | ||
, | ||
'asdf' | ||
, | ||
]) | ||
}) | ||
}) | ||
describe('push', function() { | ||
@@ -69,0 +142,0 @@ it('should push values without modifying the object', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16465
383