tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
82 lines • 3.18 kB
text/typescript
export default TinyArrayComparator;
/**
* Holds the internal tracking variables during the comparison process.
*/
export type InternalState<ArrayItem extends unknown> = {
/**
* - A map storing the hash as the key and the original item as the value.
*/
oldItemsMap: Map<string, ArrayItem>;
/**
* - The final array that collects added or deleted items.
*/
affectedItems: AffectedItems<ArrayItem>;
};
/**
* The final array that collects added or deleted items.
*/
export type AffectedItems<ArrayItem extends unknown> = Array<{
item: ArrayItem;
status: "added" | "deleted";
}>;
/**
* @fileoverview Array comparison utility.
* @module TinyArrayComparator
*/
/**
* @template {any} ArrayItem
* @typedef {Object} InternalState
* Holds the internal tracking variables during the comparison process.
* @property {Map<string, ArrayItem>} oldItemsMap - A map storing the hash as the key and the original item as the value.
* @property {AffectedItems<ArrayItem>} affectedItems - The final array that collects added or deleted items.
*/
/**
* The final array that collects added or deleted items.
* @template {any} ArrayItem
* @typedef {Array<{item: ArrayItem, status: 'added'|'deleted'}>} AffectedItems
*/
/**
* Compares two arrays efficiently by hashing their items to detect additions and deletions.
* @template {any} ArrayItem
*/
declare class TinyArrayComparator<ArrayItem extends unknown> {
/**
* Generates a simple 32-bit integer hash converted to a base36 string.
* @param {any} item - The item to be hashed (can be an object, array, string, or number).
* @returns {string} The unique hash representing the item's value.
*/
static generateHash(item: any): string;
/**
* Initializes the comparator with an optional base array.
* @param {ArrayItem[]} [oldArray] - The initial state of the array to be stored.
*/
constructor(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[]);
/**
* Gets the current base array used for comparisons.
* @returns {ArrayItem[]} The current initial state of the array.
*/
get oldArray(): ArrayItem[];
/**
* Proxy to generates a hash converted to a string.
* @param {ArrayItem} item - The item to be hashed (can be an object, array, string, or number).
* @returns {string} The unique hash representing the item's value.
* @private
*/
private _generateHash;
/**
* Compares the stored older array with a newer array and identifies missing or new items.
*
* @param {ArrayItem[]} newArray - The modified state of the array.
* @returns {AffectedItems<ArrayItem>} 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<ArrayItem>;
#private;
}
//# sourceMappingURL=TinyArrayComparator.d.mts.map