diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index e0cc348..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..02d9581 --- /dev/null +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index e8e8916..ba4a188 100644 --- a/index.js +++ b/index.js @@ -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; }; }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..639ff61 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import uniqueRandom = require('.'); + +const random = uniqueRandom(1, 10); + +expectType<() => number>(random); +expectType(random()); diff --git a/package.json b/package.json index 739605f..ead494d 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "unique", @@ -30,7 +31,8 @@ "consecutively" ], "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index fa13553..e42bc39 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test.js b/test.js index d775302..9c0e4ab 100644 --- a/test.js +++ b/test.js @@ -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();