Comparing version 0.0.1 to 0.0.2
@@ -33,2 +33,8 @@ var dotty = require("./lib/index"); | ||
console.log(dotty.remove(object, "a.b.x")); | ||
console.log(dotty.remove(object, "a.b.y")); | ||
console.log(dotty.deepKeys(object)); | ||
console.log(dotty.deepKeys(object).map(function(e) { return e.join("."); })); | ||
console.log(object); |
@@ -160,1 +160,75 @@ // | ||
}; | ||
// | ||
// `remove` is like `put` in reverse! | ||
// | ||
// The return value is `true` in the case that the value existed and was removed | ||
// successfully, or `false` otherwise. | ||
// | ||
var remove = module.exports.remove = function remove(object, path, value) { | ||
if (typeof path === "string") { | ||
path = path.split("."); | ||
} | ||
if (!(path instanceof Array) || path.length === 0) { | ||
return false; | ||
} | ||
path = path.slice(); | ||
var key = path.shift(); | ||
if (typeof object !== "object" || object === null) { | ||
return false; | ||
} | ||
if (path.length === 0) { | ||
if (!Object.hasOwnProperty.call(object, key)) { | ||
return false; | ||
} | ||
delete object[key]; | ||
return true; | ||
} else { | ||
return remove(object[key], path, value); | ||
} | ||
}; | ||
// | ||
// `deepKeys` creates a list of all possible key paths for a given object. | ||
// | ||
// The return value is always an array, the members of which are paths in array | ||
// format. If you want them in dot-notation format, do something like this: | ||
// | ||
// ```js | ||
// dotty.deepKeys(obj).map(function(e) { | ||
// return e.join("."); | ||
// }); | ||
// ``` | ||
// | ||
// *Note: this will probably explode on recursive objects. Be careful.* | ||
// | ||
var deepKeys = module.exports.deepKeys = function deepKeys(object, prefix) { | ||
if (typeof prefix === "undefined") { | ||
prefix = []; | ||
} | ||
var keys = []; | ||
for (var k in object) { | ||
if (!Object.hasOwnProperty.call(object, k)) { | ||
continue; | ||
} | ||
keys.push(prefix.concat([k])); | ||
if (typeof object[k] === "object" && object[k] !== null) { | ||
keys = keys.concat(deepKeys(object[k], prefix.concat([k]))); | ||
} | ||
} | ||
return keys; | ||
}; |
{ | ||
"name": "dotty", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Access properties of nested objects using dot-path notation", | ||
@@ -24,5 +24,5 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"vows": "~0.6.3", | ||
"docco": "~0.3.0" | ||
"vows": "~0.7.0", | ||
"docco": "~0.6.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
Dotty | ||
Dotty [![build status](https://secure.travis-ci.org/deoxxa/dotty.png)](http://travis-ci.org/deoxxa/dotty) | ||
===== | ||
@@ -12,2 +12,11 @@ | ||
Installation | ||
------------ | ||
Here's a link to the [npm](https://npmjs.org/package/dotty) page. | ||
npm install dotty | ||
Usage | ||
@@ -52,2 +61,8 @@ ----- | ||
console.log(dotty.remove(object, "a.b.x")); | ||
console.log(dotty.remove(object, "a.b.y")); | ||
console.log(dotty.deepKeys(object)); | ||
console.log(dotty.deepKeys(object).map(function(e) { return e.join("."); })); | ||
console.log(object); | ||
@@ -54,0 +69,0 @@ ``` |
Sorry, the diff of this file is not supported yet
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
400521
24
1346
80
0
1