@smo1h/shuffle-array
v1.0.2
Published
A small library that takes an array as input and returns a new array that is shuffled.
Downloads
383
Readme
@smo1h/shuffle-array (Class 2 Example)
A small library that takes an array as input and returns a new array that is shuffled. It includes both a fast standard shuffle and a secure cryptographic shuffle.
Table of Contents
Installation
Install via npm:
npm install @smo1h/shuffle-arrayInstall via yarn:
yarn add @smo1h/shuffle-arrayUsage
You can import either the standard fast shuffle or the secure cryptographic shuffle depending on your needs.
import { shuffleArray, cryptoShuffleArray } from '@smo1h/shuffle-array';
const startArray = [1, 2, 3, 4, 5];
// Fast, non-secure shuffle
const standardShuffled = shuffleArray(startArray);
// Slower, cryptographically secure shuffle
const secureShuffled = cryptoShuffleArray(startArray);
console.log(startArray); // [1, 2, 3, 4, 5]
console.log(standardShuffled); // e.g., [3, 1, 5, 2, 4]
console.log(secureShuffled); // e.g., [5, 2, 1, 4, 3]Examples
Shuffle a list of user IDs (Standard):
import { shuffleArray } from '@smo1h/shuffle-array';
const userIds = ['u1', 'u2', 'u3', 'u4'];
const randomizedOrderOfUserIds = shuffleArray(userIds);
console.log(randomizedOrderOfUserIds); // e.g., ['u1', 'u3', 'u4', 'u2']Use in a game for randomizing cards (Cryptographic):
import { cryptoShuffleArray } from '@smo1h/shuffle-array';
// For games involving currency or high stakes, use the secure shuffle!
const deck = ['Ace of Spades', 'Two of Hearts', 'Queen of Diamonds'];
const shuffledDeck = cryptoShuffleArray(deck);
console.log(shuffledDeck); // e.g., ['Two of Hearts', 'Queen of Diamonds', ...]Contribution
Contributions are welcome! Please see CONTRIBUTING.md for more details.
Security
Please refer to our SECURITY.md for information about our security policies, how to report vulnerabilities, and our approach to handling security concerns.
Important Note: This library uses non-cryptographic randomness (Math.random()). Do not rely on it for security-critical functionality.
License
This project is licensed under the MIT License.
