Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-nested-value

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-nested-value - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

.npmignore

2

package.json
{
"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;

@@ -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();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc