/** * JsonDiffPatch - A diff and patch library similar to git * Instead of storing whole data, just store the diff to save memory * * Based on: https://github.com/benjamine/jsondiffpatch * * Delta Format: * - undefined: no change * - [newValue]: added * - [oldValue, newValue]: modified * - [oldValue, 0, 0]: deleted * - ['', newIndex, 3]: moved (arrays only) * - Arrays: { _t: 'a', ...changes } */ /** * Marker constants for delta format * These follow the jsondiffpatch specification for distinguishing delta types */ export declare const DELETED_MARKER = 0; export declare const MOVED_MARKER = 3; /** * Delta represents the difference between two values */ export type Delta = undefined | AddedDelta | ModifiedDelta | DeletedDelta | MovedDelta | ObjectDelta | ArrayDelta; /** * Added delta: [newValue] * Represents a value that was added */ export type AddedDelta = [unknown]; /** * Modified delta: [oldValue, newValue] * Represents a value that was changed */ export type ModifiedDelta = [unknown, unknown]; /** * Deleted delta: [oldValue, 0, 0] * The two zeros are markers to distinguish deletion from modification * Second element (0) indicates deletion, third element (0) confirms the format */ export type DeletedDelta = [unknown, typeof DELETED_MARKER, typeof DELETED_MARKER]; /** * Moved delta: ["", newIndex, 3] * Used in array diffs to indicate an item moved to a new position * First element can be empty string or the moved value (if includeValueOnMove is true) * Second element is the target index * Third element (3) is the move marker to distinguish from other delta types */ export type MovedDelta = ["" | unknown, number, typeof MOVED_MARKER]; export interface ObjectDelta { [key: string]: Delta; } export interface ArrayDelta { _t: "a"; [key: string]: Delta | string | MovedDelta; } export interface Options { /** * Function to match objects when diffing arrays * By default only === operator is used */ objectHash?: (obj: unknown) => string | number; arrays?: { /** Detect items moved inside the array (default: true) */ detectMove?: boolean; /** Include value of moved items in deltas (default: false) */ includeValueOnMove?: boolean; }; /** * Filter to ignore certain properties */ propertyFilter?: (name: string, context: { left: unknown; right: unknown; }) => boolean; /** * Clone values in delta to avoid references (default: false) */ cloneDiffValues?: boolean | ((value: unknown) => unknown); /** * Omit removed values from delta for more compact deltas (default: false) * Warning: makes delta irreversible */ omitRemovedValues?: boolean; } /** * Deep clone a value */ export declare function clone(arg: unknown): unknown; /** * Calculate the difference between two values */ export declare function diff(left: unknown, right: unknown, options?: Options): Delta; /** * Apply a delta to a value */ export declare function patch(left: unknown, delta: Delta, options?: Options): unknown; /** * Reverse a delta (to undo it) */ export declare function reverse(delta: Delta): Delta; /** * Unpatch: reverse the delta and apply it */ export declare function unpatch(right: unknown, delta: Delta, options?: Options): unknown; /** * DiffPatcher class for creating instances with specific options */ export declare class DiffPatcher { private options; constructor(options?: Options); diff(left: unknown, right: unknown): Delta; patch(left: unknown, delta: Delta): unknown; reverse(delta: Delta): Delta; unpatch(right: unknown, delta: Delta): unknown; clone(value: unknown): unknown; } /** * Create a new DiffPatcher instance with options */ export declare function create(options?: Options): DiffPatcher; export declare const jsondiffpatch: { diff: (left: unknown, right: unknown) => Delta; patch: (left: unknown, delta: Delta) => unknown; reverse: (delta: Delta) => Delta; unpatch: (right: unknown, delta: Delta) => unknown; clone: (value: unknown) => unknown; create: typeof create; DiffPatcher: typeof DiffPatcher; }; export default jsondiffpatch;