Skip to content

Extended javascript native Array with an additional shuffle() method

License

Notifications You must be signed in to change notification settings

mirismaili/shufflable-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shufflable-Array

Extended javascript native Array ([]) with an additional (in-place) shuffle() method

This doesn't modify Array.prototype.

npm (scoped) install size downloads
dependencies devDependencies
license Forks Stars

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