/*! * Based on work from * https://github.com/mohayonao/json-touch-patch * (c) 2018 mohayonao * * MIT license * (c) 2022 Jacob Wright * * * WARNING: using /array/- syntax to indicate the end of the array makes it impossible to transform arrays correctly in * all situaions. Please avoid using this syntax when using Operational Transformations. */ import type { ApplyJSONPatchOptions, JSONPatchOp, JSONPatchOpHandlerMap } from './types.js'; export type PathLike = string | { toString(): string; }; export interface WriteOptions { soft?: boolean; } /** * A JSONPatch helps with creating and applying one or more "JSON patches". It can track one or more changes * together which may form a single operation or transaction. */ export declare class JSONPatch { ops: JSONPatchOp[]; custom: JSONPatchOpHandlerMap; /** * Create a new JSONPatch, optionally with an existing array of operations. */ constructor(ops?: JSONPatchOp[], custom?: JSONPatchOpHandlerMap); op(op: string, path: PathLike, value?: any, from?: PathLike, soft?: boolean): this; /** * Tests a value exists. If it doesn't, the patch is not applied. */ test(path: PathLike, value: any): this; /** * Adds the value to an object or array, inserted before the given index. */ add(path: PathLike, value: any, options?: WriteOptions): this; /** * Deletes the value at the given path or removes it from an array. */ remove(path: PathLike): this; /** * Replaces a value (same as remove+add). */ replace(path: PathLike, value: any, options?: WriteOptions): this; /** * Copies the value at `from` to `path`. */ copy(from: PathLike, to: PathLike, options?: WriteOptions): this; /** * Moves the value at `from` to `path`. */ move(from: PathLike, to: PathLike): this; /** * Increments a numeric value by 1 or the given amount. */ increment(path: PathLike, value?: number): this; /** * Decrements a numeric value by 1 or the given amount. */ decrement(path: PathLike, value?: number): this; /** * Flips a bit at the given index in a bitmask to the given value. */ bit(path: PathLike, index: number, on: boolean): this; /** * Creates a patch from an object partial, updating each field. Set a field to undefined to delete it. */ addUpdates(updates: { [key: string]: any; }, path?: string): this; /** * This will ensure an "add empty object" operation is created for each property along the path that does not exist. */ addObjectsInPath(obj: any, path: PathLike): this; /** * Apply this patch to an object, returning a new object with the applied changes (or the same object if nothing * changed in the patch). Optionally apply the page at the given path prefix. */ apply(obj: T, options?: ApplyJSONPatchOptions): T; /** * Transform the given patch against this one. This patch is considered to have happened first. Optionally provide * the object these operations are being applied to if available to know for sure if a numerical path is an array * index or object key. Otherwise, all numerical paths are treated as array indexes. */ transform(patch: JSONPatch | JSONPatchOp[], obj?: any): this; /** * Create a patch which can reverse what this patch does. Because JSON Patches do not store previous values, you * must provide the previous object to create a reverse patch. */ invert(obj: any): this; /** * Compose/collapse patches into fewer operations. */ compose(patch?: JSONPatch | JSONPatchOp[]): this; /** * Add two patches together. */ concat(patch: JSONPatch | JSONPatchOp[]): this; /** * Returns an array of patch operations. */ toJSON(): JSONPatchOp[]; /** * Create a new JSONPatch with the provided JSON patch operations. */ static fromJSON(this: { new (ops?: JSONPatchOp[], types?: JSONPatchOpHandlerMap): T; }, ops?: JSONPatchOp[], types?: JSONPatchOpHandlerMap): T; } export type JSONPath = T extends object ? T extends Array ? { readonly [K in keyof T & number]-?: JSONPathValue; } : { readonly [K in keyof T as T[K] extends Function ? never : K]-?: JSONPathValue>; } : never; export type JSONPathValue = { toString(): string; } & (T extends object ? JSONPath : {}); export declare function createJSONPath(): JSONPath;