https://github.com/zacharygolba/jsonptr
Read and write values to locations referenced by JSON pointers as described in RFC 6901
https://github.com/zacharygolba/jsonptr
assign cache derference json pointers typescript
Last synced: 9 months ago
JSON representation
Read and write values to locations referenced by JSON pointers as described in RFC 6901
- Host: GitHub
- URL: https://github.com/zacharygolba/jsonptr
- Owner: zacharygolba
- License: apache-2.0
- Created: 2019-02-18T16:58:02.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-20T18:16:04.000Z (almost 7 years ago)
- Last Synced: 2025-03-13T04:48:53.137Z (10 months ago)
- Topics: assign, cache, derference, json, pointers, typescript
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# jsonptr
[](https://circleci.com/gh/zacharygolba/jsonptr/tree/master) [](https://codecov.io/gh/zacharygolba/jsonptr/branch/master) [](https://www.npmjs.com/package/@zakgolba/jsonptr)
Read and write values to locations referenced by JSON pointers as described in [RFC 6901](https://tools.ietf.org/html/rfc6901).
## Usage
### Installation
```sh
# If you're using npm
npm install @zakgolba/jsonptr --save
# If you're using yarn
yarn add @zakgolba/jsonptr
```
### API Documentation
#### assign(operand: object, pointer: string, value: any): boolean
Attempts to write a value to the location that the `pointer` references.
If the value is stored successfully, the cached pointers for the `target` provided will be invalidated.
Returns a `boolean` that represents whether or not the value was able to be written to the location that the `pointer` references.
```typescript
import jsonptr from "@zakgolba/jsonptr";
const data = { hello: {} };
jsonptr.assign(data, "/hello/world", "Hello, world!");
console.log(data.hello.world);
// => Hello, world!
console.log(jsonptr.assign(data, "/invalid/reference", "Hello, world!"));
// => false
```
#### compile(tokens: Iterable\): string
Compiles and returns a JSON pointer from an iterable of unescaped tokens.
```typescript
import jsonptr from "@zakgolba/jsonptr";
console.log(jsonptr.compile(["hello", "world"]));
// => /hello/world
console.log(jsonptr.compile(["hello/world", "pointer"]));
// => /hello~1world/pointer
```
#### deref(operand: object, pointer: string): unknown
Attempts to read and return the value at the location that the `pointer` references.
```typescript
import jsonptr from "@zakgolba/jsonptr";
const data = { hello: { world: "Hello, world!" } };
console.log(jsonptr.deref(data, "/hello/world"));
// => Hello, world!
console.log(jsonptr.deref(data, "/this/points/nowhere"));
// => undefined
```
#### parse(pointer: string): IterableIterator\
Lazily parses and unescapes each token in the provided `pointer`.
```typescript
import jsonptr from "@zakgolba/jsonptr";
console.log([...jsonptr.parse("/hello/world")]);
// => ["hello", "world"]
for (const token of jsonptr.parse("/hello~1world/pointer")) {
console.log(token);
}
// => hello/world
// => pointer
```
## License
Licensed under either of
- Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.