json-p3
Version:
JSONPath, JSON Pointer and JSON Patch
107 lines (106 loc) • 3.37 kB
TypeScript
import { JSONValue } from "../types";
/**
* The symbol indicating the absence of a JSON value.
*/
export declare const UNDEFINED: unique symbol;
export type MaybeJSONValue = JSONValue | typeof UNDEFINED;
/**
* Identify a single value in JSON-like data, as per RFC 6901.
*/
export declare class JSONPointer {
#private;
tokens: string[];
/**
* @param pointer - A string representation of a JSON Pointer.
*/
constructor(pointer: string);
static encode(tokens: string[]): string;
/**
* Resolve this pointer against JSON-like data _value_.
*
* @param value - The target JSON-like value, possibly loaded using
* `JSON.parse()`.
* @param fallback - A default value to return if _value_ has no
* path matching `pointer`.
* @returns The value identified by _pointer_ or, if given, the fallback
* value in the even of a `JSONPointerResolutionError`.
*
* @throws {@link JSONPointerResolutionError}
* If the value pointed to by _pointer_ does not exist in _value_, and
* no fallback value is given.
*/
resolve(value: JSONValue, fallback?: MaybeJSONValue): JSONValue;
/**
*
* @param value -
* @returns
*/
resolveWithParent(value: JSONValue): [MaybeJSONValue, MaybeJSONValue];
/**
*
* @returns
*/
toString(): string;
/**
* Return _true_ if this pointer points to a child of _pointer_.
*/
isRelativeTo(pointer: JSONPointer): boolean;
protected parse(pointer: string): string[];
protected getItem(val: JSONValue, token: string, idx: number): JSONValue;
private _join;
/**
* Join this pointer with _tokens_.
*
* @param tokens - JSON Pointer strings, possibly without leading slashes.
* If a token or "part" does have a leading slash, the previous pointer is
* ignored and a new `JSONPointer` is created, then processing of the
* remaining tokens continues.
*
* @returns A new JSON Pointer that is the concatenation of all tokens or
* "parts".
*/
join(...tokens: string[]): JSONPointer;
/**
* Return _true_ if this pointer can be resolved against _value_.
*
* Note that `JSONPointer.resolve()` can return legitimate falsy values
* that form part of the target JSON document. This method will return
* `true` if a falsy value is found.
*/
exists(value: JSONValue): boolean;
/**
* Return this pointer's parent as a new `JSONPointer`.
*
* If this pointer points to the document root, _this_ is returned.
*/
parent(): JSONPointer;
to(rel: string | RelativeJSONPointer): JSONPointer;
}
/**
* A relative JSON Pointer.
*
* See https://datatracker.ietf.org/doc/html/draft-hha-relative-json-pointer
*/
export declare class RelativeJSONPointer {
readonly origin: number;
readonly index: number;
readonly pointer: string | JSONPointer;
/**
*
* @param rel -
*/
constructor(rel: string);
/**
*
* @returns
*/
toString(): string;
/**
*
* @param pointer -
*/
to(pointer: string | JSONPointer): JSONPointer;
protected parse(rel: string): [number, number, string | JSONPointer];
protected parseInt(s: string): number;
protected isIntLike(value: string | number | undefined): boolean;
}