File tree Expand file tree Collapse file tree 8 files changed +58
-22
lines changed Expand file tree Collapse file tree 8 files changed +58
-22
lines changed Original file line number Diff line number Diff line change 1
- * text =auto
2
- * .js text eol =lf
1
+ * text =auto eol =lf
Original file line number Diff line number Diff line change 1
1
language : node_js
2
2
node_js :
3
+ - ' 10'
3
4
- ' 8'
4
5
- ' 6'
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 1
1
'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 ;
8
11
} ;
9
12
} ;
Original file line number Diff line number Diff line change
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 ( ) ) ;
Original file line number Diff line number Diff line change 13
13
"node" : " >=6"
14
14
},
15
15
"scripts" : {
16
- "test" : " xo && ava"
16
+ "test" : " xo && ava && tsd "
17
17
},
18
18
"files" : [
19
- " index.js"
19
+ " index.js" ,
20
+ " index.d.ts"
20
21
],
21
22
"keywords" : [
22
23
" unique" ,
30
31
" consecutively"
31
32
],
32
33
"devDependencies" : {
33
- "ava" : " *" ,
34
- "xo" : " *"
34
+ "ava" : " ^1.4.1" ,
35
+ "tsd" : " ^0.7.2" ,
36
+ "xo" : " ^0.24.0"
35
37
}
36
38
}
Original file line number Diff line number Diff line change @@ -16,16 +16,16 @@ $ npm install unique-random
16
16
17
17
``` js
18
18
const uniqueRandom = require (' unique-random' );
19
- const rand = uniqueRandom (1 , 10 );
19
+ const random = uniqueRandom (1 , 10 );
20
20
21
- console .log (rand (), rand (), rand ());
21
+ console .log (random (), random (), random ());
22
22
// => 5 2 6
23
23
```
24
24
25
25
26
26
## API
27
27
28
- ### uniqueRandom(min, max )
28
+ ### uniqueRandom(minimum, maximum )
29
29
30
30
Returns a function that when called will return a random number that is never the same as the previous.
31
31
Original file line number Diff line number Diff line change 1
1
import test from 'ava' ;
2
- import m from '.' ;
2
+ import uniqueRandom from '.' ;
3
3
4
4
test ( 'main' , t => {
5
- const uniqueRandom = m ( 1 , 10 ) ;
5
+ const random = uniqueRandom ( 1 , 10 ) ;
6
6
let count = 1000 ;
7
- let current ;
8
- let prev ;
7
+ let currentValue ;
8
+ let previousValue ;
9
9
10
10
while ( -- count > 0 ) {
11
- current = uniqueRandom ( ) ;
11
+ currentValue = random ( ) ;
12
12
13
- if ( current === prev || current > 10 || current < 1 ) {
13
+ if (
14
+ currentValue === previousValue ||
15
+ currentValue > 10 ||
16
+ currentValue < 1
17
+ ) {
14
18
t . fail ( ) ;
15
19
}
16
20
17
- prev = current ;
21
+ previousValue = currentValue ;
18
22
}
19
23
20
24
t . pass ( ) ;
You can’t perform that action at this time.
0 commit comments