deep-property
Advanced tools
Comparing version 1.0.0 to 1.1.0
37
index.js
@@ -69,2 +69,39 @@ | ||
/* | ||
* Removes a deeply nested property. | ||
* | ||
* If an intermediate property of the specified path doesn't exist, | ||
* we bail early. | ||
* | ||
* Arguments: | ||
* | ||
* obj Object to remove property from. | ||
* | ||
* path [string] Dot-separated path specifier to nested | ||
* property to remove. Array indexes are not supported. | ||
* Example: "contact.name.first". | ||
* | ||
* Returns: [boolean] TRUE if property was removed, otherwise FALSE. | ||
*/ | ||
module.exports.remove = function (obj, path) { | ||
if (typeof obj === 'undefined' || typeof path === 'undefined') { | ||
return false; | ||
} | ||
var tokens = parse(path); | ||
for (var i = 0, len = tokens.length; i < len; i++) { | ||
if (! obj || ! obj.hasOwnProperty(tokens[i])) { | ||
return false; | ||
} | ||
if (i == (len - 1)) { | ||
delete obj[tokens[i]]; | ||
return true; | ||
} else { | ||
obj = obj[tokens[i]]; | ||
} | ||
} | ||
return false | ||
}; | ||
/* | ||
* Detects whether a given object has a specified nested property. | ||
@@ -71,0 +108,0 @@ * |
{ | ||
"name": "deep-property", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Fetch, set, and test deeply nested object properties", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# deep-property | ||
Enables deep property manipulation and inspection without worring about | ||
Enables deep property manipulation and inspection without worrying about | ||
exceptions. | ||
@@ -23,11 +23,13 @@ | ||
props.get(sample, 'name.first'); // John | ||
props.get(sample, 'name.middle'); // C | ||
props.get(sample, 'name.last'); // Reilly | ||
props.get(sample, 'job.title'); // Actor | ||
props.get(sample, 'name.first'); // John | ||
props.get(sample, 'name.middle'); // C | ||
props.get(sample, 'name.last'); // Reilly | ||
props.get(sample, 'job.title'); // Actor | ||
props.has(sample, 'name.first'); // True | ||
props.has(sample, 'name.title'); // False | ||
props.has(sample, 'job.title'); // True | ||
props.has(sample, 'job.salary'); // False | ||
props.remove(sample, 'name.middle'); // True | ||
props.has(sample, 'name.first'); // True | ||
props.has(sample, 'name.title'); // False | ||
props.has(sample, 'job.title'); // True | ||
props.has(sample, 'job.salary'); // False | ||
``` | ||
@@ -40,3 +42,2 @@ | ||
first: 'John', | ||
middle: 'C', | ||
last: 'Reilly' | ||
@@ -64,3 +65,4 @@ }, | ||
- `has`: `false` | ||
- `remove`: `false` | ||
- Paths with blank sections (`path.to..nothing` or `path.to.nothing.`) | ||
will be considered invalid. |
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
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
12001
10
302
66