Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trs/accessor
Deeply access the path of a Javascript object using a JSONPath-like syntax with support for sub-selects.
https://github.com/trs/accessor
Last synced: 10 days ago
JSON representation
Deeply access the path of a Javascript object using a JSONPath-like syntax with support for sub-selects.
- Host: GitHub
- URL: https://github.com/trs/accessor
- Owner: trs
- Created: 2022-09-27T23:49:46.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-03T03:55:02.000Z (about 2 years ago)
- Last Synced: 2023-02-28T03:01:32.638Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 98.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `@trs/accessor`
[![npm](https://img.shields.io/npm/v/@trs/accessor)](https://www.npmjs.com/package/@trs/accessor)
[![github](https://img.shields.io/badge/github-trs/accessor-blue)](https://github.com/trs/accessor)Deeply access the path of a Javascript object using a JSONPath-like syntax with support for sub-selects.
## Getting Started
```ts
import {access} from '@trs/accessor';const value = {
a: {
b: [1, 2],
},
};const path = 'a.b[1]';
const result = access(path, value);
assert(result === 2);
```## Usage
Paths can be resolved using sub-paths surrounded by curly braces (`{}`). These are resolved outward.
```ts
const value = {
a: {
a1: [3, 4]
},
b: {
b2: 'c'
}
c: 1,
}const path = 'a.a1[{{b.b2}}]';
const result = access(path, value);
assert(result === 4);
```Here we resolve `b.b2` first, giving us `'c'`. As this is also wrapped in curly braces, we resolve `c` giving us `1`. We then resolve `a.a1[1]` giving us `4`.