@crikey/json-pointer
v0.0.2
Published
Functions for handling JSON pointers [rfc6901](https://www.rfc-editor.org/rfc/rfc6901.html) and [relative-json-pointer](https://datatracker.ietf.org/doc/html/draft-luff-relative-json-pointer-00)
Downloads
5
Readme
@crikey/json-pointer
Functions for handling JSON pointers rfc6901 and relative-json-pointer
See @crikey/json-pointer for full documentation.
API
Types
Contains types used to represent JSON pointers and segments
Guards
Contains typescript guards for identifying type information.
is_pointer
Returns true if value is aPointer
is_absolute
Returns true if value is anAbsolutePointer
is_relative
Returns true if value is aRelativePointer
is_relative_pure
Returns true if value is aRelativeOnlyPointer
orRelativePurePointer
is_relative_iref
Returns true if value is aRelativeIRefPointer
is_relative_only
Returns true if value is aRelativeOnlyPointer
Error classes
Classes used when throwing errors
pointer-decoding-error
Error class thrown for pointer decoding errorspointer-encoding-error
Error class thrown for pointer encoding errors
Creation
Functions used for creating paths from their constituent parts
pointer
Create aPointer
from an optionalrelative
number and a series of decoded segmentsabsolute
Create anAbsolutePointer
from a series of decoded segmentsrelative
Create aRelativePointer
from arelative
number and a series of decoded segmentsrelative_iref
Create aRelativeIRefPointer
from arelative
numberpointer_encoded
Create aPointer
from an optionalrelative
number and a series of encoded segmentsabsolute_encoded
Create anAbsolutePointer
from a series of encoded segmentsrelative_encoded
Create aRelativePointer
from arelative
number and a series of encoded segments
Joining
Combining pointer components and segments
join_pointer
Join twoPointer
s togetherjoin_segments
Append decoded segments onto an existingPointer
join_iref
Append an index reference onto aRelativeOnlyPointer
join_encoded_segments
Append encoded segments onto an existingPointer
Encoding
Encode and decode path segments
segment_encode
Encode a given string segmentsegment_decode
Decode a given string segment
Splitting
Split paths into their constituent components/segments
split
Split aPointer
into its constituent parts, decoding each segmentsplit_pure
Split aPurePointer
into its constituent parts, decoding each segmentsplit_absolute
Split aAbsolutePointer
into its constituent parts, decoding each segmentsplit_relative
Split aRelativePointer
into its constituent parts, decoding each segmentsplit_relative_pure
Split aRelativePurePointer
into its constituent parts, decoding each segmentsplit_relative_iref
Split aRelativeIRefPointer
into its constituent parts, decoding each segmentsplit_encoded
Split aPointer
into its constituent parts, leaving segments encodedsplit_encoded_pure
Split aPurePointer
into its constituent parts, leaving segments encodedsplit_encoded_absolute
Split aAbsolutePointer
into its constituent parts, leaving segments encodedsplit_encoded_relative
Split aRelativePointer
into its constituent parts, leaving segments encodedsplit_encoded_relative_pure
Split aRelativePurePointer
into its constituent parts, leaving segments encodedsplit_encoded_relative_iref
Split aRelativeIRefPointer
into its constituent parts, leaving segments encoded
Installation
# pnpm
$ pnpm add @crikey/json-pointer
# npm
$ npm add @crikey/json-pointer
# yarn
$ yarn add @crikey/json-pointer
Example Usage
Guards
console.log(is_pointer('xyz')); // false
console.log(is_pointer('/xyz')); // true
console.log(is_pointer('123/xyz')); // true
console.log(is_absolute('123/xyz')); // false
console.log(is_relative('123/xyz')); // true
Creating
console.log(pointer('x','y','z')); // '/x/y/z'
console.log(pointer('~/')); // '/~0~1'
console.log(pointer(undefined,'x','y','z')); // '/x/y/z'
console.log(pointer(123,'x','y','z')); // '123/x/y/z'
console.log(relative(123,'x','y','z')); // '123/x/y/z'
Joining
console.log(join_pointer('/a/b/c', '/x/y/z')); // '/x/y/z'
console.log(join_pointer('/a/b/c', '1/x/y/z')); // '/a/b/x/y/z'
console.log(join_segments('/a/b/c', 'x', 'y', 'z')); // '/a/b/c/x/y/z'
console.log(join_segments('/a/b/c', '~')); // '/a/b/c/~0'
Splitting
console.log(split('/a/b/c')); // { segments: ['a', 'b', 'c'] }
console.log(split('123/a/b/c')); // { relative: 123, segments: ['a', 'b', 'c'] }
console.log(split('123#')); // { relative: 123, iref: true }