@crikey/json-private-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), with an additional extension for marking path segments as "
Downloads
14
Readme
@crikey/json-private-pointer
Functions for handling JSON pointers rfc6901 and relative-json-pointer, with an additional extension for marking path segments as "public" or "private".
See @crikey/json-private-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
Error classes
Classes used when throwing errors
PointerDecodingError
Error class thrown for pointer decoding errorsPointerEncodingError
Error class thrown for pointer encoding errors
Creation
Functions used for creating paths from their constituent parts
pointer
Create aPointer
from a series of decoded segmentsabsolute
Create anAbsolutePointer
from a series of decoded segmentsrelative
Create aRelativePointer
from arelative
number and a series of decoded segmentspointer_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_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_absolute
Split aAbsolutePointer
into its constituent parts, decoding each segmentsplit_relative
Split aRelativePointer
into its constituent parts, decoding each segmentsplit_encoded
Split aPointer
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 encoded
Installation
# pnpm
$ pnpm add @crikey/json-private-pointer
# npm
$ npm add @crikey/json-private-pointer
# yarn
$ yarn add @crikey/json-private-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',[true, 'y'], [false, 'z'])); // '/x/~3y/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', [true, 'y'], 'z')); // '/a/b/c/x/~3y/z'
console.log(join_segments('/a/b/c', '~')); // '/a/b/c/~0'
Splitting
console.log(split('/a/~3b/c')); // { segments: [[false, 'a'], [true, 'b'], [false, 'c']] }
console.log(split('123/a/~3b/c')); // { relative: 123, segments: [false, 'a'], [true, 'b'], [false, 'c'] }