A lightweight utility to shuffle the elements of an array in place using the FisherβYates algorithm.
Install via npm:
npm install @bernardpolidario/shuffle
const shuffleArray = require('@bernardpolidario/shuffle');
const items = ['apple', 'banana', 'cherry', 'date'];
const shuffled = shuffleArray(items);
console.log(shuffled);
// β [ 'cherry', 'apple', 'date', 'banana' ] (order will vary)
β οΈ Note: The original array is mutated. If you want to preserve the original, clone it first:
const shuffled = shuffleArray([...items]);This package uses the FisherβYates shuffle, a time-tested algorithm that ensures every possible ordering of the array is equally likely.
- Parameters:
arrayβ An array of any type (e.g. strings, numbers, objects).
- Returns:
- The same array, shuffled in place.
- Randomizing quiz options
- Shuffling playlist items
- Mixing flashcards
- Creating randomized test data
To test this package locally:
npm link
# then in another project
npm link @bernardpolidario/shuffle
MIT Β© 2025 Your Name