object-path
Advanced tools
Comparing version 0.3.0 to 0.4.0
12
index.js
@@ -133,2 +133,14 @@ (function (root, factory){ | ||
objectPath.coalesce = function (obj, paths, defaultValue) { | ||
var value; | ||
for (var i = 0, len = paths.length; i < len; i++) { | ||
if ((value = objectPath.get(obj, paths[i])) !== void 0) { | ||
return value; | ||
} | ||
} | ||
return defaultValue; | ||
}; | ||
objectPath.get = function (obj, path, defaultValue){ | ||
@@ -135,0 +147,0 @@ if (isEmpty(path)) { |
{ | ||
"name": "object-path", | ||
"description": "Access deep properties using a path", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Mario Casciaro" |
@@ -46,7 +46,14 @@ | ||
//get the first non-undefined value | ||
objectPath.coalesce(obj, ['a.z', 'a.d', ['a','b']], 'default'); | ||
//works also with arrays | ||
objectPath.get(obj, "a.c.1"); //returns "f" | ||
objectPath.get(obj, ["a","c","1"]); //returns "f" | ||
//can return a default value with get | ||
objectPath.get(obj, ["a.c.b"], "DEFAULT"); //returns "DEFAULT", since a.c.b path doesn't exists, if omitted, returns undefined | ||
//set | ||
objectPath.set(obj, "a.h", "m"); | ||
objectPath.set(obj, "a.h", "m"); // or objectPath.set(obj, ["a","h"], "m"); | ||
objectPath.get(obj, "a.h"); //returns "m" | ||
@@ -60,6 +67,9 @@ | ||
//Ensure a path exists (if it doesn't, set the default value you provide) | ||
//ensure a path exists (if it doesn't, set the default value you provide) | ||
objectPath.ensureExists(obj, "a.k.1", "DEFAULT"); | ||
//deletes a path | ||
objectPath.del(obj, "a.b"); // obj.a.b is now undefined | ||
objectPath.del(obj, ["a","c",0]); // obj.a.c is now ['f'] | ||
``` |
53
test.js
@@ -202,2 +202,55 @@ var expect = require('chai').expect, | ||
describe('coalesce', function(){ | ||
it('should return the first non-undefined value', function(){ | ||
var obj = { | ||
should: {have: 'prop'} | ||
}; | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
['might','not','exist'], | ||
'should.have' | ||
])).to.equal('prop'); | ||
}); | ||
it('should work with falsy values (null, 0, \'\', false)', function(){ | ||
var obj = { | ||
is: { | ||
false: false, | ||
null: null, | ||
empty: '', | ||
zero: 0 | ||
} | ||
}; | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
'is.zero' | ||
])).to.equal(0); | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
'is.false' | ||
])).to.equal(false); | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
'is.null' | ||
])).to.equal(null); | ||
expect(objectPath.coalesce(obj, [ | ||
'doesnt.exist', | ||
'is.empty' | ||
])).to.equal(''); | ||
}); | ||
it('returns defaultValue if no paths found', function(){ | ||
var obj = { | ||
doesnt: 'matter' | ||
}; | ||
expect(objectPath.coalesce(obj, ['some.inexistant','path',['on','object']], 'false')).to.equal('false'); | ||
}); | ||
}); | ||
describe('del', function(){ | ||
@@ -204,0 +257,0 @@ it('should return undefined on empty 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
17872
414
73