nested-property
Advanced tools
Comparing version 0.0.4 to 0.0.5
40
index.js
@@ -18,3 +18,4 @@ /** | ||
return this.has(object, property, options || {own: true}); | ||
} | ||
}, | ||
isIn: isInNestedProperty | ||
}; | ||
@@ -111,1 +112,38 @@ | ||
} | ||
/** | ||
* Tell if an object is on the path to a nested property | ||
* If the object is on the path, and the path exists, it returns true, and false otherwise. | ||
* @param {Object} object to get the nested property from | ||
* @param {String} property name of the nested property | ||
* @param {Object} objectInPath the object to check | ||
* @param {Object} options: | ||
* - validPath: return false if the path is invalid, even if the object is in the path | ||
* @returns {boolean} true if the object is on the path | ||
*/ | ||
function isInNestedProperty(object, property, objectInPath, options) { | ||
options = options || {}; | ||
if (object && typeof object == "object") { | ||
if (typeof property == "string" && property !== "") { | ||
var split = property.split("."), | ||
isIn = false, | ||
pathExists; | ||
pathExists = !!split.reduce(function (obj, prop) { | ||
isIn = isIn || obj === objectInPath || obj[prop] === objectInPath; | ||
return obj && obj[prop]; | ||
}, object); | ||
if (options.validPath) { | ||
return isIn && pathExists; | ||
} else { | ||
return isIn; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} |
{ | ||
"name": "nested-property", | ||
"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", | ||
"version": "0.0.5", | ||
"homepage": "https://github.com/cosmosio/nested-property", | ||
@@ -6,0 +6,0 @@ "licenses": [ |
@@ -107,3 +107,3 @@ Nested property | ||
__Finally, you can also test if a data structure has a nested property:__ | ||
__You can also test if a data structure has a nested property:__ | ||
@@ -123,2 +123,4 @@ ```js | ||
The example shows that it works through array, but of course, plain objects are fine too. | ||
If it must be a "own" property (i.e. not in the prototype chain) you can use the own option: | ||
@@ -139,20 +141,60 @@ | ||
```js | ||
function DataStructure() {} | ||
DataStructure.prototype.prop = true; | ||
var obj = Object.create({prop: true}); | ||
var obj = new DataStructure(); | ||
nestedProperty.hasOwn(obj, "prop"); // false | ||
``` | ||
___And finally, you can test if an object is on the path to a nested property:___ | ||
```js | ||
var obj = { | ||
nested: [ | ||
{ | ||
property: true | ||
} | ||
] | ||
}; | ||
nestedProperty.isIn(obj, "nested.0.property", obj); // true | ||
nestedProperty.isIn(obj, "nested.0.property", obj.nested); // true | ||
nestedProperty.isIn(obj, "nested.0.property", obj.nested[0]); // true | ||
nestedProperty.isIn(obj, "nested.0.property", {}); // false | ||
``` | ||
The path doesn't have to be valid to return true: | ||
```js | ||
nestedProperty.isIn(obj, "nested.0.property.foo.bar.path", obj.nested[0]); // true | ||
``` | ||
Unless the `validPath` option is set to `true`: | ||
```js | ||
nestedProperty.isIn(obj, "nested.0.property.foo.bar.path", obj.nested[0], { validPath: true }); // false | ||
``` | ||
Note that if instead of an object you give it the value of the nested property, it'll return true: | ||
```js | ||
nestedProperty.isIn(obj, "nested.0.property", obj.nested[0].property); // true | ||
nestedProperty.isIn(obj, "nested.0.property", true); // true | ||
``` | ||
CHANGELOG | ||
========= | ||
### 0.0.3 - 14 JAN 2015 | ||
### 0.0.5 - 19 JAN 2015 | ||
* Add hasNestedProperty with tests and documentation | ||
* Add isIn, to tell if an object is on the path to a nested property. | ||
### 0.0.4 - 15 JAN 2015 | ||
* Add {own: true} option to .has to ensure that a nested property isn't coming from the prototype chain | ||
* Add hasOwn, that calls .has with the {own: true} option | ||
### 0.0.3 - 14 JAN 2015 | ||
* Add has with tests and documentation | ||
LICENSE | ||
@@ -159,0 +201,0 @@ ======= |
11326
137
201