UNPKG

@stryke/json

Version:

A package containing JSON parsing/stringify utilities used by Storm Software.

52 lines 2.03 kB
//#region src/pointer/find-reference.d.ts interface Reference { /** Target value where pointer is pointing. */ readonly val: unknown; /** Object which contains the target value. */ readonly obj?: unknown | object | unknown[]; /** Key which targets the target value in the object. */ readonly key?: string | number; } /** * Finds a target in document specified by JSON Pointer. Also returns the * object containing the target and key used to reference that object. * * Throws Error('NOT_FOUND') if pointer does not result into a value in the middle * of the path. If the last element of the path does not result into a value, the * lookup succeeds with `val` set to `undefined`. It can be used to discriminate * missing values, because `undefined` is not a valid JSON value. * * If last element in array is targeted using "-", e.g. "/arr/-", use * `isArrayEnd` to verify that: * * ```js * const ref = find({arr: [1, 2, 3], ['arr', '-']}); * if (isArrayReference(ref)) { * if (isArrayEnd(ref)) { * // ... * } * } * ``` * * @param val - Document to search in. * @param path - JSON Pointer path. * @returns Reference to the target. */ declare const find: (val: unknown, path: Reference["key"][]) => Reference; interface ArrayReference<T = unknown> { /** `undefined` in case JSON Pointer points to last element, e.g. "/foo/-". */ readonly val: undefined | T; readonly obj: T[]; readonly key: number; } declare const isArrayReference: <T = unknown>(ref: Reference) => ref is ArrayReference<T>; declare const isArrayEnd: (ref: ArrayReference) => boolean; interface ObjectReference<T = unknown> { readonly val: T; readonly obj: Record<string, T>; readonly key: string; } declare const isObjectReference: <T = unknown>(ref: Reference) => ref is ObjectReference<T>; //#endregion export { isArrayEnd as a, find as i, ObjectReference as n, isArrayReference as o, Reference as r, isObjectReference as s, ArrayReference as t }; //# sourceMappingURL=find-reference-Tfj7Es3n.d.mts.map