New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

object-path-plus

Package Overview
Dependencies
Maintainers
15
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-path-plus - npm Package Compare versions

Comparing version

to
1.2.0

CHANGELOG.md

33

dist/index.js
'use strict';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var objectPath = require('object-path');
// Our valid path validator.
var VALIDATOR = /^((("[^"]*")|('[^']*')|([\w\.]+))\s*\+\s)*(("[^"]*")|('[^']*')|([\w\.]+))$/;
var VALIDATOR_TOKEN = '(' + '("[^"]*")' + // String literal of double quotes
'|(\'[^\']*\')' + // String literal of single quotes
'|([\\w\\.\\[\\]]+)' + // String of any character, brackets or a dot
')';
var VALIDATOR = new RegExp('^' + ('(' + VALIDATOR_TOKEN + '\\s*\\+\\s)*') + // Optional token followed by ' + '
VALIDATOR_TOKEN + // Ends with a regular token
'$');
/**

@@ -29,4 +38,16 @@ * `validate` determines if the given path is valid.

var STRING_LITERAL = /^['"].+['"]$/;
var ARRAY_LOOKUP = /\[(.+)\]/;
/**
* Determines if we believe the provided value to be a dynamic array lookup, e.g. `arr[index]`
*
* @param {*} str The value to check.
* @returns {boolean} True if value is a string has a dynamic array lookup.
* false otherwise.
*/
function isArrayLookup(str) {
return ARRAY_LOOKUP.test(str);
}
/**
* Determines if we believe the provided value to be a "string literal".

@@ -77,4 +98,14 @@ *

var resolved = void 0;
if (isStringLiteral(piece)) {
resolved = trimQuotes(piece);
} else if (isArrayLookup(piece)) {
// Rewrite dynamic array lookup form `[b]` into the `.1` format supported by object-path.
var _piece$match = piece.match(ARRAY_LOOKUP),
_piece$match2 = _slicedToArray(_piece$match, 2),
arrIndex = _piece$match2[1];
var numericalIndex = objectPath.get(obj, arrIndex);
var rewrittenPiece = piece.replace(ARRAY_LOOKUP, '.' + numericalIndex);
resolved = objectPath.get(obj, rewrittenPiece);
} else {

@@ -81,0 +112,0 @@ resolved = objectPath.get(obj, piece);

3

package.json
{
"name": "object-path-plus",
"version": "1.1.1",
"version": "1.2.0",
"description": "Object path traversal with concatenation",

@@ -27,2 +27,3 @@ "main": "dist/index.js",

"build": "rm -rf dist && babel src -d dist",
"watch": "babel --watch src/ -d dist",
"lint": "eslint .",

@@ -29,0 +30,0 @@ "report": "nyc ava",

@@ -49,2 +49,13 @@ # object-path-plus

## Dynamic array lookups
You can also use a property to index within an array. Just use the `[]` syntax.
```js
resolve({
firstkey: {index: 1},
arr: [{key: 1}, {key: 2}]
}, 'arr[firstkey.index].key') === 2
```
## Validating object-path-plus paths

@@ -63,11 +74,3 @@

## Changelog
* 1.1.1 Fixed `package.json` reference to non-existant file.
* 1.1.0 Make ES5 compatible.
* 1.0.1 Fix bad `package.json` entrypoint.
* 1.0.0 Initial release
[object-path's documentation]: https://github.com/mariocasciaro/object-path#usage
[object-path]: https://github.com/mariocasciaro/object-path
[object-path]: https://github.com/mariocasciaro/object-path