An open API service indexing awesome lists of open source software.

https://github.com/mirismaili/shufflable-array

Extended javascript native Array with an additional shuffle() method
https://github.com/mirismaili/shufflable-array

array built-in-array builtin-array native-array safe shufflable shuffle shuffleable

Last synced: 5 months ago
JSON representation

Extended javascript native Array with an additional shuffle() method

Awesome Lists containing this project

README

          

# Shufflable-Array

> Extended javascript native `Array` (`[]`) with an additional
> (**[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)**) `shuffle()` method
>
> ***This doesn't modify `Array.prototype`.***

[![npm (scoped)](https://img.shields.io/npm/v/shufflable-array.svg)](https://npmjs.com/package/shufflable-array)
[![install size](https://packagephobia.now.sh/badge?p=shufflable-array)](https://packagephobia.now.sh/result?p=shufflable-array)
[![downloads](https://img.shields.io/npm/dt/shufflable-array.svg)](https://npmjs.com/package/shufflable-array)

[![dependencies](https://david-dm.org/mirismaili/shufflable-array.svg)](https://david-dm.org/mirismaili/shufflable-array)
[![devDependencies](https://david-dm.org/mirismaili/shufflable-array/dev-status.svg)](https://david-dm.org/mirismaili/shufflable-array?type=dev)

[![license](https://img.shields.io/github/license/mirismaili/shufflable-array.svg)](https://github.com/mirismaili/shufflable-array/blob/master/LICENSE)
[![Forks](https://img.shields.io/github/forks/mirismaili/shufflable-array.svg?style=social)](https://github.com/mirismaili/shufflable-array/fork)
[![Stars](https://img.shields.io/github/stars/mirismaili/shufflable-array.svg?style=social)](https://github.com/mirismaili/shufflable-array)

```bash
npm i shufflable-array
```

or:

```bash
yarn add shufflable-array
```

## Usage

```javascript
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

```javascript
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
```