https://github.com/mixmaxhq/object-path-plus
https://github.com/mixmaxhq/object-path-plus
corgi-tag
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mixmaxhq/object-path-plus
- Owner: mixmaxhq
- License: mit
- Created: 2017-09-15T01:31:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-28T05:31:49.000Z (over 1 year ago)
- Last Synced: 2025-02-22T16:26:07.559Z (3 months ago)
- Topics: corgi-tag
- Language: JavaScript
- Size: 210 KB
- Stars: 1
- Watchers: 25
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# object-path-plus
`object-path-plus` is an [object-path] compatible library that also
provides the ability to synthesize values from multiple provided paths.
Currently, it only supports the `.` delimited path format of `object-path`.## Installation
```sh
npm install object-path-plus
```or
```sh
yarn add object-path-plus
```## Usage
## `object-path` usage
To see basic object path usage, see [object-path's documentation]
## Concatenating values
Concatenation with `object-path-plus` is very simple, just add a `+` sign!
You can concatenate resolve values from multiple `object-path`s and you can
also concatenate string literals with resolved path values.```js
const { resolve } = require('object-path-plus');resolve({ a: { b: 1 }, c: { d: 2 } }, 'a.b + c.d') === 3;
resolve({ a: { b: 'foo' }, c: { d: 'bar' } }, 'a.b + c.d') === 'foobar';
resolve({ a: { b: 1 }, c: { d: 2 } }, 'a.b + " - " + c.d') === '1 - 2';
resolve({ a: { b: 'foo' }, c: { d: 'bar' } }, 'a.b + " " + c.d') === 'foo bar';
resolve({
a: { b: 'foo' },
c: { d: 'bar' },
e: { f: [ 'baz', 'whiz' ] }
}, 'a.b + " " + c.d + " " + e.f.1') === 'foo bar whiz';resolve({
a: { b: 'foo' },
c: { d: 'bar' },
e: { f: [ 0, 1 ] }
}, 'a.b + " " + c.d + " " + e.f.1') === 'foo bar 1';
```## 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
You can also test if a path is valid prior to trying to resolve a value with it.
```js
const { validate } = require('object-path-plus');validate('a.b') === true;
validate('a.b + c.d') === true;
validate('a.b + hello world') === false;
```[object-path's documentation]: https://github.com/mariocasciaro/object-path#usage
[object-path]: https://github.com/mariocasciaro/object-path