object-path-immutable
Advanced tools
Comparing version 0.1.0 to 0.2.0
16
index.js
@@ -155,3 +155,19 @@ (function (root, factory){ | ||
objectPathImmutable.assign = function(obj, path, source) { | ||
return changeImmutable(obj, path, function(clonedObj, finalPath) { | ||
source = Object(source); | ||
var target = clone(clonedObj[finalPath], true) | ||
for (var key in source) { | ||
if (_hasOwnProperty.call(source, key)) { | ||
target[key] = source[key] | ||
} | ||
} | ||
clonedObj[finalPath] = target | ||
return clonedObj | ||
}) | ||
} | ||
return objectPathImmutable | ||
}) |
{ | ||
"name": "object-path-immutable", | ||
"version": "0.1.0", | ||
"version": "0.2.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>", |
@@ -122,2 +122,13 @@ object-path-immutable | ||
//shallow copy properties | ||
var newObj = immutable.assign(obj, 'a', { b: 'f', g: 'h' }) | ||
//returns | ||
//var obj = { | ||
// a: { | ||
// b: 'f', | ||
// c: ['d, 'f'], | ||
// g: 'h' | ||
// } | ||
//} | ||
//Chaining mode. value() at the end of the chain is used to retrieve the resulting object | ||
@@ -134,2 +145,2 @@ var newObj = immutable(obj).set(obj, 'a.b', 'f').del(obj, 'a.c.0').value() | ||
* [Mario Casciaro](https://github.com/mariocasciaro) - Author | ||
* [Mario Casciaro](https://github.com/mariocasciaro) - Author |
53
test.js
@@ -133,2 +133,55 @@ 'use strict'; | ||
}) | ||
}) | ||
describe('assign', function() { | ||
it('should assign an object without modifying the original object', function() { | ||
var obj = { | ||
a: { | ||
b: 1 | ||
}, | ||
c: { | ||
d: 2 | ||
} | ||
} | ||
var newObj = op.assign(obj, 'a', { b: 3 }) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.a).not.to.be.equal(obj.a) | ||
expect(obj.a).to.be.eql({b: 1}) | ||
expect(newObj.c).to.be.equal(obj.c) | ||
expect(newObj.a.b).to.be.equal(3) | ||
}) | ||
it('should keep existing fields that are not overwritten', function() { | ||
var obj = { | ||
a: { | ||
b: 1 | ||
} | ||
} | ||
var newObj = op.assign(obj, 'a', { c: 2 }) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.a).not.to.be.equal(obj.a) | ||
expect(obj.a).to.be.eql({b: 1}) | ||
expect(newObj.a).to.be.eql({ b: 1, c: 2 }) | ||
}) | ||
it('should create intermediate objects', function() { | ||
var obj = { | ||
a: {}, | ||
c: { | ||
d: 2 | ||
} | ||
} | ||
var newObj = op.assign(obj, 'a.b', { f: 'a' }) | ||
expect(newObj).not.to.be.equal(obj) | ||
expect(newObj.a).not.to.be.equal(obj.a) | ||
expect(obj.a).to.be.eql({}) | ||
expect(newObj.a).to.be.eql({b: {f: 'a'}}) | ||
}) | ||
}) |
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
14002
290
145