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.

108 lines (70 loc) โ€ข 2.97 kB
# ๐Ÿ“ฆ array.mjs 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). ## โœจ Features - ๐Ÿ“š Uses the classic **Fisherโ€“Yates** algorithm - ๐Ÿ”„ Shuffles arrays **in-place** - ๐ŸŽฏ Guarantees **uniform distribution** of permutations - ๐Ÿ” Well-documented and easy to read - ๐Ÿงช No dependencies โ€” just plug and play ## ๐Ÿš€ 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. ## ๐Ÿง  How It Works This function implements the **Fisherโ€“Yates shuffle** (also known as the Knuth shuffle), which runs in linear time and guarantees uniform randomness: ```js while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [items[currentIndex], items[randomIndex]] = [items[randomIndex], items[currentIndex]]; } ``` You can find the original discussion here: ๐Ÿ”— https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array ## ๐Ÿ“ API ### `shuffleArray(items: string[]): string[]` - **items** โ€” An array of strings to shuffle. - **Returns** โ€” The same array instance, but now shuffled. --- # ๐Ÿ“ฆ `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