Skip to content

Commit

Permalink
Add Typescript definition and require Node.js 10 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Dec 3, 2020
1 parent 4fb28db commit 38d450a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: node_js

node_js:
- '6'
- '4'
- '0.12'
- '0.10'
- '14'
- '12'
- '10'
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
Randomize the order of items in an array
@param array The array to shuffle.
@example
```
const shuffled = arrayShuffle([1, 2, 3, 4, 5, 6]);
//=> [3, 5, 4, 1, 2, 6]
```
*/
declare function arrayShuffle<ArrayValueType>(array: readonly ArrayValueType[]): ArrayValueType[];

export = arrayShuffle;
22 changes: 9 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
'use strict';
module.exports = function (arr) {
if (!Array.isArray(arr)) {
throw new TypeError('Expected Array, got ' + typeof arr);

module.exports = array => {
if (!Array.isArray(array)) {
throw new TypeError(`Expected an array, got ${typeof array}`);
}

var rand;
var tmp;
var len = arr.length;
var ret = arr.slice();
array = [...array];

while (len) {
rand = Math.floor(Math.random() * len--);
tmp = ret[len];
ret[len] = ret[rand];
ret[rand] = tmp;
for (let index = array.length - 1; index > 0; index--) {
const newIndex = Math.floor(Math.random() * (index + 1));
[array[index], array[newIndex]] = [array[newIndex], array[index]];
}

return ret;
return array;
};
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import arrayShuffle = require('.');

const fixture = [1, 2, 3, 4, 5];

expectType<number[]>(arrayShuffle(fixture));
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"array",
"arr",
"list",
"shuffle",
"sort",
"random",
"rand",
"fisher",
"yates"
"yates",
"durstenfeld"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^3.13.0",
"tsd": "^0.13.1",
"xo": "^0.35.0"
}
}
15 changes: 9 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

> Randomize the order of items in an array
Uses the [Fisher–Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).

Uses the [Durstenfeld algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm) based on the [Fisher–Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).

## Install

```
$ npm install --save array-shuffle
$ npm install array-shuffle
```


## Usage

```js
const shuffled = arrayShuffle([1, 2, 3, 4, 5, 6]);
//=> [3, 5, 4, 1, 2, 6]
```

## API

### arrayShuffle(array)

#### array

## License
Type: `array`

MIT © [Sindre Sorhus](https://sindresorhus.com)
The array to shuffle.
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import m from './';
const test = require('ava');
const arrayShuffle = require('.');

test(t => {
t.notDeepEqual(m([1, 2, 3, 4, 5, 6]), [1, 2, 3, 4, 5, 6]);
test('main', t => {
t.notDeepEqual(arrayShuffle([1, 2, 3, 4, 5, 6]), [1, 2, 3, 4, 5, 6]);
});

0 comments on commit 38d450a

Please sign in to comment.