JSPM

@zakgolba/jsonptr

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q46942F
  • License (Apache 2.0 OR MIT)

Read and write values to locations referenced by JSON pointers as described in RFC 6901

Package Exports

  • @zakgolba/jsonptr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@zakgolba/jsonptr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jsonptr

CircleCI Codecov npm

Read and write values to locations referenced by JSON pointers as described in RFC 6901.

Usage

Installation

# 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.

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>): string

Compiles and returns a JSON pointer from an iterable of unescaped tokens.

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.

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<string>

Lazily parses and unescapes each token in the provided pointer.

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

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.