object-path
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "object-path", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
14
index.js
@@ -146,2 +146,12 @@ (function (root, factory){ | ||
objectPath.insert = function (obj, path, value, at){ | ||
var arr = objectPath.get(obj, path); | ||
at = ~~at; | ||
if (!isArray(arr)) { | ||
arr = []; | ||
objectPath.set(obj, path, arr); | ||
} | ||
arr.splice(at, 0, value); | ||
}; | ||
objectPath.empty = function(obj, path) { | ||
@@ -185,4 +195,4 @@ if (isEmpty(path)) { | ||
} | ||
var args = Array.prototype.slice.call(arguments, 2); | ||
arr.push.apply(arr, args); | ||
arr.push.apply(arr, Array.prototype.slice.call(arguments, 2)); | ||
}; | ||
@@ -189,0 +199,0 @@ |
{ | ||
"name": "object-path", | ||
"description": "Access deep properties using a path", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mario Casciaro" |
@@ -47,7 +47,7 @@ | ||
//get the first non-undefined value | ||
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); | ||
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); | ||
//empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays. | ||
//functions that are not inherited from prototype are set to null. | ||
//object instances are considered objects and just own property names are deleted | ||
//empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays. | ||
//functions that are not inherited from prototype are set to null. | ||
//object instances are considered objects and just own property names are deleted | ||
objectPath.empty(obj, 'a.b'); // obj.a.b is now '' | ||
@@ -71,2 +71,5 @@ objectPath.empty(obj, 'a.c'); // obj.a.c is now [] | ||
//will insert values in array | ||
objectPath.insert(obj, "a.c", "m", 1); // obj.a.c = ["e", "m", "f"] | ||
//push into arrays (and create intermediate objects/arrays) | ||
@@ -73,0 +76,0 @@ objectPath.push(obj, "a.k", "o"); |
219
test.js
@@ -7,8 +7,8 @@ var expect = require('chai').expect, | ||
return { | ||
a: "b", | ||
a: 'b', | ||
b: { | ||
c: [], | ||
d: ["a", "b"], | ||
e: [{},{f: "g"}], | ||
f: "i" | ||
d: ['a', 'b'], | ||
e: [{},{f: 'g'}], | ||
f: 'i' | ||
} | ||
@@ -21,4 +21,4 @@ }; | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "a")).to.be.equal("b"); | ||
expect(objectPath.get(obj, ["a"])).to.be.equal("b"); | ||
expect(objectPath.get(obj, 'a')).to.be.equal('b'); | ||
expect(objectPath.get(obj, ['a'])).to.be.equal('b'); | ||
}); | ||
@@ -28,3 +28,3 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj.b.d, 0)).to.be.equal("a"); | ||
expect(objectPath.get(obj.b.d, 0)).to.be.equal('a'); | ||
expect(objectPath.get(obj.b, 0)).to.be.equal(void 0); | ||
@@ -35,4 +35,4 @@ }); | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "b.f")).to.be.equal("i"); | ||
expect(objectPath.get(obj, ["b","f"])).to.be.equal("i"); | ||
expect(objectPath.get(obj, 'b.f')).to.be.equal('i'); | ||
expect(objectPath.get(obj, ['b','f'])).to.be.equal('i'); | ||
}); | ||
@@ -42,4 +42,4 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "b.d.0")).to.be.equal("a"); | ||
expect(objectPath.get(obj, ["b","d",0])).to.be.equal("a"); | ||
expect(objectPath.get(obj, 'b.d.0')).to.be.equal('a'); | ||
expect(objectPath.get(obj, ['b','d',0])).to.be.equal('a'); | ||
}); | ||
@@ -49,4 +49,4 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "b.e.1.f")).to.be.equal("g"); | ||
expect(objectPath.get(obj, ["b","e",1,"f"])).to.be.equal("g"); | ||
expect(objectPath.get(obj, 'b.e.1.f')).to.be.equal('g'); | ||
expect(objectPath.get(obj, ['b','e',1,'f'])).to.be.equal('g'); | ||
}); | ||
@@ -56,4 +56,4 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "a.b")).to.not.exist; | ||
expect(objectPath.get(obj, ["a","b"])).to.not.exist; | ||
expect(objectPath.get(obj, 'a.b')).to.not.exist; | ||
expect(objectPath.get(obj, ['a','b'])).to.not.exist; | ||
}); | ||
@@ -63,23 +63,23 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.get(obj, "b.d.5")).to.not.exist; | ||
expect(objectPath.get(obj, ["b","d","5"])).to.not.exist; | ||
expect(objectPath.get(obj, 'b.d.5')).to.not.exist; | ||
expect(objectPath.get(obj, ['b','d','5'])).to.not.exist; | ||
}); | ||
it('should return the value under integer-like key', function() { | ||
var obj = { "1a": "foo" }; | ||
expect(objectPath.get(obj, "1a")).to.be.equal("foo"); | ||
expect(objectPath.get(obj, ["1a"])).to.be.equal("foo"); | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '1a')).to.be.equal('foo'); | ||
expect(objectPath.get(obj, ['1a'])).to.be.equal('foo'); | ||
}); | ||
it('should return the default value when the key doesnt exist', function() { | ||
var obj = { "1a": "foo" }; | ||
expect(objectPath.get(obj, "1b", null)).to.be.equal(null); | ||
expect(objectPath.get(obj, ["1b"], null)).to.be.equal(null); | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '1b', null)).to.be.equal(null); | ||
expect(objectPath.get(obj, ['1b'], null)).to.be.equal(null); | ||
}); | ||
it('should return the default value when path is empty', function() { | ||
var obj = { "1a": "foo" }; | ||
expect(objectPath.get(obj, "", null)).to.be.deep.equal({ "1a": "foo" }); | ||
expect(objectPath.get(obj, [])).to.be.deep.equal({ "1a": "foo" }); | ||
expect(objectPath.get({ }, ["1"])).to.be.equal(undefined); | ||
var obj = { '1a': 'foo' }; | ||
expect(objectPath.get(obj, '', null)).to.be.deep.equal({ '1a': 'foo' }); | ||
expect(objectPath.get(obj, [])).to.be.deep.equal({ '1a': 'foo' }); | ||
expect(objectPath.get({ }, ['1'])).to.be.equal(undefined); | ||
}); | ||
@@ -112,7 +112,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "c", {m: "o"}); | ||
expect(obj).to.have.deep.property("c.m", "o"); | ||
objectPath.set(obj, 'c', {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["c"], {m: "o"}); | ||
expect(obj).to.have.deep.property("c.m", "o"); | ||
objectPath.set(obj, ['c'], {m: 'o'}); | ||
expect(obj).to.have.deep.property('c.m', 'o'); | ||
}); | ||
@@ -122,4 +122,4 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj.b.d, 0, "o"); | ||
expect(obj).to.have.deep.property("b.d.0", "o"); | ||
objectPath.set(obj.b.d, 0, 'o'); | ||
expect(obj).to.have.deep.property('b.d.0', 'o'); | ||
}); | ||
@@ -129,7 +129,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "b.c", "o"); | ||
expect(obj).to.have.deep.property("b.c", "o"); | ||
objectPath.set(obj, 'b.c', 'o'); | ||
expect(obj).to.have.deep.property('b.c', 'o'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["b","c"], "o"); | ||
expect(obj).to.have.deep.property("b.c", "o"); | ||
objectPath.set(obj, ['b','c'], 'o'); | ||
expect(obj).to.have.deep.property('b.c', 'o'); | ||
}); | ||
@@ -139,7 +139,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "b.e.1.g", "f"); | ||
expect(obj).to.have.deep.property("b.e.1.g", "f"); | ||
objectPath.set(obj, 'b.e.1.g', 'f'); | ||
expect(obj).to.have.deep.property('b.e.1.g', 'f'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["b","e",1,"g"], "f"); | ||
expect(obj).to.have.deep.property("b.e.1.g", "f"); | ||
objectPath.set(obj, ['b','e',1,'g'], 'f'); | ||
expect(obj).to.have.deep.property('b.e.1.g', 'f'); | ||
}); | ||
@@ -149,7 +149,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "c.d.e.f", "l"); | ||
expect(obj).to.have.deep.property("c.d.e.f", "l"); | ||
objectPath.set(obj, 'c.d.e.f', 'l'); | ||
expect(obj).to.have.deep.property('c.d.e.f', 'l'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["c","d","e","f"], "l"); | ||
expect(obj).to.have.deep.property("c.d.e.f", "l"); | ||
objectPath.set(obj, ['c','d','e','f'], 'l'); | ||
expect(obj).to.have.deep.property('c.d.e.f', 'l'); | ||
}); | ||
@@ -159,9 +159,9 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "c.0.1.m", "l"); | ||
expect(obj.c[0]).to.be.an("array"); | ||
expect(obj).to.have.deep.property("c.0.1.m", "l"); | ||
objectPath.set(obj, 'c.0.1.m', 'l'); | ||
expect(obj.c[0]).to.be.an('array'); | ||
expect(obj).to.have.deep.property('c.0.1.m', 'l'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["c","0",1,"m"], "l"); | ||
expect(obj.c[0]).to.be.an("array"); | ||
expect(obj).to.have.deep.property("c.0.1.m", "l"); | ||
objectPath.set(obj, ['c','0',1,'m'], 'l'); | ||
expect(obj.c[0]).to.be.an('array'); | ||
expect(obj).to.have.deep.property('c.0.1.m', 'l'); | ||
}); | ||
@@ -171,7 +171,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.set(obj, "1a", "foo"); | ||
expect(obj).to.have.deep.property("1a", "foo"); | ||
objectPath.set(obj, '1a', 'foo'); | ||
expect(obj).to.have.deep.property('1a', 'foo'); | ||
obj = getTestObj(); | ||
objectPath.set(obj, ["1a"], "foo"); | ||
expect(obj).to.have.deep.property("1a", "foo"); | ||
objectPath.set(obj, ['1a'], 'foo'); | ||
expect(obj).to.have.deep.property('1a', 'foo'); | ||
}); | ||
@@ -181,7 +181,7 @@ | ||
var obj = []; | ||
objectPath.set(obj, [0], "foo"); | ||
expect(obj[0]).to.be.equal("foo"); | ||
objectPath.set(obj, [0], 'foo'); | ||
expect(obj[0]).to.be.equal('foo'); | ||
obj = []; | ||
objectPath.set(obj, "0", "foo"); | ||
expect(obj[0]).to.be.equal("foo"); | ||
objectPath.set(obj, '0', 'foo'); | ||
expect(obj[0]).to.be.equal('foo'); | ||
}); | ||
@@ -194,7 +194,7 @@ }); | ||
var obj = getTestObj(); | ||
objectPath.push(obj, "b.c", "l"); | ||
expect(obj).to.have.deep.property("b.c.0", "l"); | ||
objectPath.push(obj, 'b.c', 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
obj = getTestObj(); | ||
objectPath.push(obj, ["b","c"], "l"); | ||
expect(obj).to.have.deep.property("b.c.0", "l"); | ||
objectPath.push(obj, ['b','c'], 'l'); | ||
expect(obj).to.have.deep.property('b.c.0', 'l'); | ||
}); | ||
@@ -204,7 +204,7 @@ | ||
var obj = getTestObj(); | ||
objectPath.push(obj, "b.h", "l"); | ||
expect(obj).to.have.deep.property("b.h.0", "l"); | ||
objectPath.push(obj, 'b.h', 'l'); | ||
expect(obj).to.have.deep.property('b.h.0', 'l'); | ||
obj = getTestObj(); | ||
objectPath.push(obj, ["b","h"], "l"); | ||
expect(obj).to.have.deep.property("b.h.0", "l"); | ||
objectPath.push(obj, ['b','h'], 'l'); | ||
expect(obj).to.have.deep.property('b.h.0', 'l'); | ||
}); | ||
@@ -214,4 +214,4 @@ | ||
var obj = getTestObj(); | ||
objectPath.push(obj.b.e, 0, "l"); | ||
expect(obj).to.have.deep.property("b.e.0.0", "l"); | ||
objectPath.push(obj.b.e, 0, 'l'); | ||
expect(obj).to.have.deep.property('b.e.0.0', 'l'); | ||
}); | ||
@@ -225,8 +225,8 @@ | ||
var obj = getTestObj(); | ||
var oldVal = objectPath.ensureExists(obj, "b.g.1.l", "test"); | ||
var oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test'); | ||
expect(oldVal).to.not.exist; | ||
expect(obj).to.have.deep.property("b.g.1.l", "test"); | ||
oldVal = objectPath.ensureExists(obj, "b.g.1.l", "test1"); | ||
expect(oldVal).to.be.equal("test"); | ||
expect(obj).to.have.deep.property("b.g.1.l", "test"); | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
oldVal = objectPath.ensureExists(obj, 'b.g.1.l', 'test1'); | ||
expect(oldVal).to.be.equal('test'); | ||
expect(obj).to.have.deep.property('b.g.1.l', 'test'); | ||
}); | ||
@@ -237,3 +237,3 @@ | ||
var obj = getTestObj(); | ||
expect(objectPath.ensureExists(obj, [], "test")).to.have.property('a', 'b'); | ||
expect(objectPath.ensureExists(obj, [], 'test')).to.have.property('a', 'b'); | ||
}); | ||
@@ -380,23 +380,23 @@ }); | ||
objectPath.set(obj, 'b.g.1.0', "test"); | ||
objectPath.set(obj, 'b.g.1.1', "test"); | ||
objectPath.set(obj, 'b.h.az', "test"); | ||
objectPath.set(obj, 'b.g.1.0', 'test'); | ||
objectPath.set(obj, 'b.g.1.1', 'test'); | ||
objectPath.set(obj, 'b.h.az', 'test'); | ||
expect(obj).to.have.deep.property("b.g.1.0","test"); | ||
expect(obj).to.have.deep.property("b.g.1.1","test"); | ||
expect(obj).to.have.deep.property("b.h.az","test"); | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
expect(obj).to.have.deep.property('b.g.1.1','test'); | ||
expect(obj).to.have.deep.property('b.h.az','test'); | ||
objectPath.del(obj, 'b.h.az'); | ||
expect(obj).to.not.have.deep.property("b.h.az"); | ||
expect(obj).to.have.deep.property("b.h"); | ||
expect(obj).to.not.have.deep.property('b.h.az'); | ||
expect(obj).to.have.deep.property('b.h'); | ||
objectPath.del(obj, 'b.g.1.1'); | ||
expect(obj).to.not.have.deep.property("b.g.1.1"); | ||
expect(obj).to.have.deep.property("b.g.1.0","test"); | ||
expect(obj).to.not.have.deep.property('b.g.1.1'); | ||
expect(obj).to.have.deep.property('b.g.1.0','test'); | ||
objectPath.del(obj, ['b','g','1','0']); | ||
expect(obj).to.not.have.deep.property("b.g.1.0"); | ||
expect(obj).to.have.deep.property("b.g.1"); | ||
expect(obj).to.not.have.deep.property('b.g.1.0'); | ||
expect(obj).to.have.deep.property('b.g.1'); | ||
expect(objectPath.del(obj, ['b'])).to.not.have.deep.property("b.g"); | ||
expect(objectPath.del(obj, ['b'])).to.not.have.deep.property('b.g'); | ||
expect(obj).to.be.deep.equal({'a':'b'}); | ||
@@ -423,2 +423,45 @@ }); | ||
}); | ||
}); | ||
describe('insert', function(){ | ||
it('should insert value into existing array', function(){ | ||
var obj = getTestObj(); | ||
objectPath.insert(obj, 'b.c', 'asdf'); | ||
expect(obj).to.have.deep.property('b.c.0', 'asdf'); | ||
expect(obj).to.not.have.deep.property('b.c.1'); | ||
}); | ||
it('should create intermediary array', function(){ | ||
var obj = getTestObj(); | ||
objectPath.insert(obj, 'b.c.0', 'asdf'); | ||
expect(obj).to.have.deep.property('b.c.0.0', 'asdf'); | ||
}); | ||
it('should insert in another index', function(){ | ||
var obj = getTestObj(); | ||
objectPath.insert(obj, 'b.d', 'asdf', 1); | ||
expect(obj).to.have.deep.property('b.d.1', 'asdf'); | ||
expect(obj).to.have.deep.property('b.d.0', 'a'); | ||
expect(obj).to.have.deep.property('b.d.2', 'b'); | ||
}); | ||
it('should handle sparse array', function(){ | ||
var obj = getTestObj(); | ||
obj.b.d = new Array(4); | ||
obj.b.d[0] = 'a'; | ||
obj.b.d[1] = 'b'; | ||
objectPath.insert(obj, 'b.d', 'asdf', 3); | ||
expect(obj.b.d).to.have.members([ | ||
'a', | ||
'b', | ||
, | ||
, | ||
'asdf' | ||
]); | ||
}); | ||
}); |
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
23790
582
93