/** * Allows diffing of arbitrary JSON objects. */ import type { Get, Paths } from 'type-fest'; export interface DefinitionDiffOperation { type: 'add' | 'update' | 'remove'; key: string; item: T; } declare abstract class DefinitionDiffField { /** * The name of the field to be presented to the user. */ readonly name: string; constructor(name: string); abstract diff(current: T, desired: T): DefinitionDiffOperation[]; abstract apply(current: T, diff: DefinitionDiffOperation[]): T; /** * Returns the verb to be used in the UI to describe the action e.g. created or added. * @param isNew Whether the item is new. */ abstract getActionVerb(isNew: boolean): string; } interface DefinitionDiffKeyedArrayFieldOptions { /** * Whether to allow removing items from the array. */ allowRemove?: boolean; /** * Ignore fields that should be ignored when diffing. Defaults to `['id']`. */ ignoreFields?: readonly string[]; } /** * A field that is an array of objects with a unique key. */ export declare class DefinitionDiffKeyedArrayField[]> extends DefinitionDiffField { readonly getKey: (item: T[number]) => string; readonly options: DefinitionDiffKeyedArrayFieldOptions; constructor(name: string, getKey: (item: T[number]) => string, options?: DefinitionDiffKeyedArrayFieldOptions); diff(current: T | undefined, desired: T | undefined): DefinitionDiffOperation[]; apply(current: T | undefined, diff: DefinitionDiffOperation[]): T; getActionVerb(isNew: boolean): string; } /** * A field that is a replacement for the entire object or array. */ export declare class DefinitionDiffReplacementField extends DefinitionDiffField { diff(current: T, desired: T): DefinitionDiffOperation[]; apply(current: T, diff: DefinitionDiffOperation[]): T; getActionVerb(isNew: boolean): string; } /** * A field that ensures an array contains certain values. * This field type only adds items, never removes them. */ export declare class DefinitionDiffArrayIncludesField extends DefinitionDiffField { private readonly getKey?; constructor(name: string, getKey?: (item: T[number]) => string); diff(current: T | undefined, desired: T | undefined): DefinitionDiffOperation[]; apply(current: T | undefined, diff: DefinitionDiffOperation[]): T; getActionVerb(isNew: boolean): string; } type ConvertToTemplateString = T extends number ? `${T}` : T; export type DefinitionDiffConfig = Partial<{ [K in Paths]: DefinitionDiffField>, undefined>>; }>; export type DefinitionDiffOutput> = Partial>; export declare function createDefinitionDiffConfig(config: DefinitionDiffConfig): DefinitionDiffConfig; type InferDefinitionDiffInputFromConfig> = TConfig extends DefinitionDiffConfig ? T : never; /** * Creates a diff between two objects. */ export declare function createDefinitionDiff>(current: InferDefinitionDiffInputFromConfig, desired: InferDefinitionDiffInputFromConfig, configuration: TConfig): DefinitionDiffOutput | undefined; /** * Applies a diff to an object. */ export declare function applyDefinitionDiff>(current: TInput, diff: DefinitionDiffOutput, configuration: TConfig): TInput; export {}; //# sourceMappingURL=definition-diff.d.ts.map