Skip to content

Commit 8ca6bc3

Browse files
committed
Add type definitions of shuffle-array package
1 parent e5a27ea commit 8ca6bc3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

shuffle-array/shuffle-array-tests.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path="../shuffle-array/shuffle-array.d.ts" />
2+
3+
import shuffle = require('shuffle-array');
4+
5+
// shuffle()
6+
var a = [1, 2, 3, 4, 5];
7+
var result: number[];
8+
result = shuffle(a);
9+
result = shuffle(a, {});
10+
result = shuffle(a, {copy: true});
11+
result = shuffle(a, {rng: () => 0});
12+
result = shuffle(a, {copy: true, rng: () => 0});
13+
14+
var b = ['aaa', 'bbb', 'ccc']
15+
var result2: string[];
16+
result2 = shuffle.pick(b);
17+
result2 = shuffle.pick(b, {});
18+
result2 = shuffle.pick(b, {picks: 3});
19+
result2 = shuffle.pick(b, {rng: () => 0});
20+
result2 = shuffle.pick(b, {picks: 3, rng: () => 0});

shuffle-array/shuffle-array.d.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Type definitions for shuffle-array
2+
// Project: https://github.com/pazguille/shuffle-array
3+
// Definitions by: rhysd <https://rhysd.github.io>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
declare module "shuffle-array" {
7+
/**
8+
* copy - Sets if should return a shuffled copy of the given array. By default it's a falsy value.
9+
* rng - Specifies a custom random number generator.
10+
*/
11+
interface ShuffleOption {
12+
copy?: boolean;
13+
rng?: () => number;
14+
}
15+
/**
16+
* picks - Specifies how many random elements you want to pick. By default it picks 1.
17+
* rng - Specifies a custom random number generator.
18+
*/
19+
interface PickOption {
20+
picks?: number;
21+
rng?: () => number;
22+
}
23+
interface ShuffleArray {
24+
/**
25+
* Randomizes the order of the elements in a given array.
26+
*
27+
* arr - The given array.
28+
* options - Optional configuration options.
29+
*/
30+
<T>(arr: T[], options?: ShuffleOption): T[];
31+
/**
32+
* Pick one or more random elements from the given array.
33+
*
34+
* arr - The given array.
35+
* options - Optional configuration options.
36+
*/
37+
pick<T>(arr: T[], options?: Object): T[];
38+
}
39+
var shuffle: ShuffleArray;
40+
export = shuffle;
41+
}
42+

0 commit comments

Comments
 (0)