/** * @fileoverview Array comparison utility with deep object diffing capabilities. * @module TinyArrayComparator */ export type HashEntry = { /** * - The original item stored in the map. */ item: Item; /** * - The calculated hash representing the item's value. */ hash: string; }; export type ObjectDiff = { /** * - Properties added in the new object. */ added: Partial; /** * - Properties removed from the old object. */ deleted: Partial; /** * - Properties that changed value. */ modified: Partial<{ [K in keyof Item]: { oldValue: Item[K]; newValue: Item[K]; }; }>; /** * - Nested differences for nested objects. */ nested: Partial<{ [K in keyof Item]: ObjectDiff; }>; }; export type DiffResult = { /** * - The item that was affected. */ item: Item; /** * - The original item before the change (only for 'edited' status). */ oldItem?: Item; /** * - The nature of the change. */ status: 'added' | 'deleted' | 'edited'; /** * - Detailed property changes (only if status is 'edited' and deepComparison is enabled). */ details?: ObjectDiff; }; export type InternalState = { /** * - A map storing the identity key as the key and the hash entry as the value. */ oldItemsMap: Map>; /** * - The final array that collects added, deleted, or edited items. */ affectedItems: Array>; }; export type AffectedItems = Array>; export type ComparatorOptions = { /** * - The property name used as a unique identifier for objects to detect edits. */ idKey?: keyof Item | (string & {}); /** * - If true, provides detailed property-level diffs for edited objects. */ deepComparison?: boolean; }; /** * @template Item * @typedef {Object} HashEntry * @property {Item} item - The original item stored in the map. * @property {string} hash - The calculated hash representing the item's value. */ /** * @template Item * @typedef {Object} ObjectDiff * @property {Partial} added - Properties added in the new object. * @property {Partial} deleted - Properties removed from the old object. * @property {Partial<{ [K in keyof Item]: { oldValue: Item[K], newValue: Item[K] } }>} modified - Properties that changed value. * @property {Partial<{ [K in keyof Item]: ObjectDiff }>} nested - Nested differences for nested objects. */ /** * @template Item * @typedef {Object} DiffResult * @property {Item} item - The item that was affected. * @property {Item} [oldItem] - The original item before the change (only for 'edited' status). * @property {'added'|'deleted'|'edited'} status - The nature of the change. * @property {ObjectDiff} [details] - Detailed property changes (only if status is 'edited' and deepComparison is enabled). */ /** * @template Item * @typedef {Object} InternalState * Holds the internal tracking variables during the comparison process. * @property {Map>} oldItemsMap - A map storing the identity key as the key and the hash entry as the value. * @property {Array>} affectedItems - The final array that collects added, deleted, or edited items. */ /** * The final array that collects added, deleted, or edited items. * @template Item * @typedef {Array>} AffectedItems */ /** * Options to configure the comparator. * @template Item * @typedef {Object} ComparatorOptions * @property {keyof Item | (string & {})} [idKey='id'] - The property name used as a unique identifier for objects to detect edits. * @property {boolean} [deepComparison=true] - If true, provides detailed property-level diffs for edited objects. */ /** * Compares two arrays efficiently by hashing their items to detect additions, deletions, and edits. * @template {any} ArrayItem */ declare class TinyArrayComparator { #private; /** * Generates a simple 32-bit integer hash converted to a base36 string. * @param {any} item - The item to be hashed. * @returns {string} The unique hash representing the item's value. * @throws {TypeError} Throws a TypeError if the item cannot be stringified due to circular references. */ static generateHash(item: any): string; /** * Gets the current identity key. * @returns {string|null} */ get idKey(): string | null; /** * Sets the identity key used for object comparison. * @param {string|null} value - The property name to use as an identifier. * @throws {TypeError} Throws a TypeError if the value is not a string. */ set idKey(value: string | null); /** * Gets whether deep comparison is enabled. * @returns {boolean} */ get deepComparison(): boolean; /** * Sets whether deep comparison is enabled. * @param {boolean} value - The configuration value. */ set deepComparison(value: boolean); /** * Gets the current base array used for comparisons. * @returns {ArrayItem[]} The current initial state of the array. */ get oldArray(): ArrayItem[]; /** * Sets a new base array for future comparisons. * @param {ArrayItem[]} oldArray - The initial state of the array. * @throws {TypeError} Throws an error if the provided value is not an array. */ set oldArray(oldArray: ArrayItem[]); /** * Initializes the comparator with an optional base array and options. * @param {ArrayItem[]} [oldArray] - The initial state of the array to be stored. * @param {ComparatorOptions} [options={}] - Configuration options for the comparator. */ constructor(oldArray?: ArrayItem[], options?: ComparatorOptions); /** * Proxy to generates a hash converted to a string. * @param {ArrayItem} item - The item to be hashed. * @returns {string} The unique hash representing the item's value. * @private */ private _generateHash; /** * Compares the stored older array with a newer array and identifies added, deleted, or edited items. * * @param {ArrayItem[]} newArray - The modified state of the array. * @returns {AffectedItems} An array containing the affected items and their status. * @throws {TypeError} Throws an error if the provided value is not an array. */ compare(newArray: ArrayItem[]): AffectedItems; } export default TinyArrayComparator; //# sourceMappingURL=TinyArrayComparator.d.mts.map