UNPKG

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.

44 lines 1.9 kB
/** * Randomly shuffles the elements of an array in place using the Fisher–Yates algorithm. * * This implementation ensures a uniform distribution of permutations. * Original algorithm source: StackOverflow (link above). * * @param {any[]} items - The array to shuffle. * @returns {any[]} The same array instance, now shuffled in place. */ export function shuffleArray(items: any[]): any[]; /** * Generates an array with repeated phases according to counts. * * @param {string[]} phases - Array of phase names, e.g., ['Full', 'Half1', 'Half2', 'New']. * @param {number[]} counts - Array of integers specifying how many times to repeat each phase, e.g., [4,5,5,4]. * @returns {string[]} - Flattened array containing phases repeated according to counts, concatenated in order. */ export function multiplyArrayBlocks(phases: string[], counts: number[]): string[]; /** * Diff two class lists. * @param {any[]} oldItems * @param {any[]} newItems */ export function diffArrayList(oldItems: any[], newItems: any[]): { added: any[]; removed: any[]; }; /** * Generates a comparator function to sort an array of objects by a given key. * * @param {string} item - The object key to sort by. * @param {boolean} [isReverse=false] - If `true`, the sorting will be in descending order. * @returns {(a: Object<string|number, *>, b: Object<string|number, *>) => number} Comparator function compatible with Array.prototype.sort(). * * @example * const arr = [{ pos: 2 }, { pos: 1 }, { pos: 3 }]; * arr.sort(arraySortPositions('pos')); // Ascending: [{pos: 1}, {pos: 2}, {pos: 3}] * * @example * const arr = [{ pos: 2 }, { pos: 1 }, { pos: 3 }]; * arr.sort(arraySortPositions('pos', true)); // Descending: [{pos: 3}, {pos: 2}, {pos: 1}] */ export function arraySortPositions(item: string, isReverse?: boolean): (a: any, b: any) => number; //# sourceMappingURL=array.d.mts.map