@endritkj/myshufflepackage
v0.2.0
Published
A small array shuffle utility package for Node.js
Downloads
114
Readme
@endritkj/myshufflepackage
A small utility package to shuffle arrays in Node.js.
Description
This package provides two functions for shuffling arrays:
shuffleArray(array)— shuffles an array usingMath.random()cryptoShuffleArray(array)— shuffles an array using cryptographically secure randomness via Node.jscrypto.randomInt()
Both functions return a new array and do not mutate the original input array.
Features
- Easy array shuffling
- Standard shuffle with
Math.random() - Cryptographically secure shuffle with
crypto.randomInt() - Returns a new array
- Does not modify the original input array
Installation
npm install @endritkj/myshufflepackageUsage
Import the functions:
const {
shuffleArray,
cryptoShuffleArray,
} = require("@endritkj/myshufflepackage");Standard shuffle
Use shuffleArray() for a simple shuffle based on Math.random().
const { shuffleArray } = require("@endritkj/myshufflepackage");
const numbers = [1, 2, 3, 4, 5];
const shuffled = shuffleArray(numbers);
console.log("Original:", numbers);
console.log("Shuffled:", shuffled);Cryptographic shuffle
Use cryptoShuffleArray() when you want stronger randomness based on Node.js cryptographic functions.
const { cryptoShuffleArray } = require("@endritkj/myshufflepackage");
const numbers = [1, 2, 3, 4, 5];
const shuffled = cryptoShuffleArray(numbers);
console.log("Original:", numbers);
console.log("Crypto shuffled:", shuffled);API
shuffleArray(array)
Shuffles the given array using Math.random().
Parameters
array— an array of values
Returns
- A new shuffled array
Example
const { shuffleArray } = require("@endritkj/myshufflepackage");
const letters = ["a", "b", "c", "d"];
const result = shuffleArray(letters);
console.log(result);cryptoShuffleArray(array)
Shuffles the given array using cryptographically secure randomness via Node.js crypto.randomInt().
Parameters
array— an array of values
Returns
- A new shuffled array
Example
const { cryptoShuffleArray } = require("@endritkj/myshufflepackage");
const letters = ["a", "b", "c", "d"];
const result = cryptoShuffleArray(letters);
console.log(result);Why use cryptoShuffleArray()?
cryptoShuffleArray() uses cryptographically secure randomness from Node.js instead of Math.random().
Use it when:
- you want stronger randomness
- you want a more secure alternative to pseudo-random shuffling
- your task explicitly requires a cryptographic shuffle
Notes
- Both functions return a new array
- The original input array is not modified
shuffleArray()usesMath.random()cryptoShuffleArray()uses Node.jscrypto.randomInt()- For most cases in this assignment,
cryptoShuffleArray()is the recommended function
Testing
Run the test suite with:
npm testVersioning
This package follows Semantic Versioning.
0.1.0— initial release0.2.0— addedcryptoShuffleArray()and updated the documentation
License
MIT
Author
Endrit Kurtaj
