Skip to content

Commit 7b51d9c

Browse files
BendingBendersindresorhus
authored andcommitted
Add TypeScript definition (#4)
1 parent 65cac73 commit 7b51d9c

File tree

8 files changed

+58
-22
lines changed

8 files changed

+58
-22
lines changed

.gitattributes

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
22
node_js:
3+
- '10'
34
- '8'
45
- '6'

index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Generate random numbers that are consecutively unique.
3+
4+
@returns A function that when called will return a random number that is never the same as the previous.
5+
6+
@example
7+
```
8+
import uniqueRandom = require('unique-random');
9+
const random = uniqueRandom(1, 10);
10+
11+
console.log(random(), random(), random());
12+
//=> 5 2 6
13+
```
14+
*/
15+
declare function uniqueRandom(
16+
minimum: number,
17+
maximum: number
18+
): () => number;
19+
20+
export = uniqueRandom;

index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
2-
module.exports = (min, max) => {
3-
let prev;
4-
return function rand() {
5-
const num = Math.floor((Math.random() * (max - min + 1)) + min);
6-
prev = (num === prev && min !== max) ? rand() : num;
7-
return prev;
2+
3+
module.exports = (minimum, maximum) => {
4+
let previousValue;
5+
return function random() {
6+
const number = Math.floor(
7+
(Math.random() * (maximum - minimum + 1)) + minimum
8+
);
9+
previousValue = number === previousValue && minimum !== maximum ? random() : number;
10+
return previousValue;
811
};
912
};

index.test-d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {expectType} from 'tsd';
2+
import uniqueRandom = require('.');
3+
4+
const random = uniqueRandom(1, 10);
5+
6+
expectType<() => number>(random);
7+
expectType<number>(random());

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"unique",
@@ -30,7 +31,8 @@
3031
"consecutively"
3132
],
3233
"devDependencies": {
33-
"ava": "*",
34-
"xo": "*"
34+
"ava": "^1.4.1",
35+
"tsd": "^0.7.2",
36+
"xo": "^0.24.0"
3537
}
3638
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ $ npm install unique-random
1616

1717
```js
1818
const uniqueRandom = require('unique-random');
19-
const rand = uniqueRandom(1, 10);
19+
const random = uniqueRandom(1, 10);
2020

21-
console.log(rand(), rand(), rand());
21+
console.log(random(), random(), random());
2222
//=> 5 2 6
2323
```
2424

2525

2626
## API
2727

28-
### uniqueRandom(min, max)
28+
### uniqueRandom(minimum, maximum)
2929

3030
Returns a function that when called will return a random number that is never the same as the previous.
3131

test.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import test from 'ava';
2-
import m from '.';
2+
import uniqueRandom from '.';
33

44
test('main', t => {
5-
const uniqueRandom = m(1, 10);
5+
const random = uniqueRandom(1, 10);
66
let count = 1000;
7-
let current;
8-
let prev;
7+
let currentValue;
8+
let previousValue;
99

1010
while (--count > 0) {
11-
current = uniqueRandom();
11+
currentValue = random();
1212

13-
if (current === prev || current > 10 || current < 1) {
13+
if (
14+
currentValue === previousValue ||
15+
currentValue > 10 ||
16+
currentValue < 1
17+
) {
1418
t.fail();
1519
}
1620

17-
prev = current;
21+
previousValue = currentValue;
1822
}
1923

2024
t.pass();

0 commit comments

Comments
 (0)