UNPKG

immutable-json-patch

Version:

Immutable JSON patch with support for reverting operations

66 lines 2.73 kB
import type { JSONPath } from './types'; /** * Shallow clone of an Object, Array, or value * Symbols are cloned too. */ export declare function shallowClone<T>(value: T): T; /** * Update a value in an object in an immutable way. * If the value is unchanged, the original object will be returned */ export declare function applyProp<T, U = unknown>(object: T, key: string | number, value: U): T; /** * helper function to get a nested property in an object or array * * @return Returns the field when found, or undefined when the path doesn't exist */ export declare function getIn<T, U = unknown>(object: U, path: JSONPath): T | undefined; /** * helper function to replace a nested property in an object with a new value * without mutating the object itself. * * @param object * @param path * @param value * @param [createPath=false] * If true, `path` will be created when (partly) missing in * the object. For correctly creating nested Arrays or * Objects, the function relies on `path` containing number * in case of array indexes. * If false (default), an error will be thrown when the * path doesn't exist. * @return Returns a new, updated object or array */ export declare function setIn<T, U = unknown, V = unknown>(object: U, path: JSONPath, value: V, createPath?: boolean): T; /** * helper function to replace a nested property in an object with a new value * without mutating the object itself. * * @return Returns a new, updated object or array */ export declare function updateIn<T, U = unknown, V = unknown>(object: T, path: JSONPath, transform: (value: U) => V): T; /** * helper function to delete a nested property in an object * without mutating the object itself. * * @return Returns a new, updated object or array */ export declare function deleteIn<T, U = unknown>(object: U, path: JSONPath): T; /** * Insert a new item in an array at a specific index. * Example usage: * * insertAt({arr: [1,2,3]}, ['arr', '2'], 'inserted') // [1,2,'inserted',3] */ export declare function insertAt<T, U = unknown>(document: T, path: JSONPath, value: U): T; /** * Transform a JSON object, traverse over the whole object, * and allow replacing Objects/Arrays/values. */ export declare function transform<T, U = unknown, V = unknown, W = unknown>(document: U, callback: (document: V, path: JSONPath) => W, path?: JSONPath): T; /** * Test whether a path exists in a JSON object * @return Returns true if the path exists, else returns false */ export declare function existsIn<T>(document: T, path: JSONPath): boolean; //# sourceMappingURL=immutabilityHelpers.d.ts.map