-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Typescript definition and require Node.js 10 (#1)
- Loading branch information
Showing
9 changed files
with
58 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules | ||
package-lock.json | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); |