/** * Options available for doing diffs. Currently no options are defined. * * @public */ type DiffOptions = Record; /** * The recognized diff value types * * @public */ type ValueType = 'array' | 'boolean' | 'null' | 'number' | 'object' | 'string' | 'undefined'; /** * An "input" holds the _type_ of the value, the actual value, an optional annotation, * along with potential helper methods and properties, which vary dependending on the type * * @public */ type Input = NumberInput | BooleanInput | StringInput | NullInput | ObjectInput | ArrayInput; /** * Shared properties for all input types * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface BaseInput { annotation: A; } /** * Input type for strings, which supports slicing parts of the string while maintaining the * annotation of the parent. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface StringInput extends BaseInput { type: 'string'; value: string; sliceAnnotation(start: number, end: number): { text: string; annotation: A; }[]; } /** * Input type for numbers. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface NumberInput extends BaseInput { type: 'number'; value: number; } /** * Input type for booleans. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface BooleanInput extends BaseInput { type: 'boolean'; value: boolean; } /** * Input type for `null` values. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface NullInput extends BaseInput { type: 'null'; value: null; } /** * Input type for object values. Caches the available keys, and allows retrieval of properties, * while automatically wrapping the retrieved property in a typed input container. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface ObjectInput extends BaseInput { type: 'object'; /** * The actual object value */ value: Record; /** * The keys of the object */ keys: string[]; /** * Retrieve the property with the given `key`, automatically wrapping it in an input container. * * @param key - The key of the property you want to retrieve. * @returns Typed input container, or `undefined` if the property does not exist */ get(key: string): Input | undefined; } /** * Input type for array values. Helper functions are available for getting the item and/or * annotation at a given index. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface ArrayInput extends BaseInput { type: 'array'; /** * The actual array value */ value: unknown[]; /** * The length of the array */ length: number; /** * Retrieve the value at the given `index`, automatically wrapping it in an input container. * * @param index - The index of the item to retrieve * @returns Typed input container, or `undefined` if the item does not exist */ at(index: number): Input; /** * Retrieve the _annotation_ for an item at the given index * * @param index - The index of the item to fetch the annotation for * @returns The annotation at the given index, or `undefined` if the item does not exist */ annotationAt(index: number): A; } /** * Diff for something that has been added - eg a property in an object, * an item in an array, a segment of a string or similar. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Value. The type of the destination (eg `after` or `to`) value. * @public */ interface AddedDiff { action: 'added'; isChanged: true; fromValue: null | undefined; toValue: V; annotation: A; } /** * Diff for something that has been removed - eg a property in an object, * an item in an array, a segment of a string or similar. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Value. The type of the source (eg `before` or `from`) value. * @public */ interface RemovedDiff { action: 'removed'; isChanged: true; fromValue: V; toValue: null | undefined; annotation: A; } /** * Diff for something that has changed - eg it was not added or removed, but the * value has changed "in place". Note that {@link TypeChangeDiff} is used for values that change * their _type_, thus the `V` type parameter represents both the source and the destination value * in this type. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Value. The type of the value. * @public */ interface ChangedDiff { action: 'changed'; isChanged: true; fromValue: V; toValue: V; annotation: A; } /** * Diff (or lack thereof, in this case) for a value that has _not_ changed. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Value. The type of the destination (eg `after`) value. * @public */ interface UnchangedDiff { action: 'unchanged'; isChanged: false; fromValue: V; toValue: V; } /** * Diff with all the possible diff types. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Value. Type of the value repesented in the diff. * @public */ type FullDiff = AddedDiff | RemovedDiff | ChangedDiff | UnchangedDiff; /** * Diff of a string. Holds an additional array of string _segments_, * indicating which portions of the string is changed/unchanged. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type StringDiff = FullDiff & { type: 'string'; segments: StringDiffSegment[]; }; /** * Diff of a number. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type NumberDiff = FullDiff & { type: 'number'; }; /** * Diff of a boolean. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type BooleanDiff = FullDiff & { type: 'boolean'; }; /** * Diff for a value that has changed from one type to another. * For example, an object property going from `null` to `string`. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface TypeChangeDiff { type: 'typeChange'; action: 'changed'; isChanged: true; fromType: string; fromValue: unknown; fromDiff: Diff & { action: 'removed'; }; toType: string; toValue: unknown; toDiff: Diff & { action: 'added'; }; annotation: A; } /** * Diff for an object value. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam T - Value type. * @public */ type ObjectDiff> = FullDiff & { type: 'object'; fields: Record>; }; /** * Diff for an array value. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @typeParam V - Item value type. * @public */ type ArrayDiff = FullDiff & { type: 'array'; items: ItemDiff[]; }; /** * Diff for a `null` value. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type NullDiff = FullDiff & { type: 'null'; }; /** * Diff for any value type. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type Diff = NullDiff | StringDiff | NumberDiff | BooleanDiff | ObjectDiff | ArrayDiff | TypeChangeDiff; /** * Diff of a string segment (eg a portion/slice), and whether or not it was changed or unchanged. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ type StringDiffSegment = StringSegmentChanged | StringSegmentUnchanged; /** * Diff of a string segment that has changed. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface StringSegmentChanged { type: 'stringSegment'; action: 'added' | 'removed'; text: string; annotation: A; } /** * Diff of a string segment that is unchanged. * * @public */ interface StringSegmentUnchanged { type: 'stringSegment'; action: 'unchanged'; text: string; } /** * Diff of an item in an array, representing whether or not it has moved within the array, * and if so, which index it was moved from/to. * * If not moved, `fromIndex` and `toIndex` will have the same value. * If the item was added, `fromIndex` will be `undefined`. * If the item was removed, `toIndex` will be `undefined`. * * @typeParam A - Annotation. Timestamps, author and similar info is attached by Sanity, but anything is allowed. * @public */ interface ItemDiff { fromIndex: number | undefined; toIndex: number | undefined; hasMoved: boolean; diff: Diff; annotation: A; } /** * Takes a `from` and `to` input and calulates a diff between the two * * @param fromInput - The source (`from`) input - use {@link wrap | the wrap() method} to generate an "input" * @param toInput - The destination (`to`) input - use {@link wrap | the wrap() method} to generate an "input" * @param options - Options for the diffing process - currently no options are defined * @returns A diff object representing the change * @public */ declare function diffInput(fromInput: Input, toInput: Input, options?: DiffOptions): Diff; /** * Takes an input (any JSON-serializable value) and an annotation, and generates an input * object for it, to be used with {@link diffInput | the diffInput() method} and others. * * @param input - The value to wrap in an input object * @param annotation - Annotation attached to the input - will be bound to generated diffs * @returns A input object * @throws if `input` is not a JSON-serializable type * @public */ declare function wrap(input: unknown, annotation: A): Input; export { AddedDiff, ArrayDiff, ArrayInput, BaseInput, BooleanDiff, BooleanInput, ChangedDiff, Diff, DiffOptions, FullDiff, Input, ItemDiff, NullDiff, NullInput, NumberDiff, NumberInput, ObjectDiff, ObjectInput, RemovedDiff, StringDiff, StringDiffSegment, StringInput, StringSegmentChanged, StringSegmentUnchanged, TypeChangeDiff, UnchangedDiff, ValueType, diffInput, wrap };