Skip to content

Commit

Permalink
Add TypeScript definition (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 12, 2019
1 parent 65cac73 commit 7b51d9c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
Generate random numbers that are consecutively unique.
@returns A function that when called will return a random number that is never the same as the previous.
@example
```
import uniqueRandom = require('unique-random');
const random = uniqueRandom(1, 10);
console.log(random(), random(), random());
//=> 5 2 6
```
*/
declare function uniqueRandom(
minimum: number,
maximum: number
): () => number;

export = uniqueRandom;
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';
module.exports = (min, max) => {
let prev;
return function rand() {
const num = Math.floor((Math.random() * (max - min + 1)) + min);
prev = (num === prev && min !== max) ? rand() : num;
return prev;

module.exports = (minimum, maximum) => {
let previousValue;
return function random() {
const number = Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
);
previousValue = number === previousValue && minimum !== maximum ? random() : number;
return previousValue;
};
};
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import uniqueRandom = require('.');

const random = uniqueRandom(1, 10);

expectType<() => number>(random);
expectType<number>(random());
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"unique",
Expand All @@ -30,7 +31,8 @@
"consecutively"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ $ npm install unique-random

```js
const uniqueRandom = require('unique-random');
const rand = uniqueRandom(1, 10);
const random = uniqueRandom(1, 10);

console.log(rand(), rand(), rand());
console.log(random(), random(), random());
//=> 5 2 6
```


## API

### uniqueRandom(min, max)
### uniqueRandom(minimum, maximum)

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

Expand Down
18 changes: 11 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import test from 'ava';
import m from '.';
import uniqueRandom from '.';

test('main', t => {
const uniqueRandom = m(1, 10);
const random = uniqueRandom(1, 10);
let count = 1000;
let current;
let prev;
let currentValue;
let previousValue;

while (--count > 0) {
current = uniqueRandom();
currentValue = random();

if (current === prev || current > 10 || current < 1) {
if (
currentValue === previousValue ||
currentValue > 10 ||
currentValue < 1
) {
t.fail();
}

prev = current;
previousValue = currentValue;
}

t.pass();
Expand Down

0 comments on commit 7b51d9c

Please sign in to comment.