get-nested-value
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "get-nested-value", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Utility package to get deeply nested values from objects. Support arrays.", | ||
@@ -5,0 +5,0 @@ "main": "readme.js", |
/** | ||
* @fileoverview | ||
* | ||
* This package safely returns the value at a requested path inside of | ||
* an object. Best used on unpredictable deeply nested structures. Supports | ||
* arrays as well. | ||
* arrays as well. See README.md for more info. | ||
* | ||
* Install: | ||
* `npm install get-nested-value --save` | ||
* # or | ||
* `yarn add get-nested-value` | ||
* | ||
* | ||
* Example: | ||
* import getNestedValue from 'get-nestedValue'; | ||
* | ||
* getNestedValue('some.path.to.key', object); | ||
* // or | ||
* getNestedValue(['some', 'path', 'to', 'key'], object); | ||
* // dealing with arrays? | ||
* getNestedValue(['some', 'path', 'atIndex', 0, 'key'], object); | ||
* | ||
* Notes: | ||
* - Returns undefined if path does not exist. | ||
* - Throws an Error if path is not a string or array. | ||
*/ | ||
/** | ||
* @param {array|string} path – e.g ['key1', 0, 'key2'] or a string such as 'key1.0.key2 | ||
* @param {object} object | ||
* @param {Array.<string|number>|String} path – e.g ['key1', 0, 'key2'] or a string such as 'key1.0.key2 | ||
* @param {Object|Array} object | ||
* @returns {*} | ||
@@ -36,3 +16,3 @@ */ | ||
if (!Array.isArray(path) && typeof path !== 'string') { | ||
throw Error('The package get-nested-value received a non-contract path. Please provide a string or an array!'); | ||
throw Error('The package get-nested-value received a non-contract path. Please provide a string, number, or an array of those!'); | ||
} | ||
@@ -39,0 +19,0 @@ var searchPath = path; |
20
test.js
@@ -59,5 +59,23 @@ const test = require('tape'); | ||
} catch (e) { | ||
t.equal(e.message, 'The package get-nested-value received a non-contract path. Please provide a string or an array!') | ||
t.equal(e.message, 'The package get-nested-value received a non-contract path. Please provide a string, number, or an array of those!') | ||
} | ||
t.end(); | ||
}); | ||
test('README example works', (t) => { | ||
t.plan(1); | ||
t.equal( | ||
getNestedValue('a.b.0.c.d', { a: { b: [{ c: { d: 'hello!' } }] } }), | ||
'hello!' | ||
); | ||
t.end(); | ||
}); | ||
test('works with arrays of arrays', (t) => { | ||
t.plan(1); | ||
t.equal( | ||
getNestedValue([0, 1, 2, 'key'], [ [ [], [{}, {}, { key: 'hello!' }] ] ]), | ||
'hello!' | ||
); | ||
t.end(); | ||
}); |
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
7
12745
102
51