Comparing version 1.1.0 to 1.2.1
33
index.js
@@ -5,5 +5,5 @@ function isObject (o) { | ||
function get (obj, path) { | ||
function get (obj, path, dft) { | ||
for(var i = 0; i < path.length; i++) { | ||
if(null == (obj = obj[path[i]])) return obj | ||
if(null == (obj = obj[path[i]])) return dft | ||
} | ||
@@ -14,3 +14,3 @@ return obj | ||
function set (obj, path, value) { | ||
if(!obj) throw new Error('must be a an object') | ||
if(!obj) throw new Error('libnested.set: first arg must be an object') | ||
for(var i = 0; i < path.length; i++) | ||
@@ -26,9 +26,9 @@ if(i === path.length - 1) | ||
function each (obj, iter, _a) { | ||
_a = _a || [] | ||
function each (obj, iter, path) { | ||
path = path || [] | ||
for(var k in obj) { | ||
if(isObject(obj[k])) { | ||
if(false === each(obj[k], iter, _a.concat(k))) return false | ||
if(false === each(obj[k], iter, path.concat(k))) return false | ||
} else { | ||
if(false === iter(obj[k], _a.concat(k))) return false | ||
if(false === iter(obj[k], path.concat(k))) return false | ||
} | ||
@@ -39,16 +39,16 @@ } | ||
function map (obj, iter) { | ||
var o = {} | ||
each(obj, function (v, path) { | ||
set(o, path, iter(v, path)) | ||
function map (obj, iter, out) { | ||
var out = out || {} | ||
each(obj, function (val, path) { | ||
set(out, path, iter(val, path)) | ||
}) | ||
return o | ||
return out | ||
} | ||
function paths (obj) { | ||
var p = [] | ||
var out = [] | ||
each(obj, function (_, path) { | ||
p.push(path) | ||
out.push(path) | ||
}) | ||
return p | ||
return out | ||
} | ||
@@ -61,4 +61,1 @@ | ||
exports.paths = paths | ||
{ | ||
"name": "libnested", | ||
"description": "", | ||
"version": "1.1.0", | ||
"version": "1.2.1", | ||
"homepage": "https://github.com/dominictarr/libnested", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -15,5 +15,5 @@ # libnested | ||
### map (object, iter(value, path) => _value) => _object | ||
### map (object, iter(value, path) => nextValue, output) => output | ||
map over a nested object (depth first). A new object is returned, containing values returned by `iter`. | ||
map over a nested object (depth first). A new object is returned (unless `output` is given), containing values returned by `iter`. | ||
@@ -24,6 +24,6 @@ ### paths (object) => [path...] | ||
### get(object, path) => value | ||
### get(object, path, default) => value | ||
get the `value` at `path` within object. | ||
if `path` does not exist in the `object`, return null. | ||
if `path` does not exist in the `object`, return `default`. | ||
@@ -30,0 +30,0 @@ ### set(object, path, value) |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
4664
1