nested-property
Advanced tools
Comparing version 1.0.4 to 2.0.0-beta1
{ | ||
"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": "1.0.4", | ||
"version": "2.0.0-beta1", | ||
"homepage": "https://github.com/cosmosio/nested-property", | ||
@@ -9,3 +9,3 @@ "license": "MIT", | ||
"LICENSE", | ||
"index.js" | ||
"dist/nested-property.js" | ||
], | ||
@@ -37,7 +37,16 @@ "author": "Olivier Scherrer <pode.fr@gmail.com>", | ||
"scripts": { | ||
"test": "mocha test/" | ||
"test": "mocha test/", | ||
"debug-test": "mocha debug test/", | ||
"test-build": "ENTRYPOINT=../dist/nested-property.js mocha test/", | ||
"lint": "eslint index.js test/", | ||
"fix-lint": "eslint index.js test/ --fix", | ||
"build": "babel index.js -o dist/nested-property.js" | ||
}, | ||
"main": "index.js", | ||
"main": "./dist/nested-property.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.8.4", | ||
"@babel/preset-env": "^7.8.4", | ||
"chai": "~1.9.1", | ||
"eslint": "^6.8.0", | ||
"mocha": "^6.1.4", | ||
@@ -44,0 +53,0 @@ "sinon": "~1.12.2" |
118
README.md
@@ -1,9 +0,36 @@ | ||
Nested property | ||
============= | ||
# Nested property | ||
Read, write or test a data structure's nested property via a string like 'my.nested.property'. It works through arrays and objects. | ||
Traverse a deeply nested JS data structure to get, set values, or test if values are part of the data structure. | ||
Nested property offers a simple syntax to define a path to access a value with. | ||
Installation | ||
============ | ||
For instance: | ||
``` | ||
const data = { | ||
a: { | ||
b: [ | ||
10, | ||
20 | ||
] | ||
} | ||
}; | ||
nestedProperty.get(data, "a.b.1"); // returns 20, or sdata.a.b[1] | ||
``` | ||
The syntax also supports array wildcards to access all items within an array: | ||
``` | ||
const array = [ | ||
{ ssn: "123-456-7890", name: "alice" }, | ||
{ ssn: "234-567-8901", name: "bob" }, | ||
{ ssn: "456-789-0123", name: "charlie" } | ||
] | ||
nestedProperty.set(array, "+.ssn", "<redacted>"); // sets all `ssn` values to <redacted> | ||
``` | ||
## Install | ||
```bash | ||
@@ -13,4 +40,3 @@ npm install nested-property | ||
How to use | ||
========== | ||
## Use | ||
@@ -23,2 +49,4 @@ Require nested-property: | ||
### nestedProperty.get(data, "path") | ||
__You can get a nested property from an object:__ | ||
@@ -60,2 +88,16 @@ | ||
You may also use wildcards to access multiple values: | ||
```js | ||
var array = [ | ||
{ a: 0, b: 1, c: 2 }, | ||
{ a: 10, b: 11, c: 12 }, | ||
{ a: 20, b: 21, c: 22 } | ||
] | ||
nestedProperty.get(array, "+.b"); // returns [1, 11, 21] | ||
``` | ||
### nestedProperty.set(data, "path", value) | ||
__You can set a nested property on an object:__ | ||
@@ -94,18 +136,17 @@ | ||
Caveat! | ||
You may also use wildcards to set multiple values: | ||
```js | ||
var object = {}; | ||
nestedProperty.set(object, "0.1.2", "new object"); | ||
var array = [ | ||
{ a: 0, b: 1, c: 2 }, | ||
{ a: 10, b: 11, c: 12 }, | ||
{ a: 20, b: 21, c: 22 } | ||
] | ||
// will not create arrays, but objects such as: | ||
{ | ||
"0": { | ||
"1": { | ||
"2": "new object" | ||
} | ||
} | ||
} | ||
nestedProperty.set(array, "+.b", 0); // array[0].b === 0, array[1].b === 0, array[2].b === 0 | ||
``` | ||
### nestedProperty.has(data, "path") | ||
__You can also test if a data structure has a nested property:__ | ||
@@ -128,3 +169,3 @@ | ||
If it must be a "own" property (i.e. not in the prototype chain) you can use the own option: | ||
If it must be an "own" property (i.e. not in the prototype chain) you can use the own option: | ||
@@ -149,5 +190,19 @@ ```js | ||
___And finally, you can test if an object is on the path to a nested property:___ | ||
Just like other methods, you may also use array wildcards. For instance, testing if any item in an array has a given property: | ||
```js | ||
var array = [ | ||
{ a: 0, b: 1, c: 2 }, | ||
{ a: 10, b: 11, c: 12, d: 13 }, | ||
{ a: 20, b: 21, c: 22 } | ||
] | ||
nestedProperty.has(array, "+.d"); // returns true, since array[1].d exists | ||
``` | ||
### nestedProperty.isIn(data, "path", value) | ||
__And finally, you can test if an object is on the path to a nested property:__ | ||
```js | ||
var obj = { | ||
@@ -168,3 +223,3 @@ nested: [ | ||
The path doesn't have to be valid to return true: | ||
The path doesn't have to be completely valid to return true, as long as the value exists within the valid portion. | ||
@@ -175,3 +230,3 @@ ```js | ||
Unless the `validPath` option is set to `true`: | ||
Unless the `validPath` option is set to `true`, in this case the full path needs to be valid: | ||
@@ -189,8 +244,18 @@ ```js | ||
CHANGELOG | ||
========= | ||
# CHANGELOG | ||
### 2.0.0-beta1 - 02 FEB 2020 | ||
* Add array wildcard `+` to access all properties nested within an array. For example: | ||
```js | ||
// sets all `name` property in the array to "<redacted>" | ||
nestedProperty.set(array, "+.name", "<redacted>"); | ||
``` | ||
Closes [Issue #8](https://github.com/cosmosio/nested-property/issues/8). Thanks [vemuez](https://github.com/vemuez) for the suggestion! | ||
### 1.0.4 - 18 JAN 2020 | ||
* Fix license field in package.json | ||
* Fix license field in package.json. Thanks [zr87](https://github.com/zr87) for raising the issue! | ||
@@ -236,5 +301,4 @@ ### 1.0.3 - 15 JAN 2020 | ||
LICENSE | ||
======= | ||
# LICENSE | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17312
204
295
7
1
1