Shufflable-Array
Extended javascript native
Array
([]
) with an additional (in-place)shuffle()
methodThis doesn't modify
Array.prototype
.
npm i shufflable-array
or:
yarn add shufflable-array
Usage
import ShufflableArray from 'shufflable-array'
const shufflableArray = new ShufflableArray(1, 2, 3, 4, 5)
console.log(shufflableArray instanceof Array) // => true
shufflableArray.shuffle() // => Ex. [ 5, 1, 4, 3, 2 ]
Example
import ShufflableArray from 'shufflable-array'
const shufflableArray = new ShufflableArray(1, 2, 3, 4, 5)
console.log(shufflableArray) // => ShufflableArray(5) [ 1, 2, 3, 4, 5 ]
console.log(shufflableArray[2], shufflableArray[5]) // => 3 undefined
console.log(shufflableArray.length) // => 5
shufflableArray.push(6)
console.log(shufflableArray) // => ShufflableArray(6) [ 1, 2, 3, 4, 5, 6 ]
shufflableArray[5] = 0
console.log(shufflableArray) // => ShufflableArray(6) [ 1, 2, 3, 4, 5, 0 ]
const shuffledArray = shufflableArray.shuffle()
console.log(shufflableArray) // => Ex. ShufflableArray(6) [ 1, 3, 5, 4, 0, 2 ]
console.log(shufflableArray === shuffledArray) // => true