-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65cac73
commit 7b51d9c
Showing
8 changed files
with
58 additions
and
22 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,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
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,4 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '8' | ||
- '6' |
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,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; |
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,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; | ||
}; | ||
}; |
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,7 @@ | ||
import {expectType} from 'tsd'; | ||
import uniqueRandom = require('.'); | ||
|
||
const random = uniqueRandom(1, 10); | ||
|
||
expectType<() => number>(random); | ||
expectType<number>(random()); |
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