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.

117 lines (75 loc) β€’ 3.77 kB
# πŸ“¦ array.mjs A minimal and handy module offering a couple of focused utilities for working with arrays β€” from shuffling items randomly to generating smart sort functions for object arrays. Ideal for quick scripting or modular use in larger projects. --- ## `shuffleArray(items: string[]): string[]` πŸ”„ A tiny utility for shuffling arrays using the good old Fisher–Yates algorithm. Simple, efficient, and perfectly random (as far as JavaScript's `Math.random()` allows, anyway). - **items** β€” An array of strings to shuffle. - **Returns** β€” The same array instance, but now shuffled. ### πŸš€ Usage ```js import { shuffleArray } from './array.mjs'; const fruits = ['apple', 'banana', 'cherry', 'date']; shuffleArray(fruits); console.log(fruits); // ['banana', 'cherry', 'apple', 'date'] (order will vary) ``` > Note: The original array is shuffled in place. If you want to preserve the original order, make a copy before shuffling. You can find the original discussion here: πŸ”— https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array --- ## πŸ“¦ `arraySortPositions` πŸ”§ **Generates a comparator function** to sort an array of objects by a specified key, with optional reverse order. ### πŸ“Œ Function Signature ```js arraySortPositions(item: string, isReverse?: boolean): (a: Object<string|number, *>, b: Object<string|number, *>) => number ``` ### 🧠 Parameters | Name | Type | Default | Description | | ----------- | --------- | ------- | ---------------------------------------------------------- | | `item` | `string` | β€” | πŸ”‘ The key to sort the objects by. | | `isReverse` | `boolean` | `false` | πŸ”„ If `true`, the sorting will be in **descending** order. | ### 🎯 Returns 🧩 A **comparator function** compatible with `Array.prototype.sort()`: ```js (a, b) => number ``` It compares two objects based on the specified `item` key. ### πŸ’‘ Examples ```js const arr = [{ pos: 2 }, { pos: 1 }, { pos: 3 }]; arr.sort(arraySortPositions('pos')); // πŸ”Ό Ascending: [{ pos: 1 }, { pos: 2 }, { pos: 3 }] ``` ```js const arr = [{ pos: 2 }, { pos: 1 }, { pos: 3 }]; arr.sort(arraySortPositions('pos', true)); // πŸ”½ Descending: [{ pos: 3 }, { pos: 2 }, { pos: 1 }] ``` ### πŸ› οΈ Use Case Great for situations where you need to **dynamically sort objects** by one of their keys, such as: * Sorting users by age * Ordering tasks by priority * Displaying leaderboard scores --- ## πŸ” `multiplyArrayBlocks(phases, counts)` Generates a flattened array with phases repeated according to the specified counts. * **`phases`** πŸ“ β€” Array of phase names (e.g., `['Full', 'Half1', 'Half2', 'New']`). * **`counts`** πŸ”’ β€” Array of integers specifying how many times each phase should be repeated (e.g., `[4, 5, 5, 4]`). * **Returns** πŸ“¦ β€” A single array where each phase appears the given number of times, in order. **Example:** ```js multiplyArrayBlocks(['Full', 'Half1', 'Half2', 'New'], [4, 5, 5, 4]); // ➑ ['Full','Full','Full','Full','Half1','Half1','Half1','Half1','Half1', ...] ``` --- ## πŸ”€ `diffArrayList(oldItems, newItems)` Compares two arrays and determines which items were **added** and which were **removed**. * **`oldItems`** πŸ“œ β€” Array of original class names. * **`newItems`** πŸ“œ β€” Array of updated class names. * **Returns** πŸ“¦ β€” An object with two arrays: * `added` β†’ classes that exist in `newItems` but not in `oldItems`. * `removed` β†’ classes that exist in `oldItems` but not in `newItems`. **Example:** ```js diffArrayList(['btn', 'active'], ['btn', 'disabled']); // ➑ { added: ['disabled'], removed: ['active'] } ```