nested-property
Advanced tools
+15
-3
@@ -15,3 +15,6 @@ /** | ||
| get: getNestedProperty, | ||
| has: hasNestedProperty | ||
| has: hasNestedProperty, | ||
| hasOwn: function (object, property, options) { | ||
| return this.has(object, property, options || {own: true}); | ||
| } | ||
| }; | ||
@@ -46,7 +49,12 @@ | ||
| * given an object such as a.b.c.d = 5, hasNestedProperty(a, "b.c.d") will return true. | ||
| * It also returns true if the property is in the prototype chain. | ||
| * @param {Object} object the object to get the property from | ||
| * @param {String} property the path to the property as a string | ||
| * @param {Object} options: | ||
| * - own: set to reject properties from the prototype | ||
| * @returns true if has (property in object), false otherwise | ||
| */ | ||
| function hasNestedProperty(object, property) { | ||
| function hasNestedProperty(object, property, options) { | ||
| options = options || {}; | ||
| if (object && typeof object == "object") { | ||
@@ -57,3 +65,7 @@ if (typeof property == "string" && property !== "") { | ||
| if (idx == array.length - 1) { | ||
| return !!(obj && prop in obj); | ||
| if (options.own) { | ||
| return !!(obj && obj.hasOwnProperty(prop)); | ||
| } else { | ||
| return !!(obj !== null && typeof obj == "object" && prop in obj); | ||
| } | ||
| } | ||
@@ -60,0 +72,0 @@ return obj && obj[prop]; |
+4
-3
| { | ||
| "name": "nested-property", | ||
| "description": "Read or write an array or object's nested property via a string like 'my.nested.property'", | ||
| "version": "0.0.3", | ||
| "description": "Read, write or test a data structure's nested property via a string like 'my.nested.property'. It works through arrays and objects.'", | ||
| "version": "0.0.4", | ||
| "homepage": "https://github.com/cosmosio/nested-property", | ||
@@ -40,4 +40,5 @@ "licenses": [ | ||
| "chai": "~1.9.1", | ||
| "mocha": "~1.18.2" | ||
| "mocha": "~1.18.2", | ||
| "sinon": "~1.12.2" | ||
| } | ||
| } |
+24
-0
@@ -122,4 +122,28 @@ Nested property | ||
| If it must be a "own" property (i.e. not in the prototype chain) you can use the own option: | ||
| ```js | ||
| function DataStructure() {} | ||
| DataStructure.prototype.prop = true; | ||
| var obj = new DataStructure(); | ||
| nestedProperty.has(obj, "prop", { own: true}); // false | ||
| nestedProperty.has(obj, "prop"); // true | ||
| ``` | ||
| Alternatively, you can use the hasOwn function: | ||
| ```js | ||
| function DataStructure() {} | ||
| DataStructure.prototype.prop = true; | ||
| var obj = new DataStructure(); | ||
| nestedProperty.hasOwn(obj, "prop"); // false | ||
| ``` | ||
| CHANGELOG | ||
@@ -126,0 +150,0 @@ ========= |
8750
14.02%103
11.96%159
17.78%3
50%