Skip to content

Commit 5ed6185

Browse files
Generator now only have one abstract method, while self handling the calculation of characters. Specific generators now implements getRandom.
1 parent 0d4ea44 commit 5ed6185

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed

src/Generator.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1+
import { randomBytes } from "crypto";
2+
13
export default class Generator {
4+
5+
/**
6+
* @internal
7+
* @abstract
8+
* @param {number} len Length of list.
9+
* @return {Uint8Array} List of random values.
10+
*/
11+
getRandom (len) {}
12+
213
/**
314
* Fetch a list of random char codes for alpha characters.
415
*
5-
* @abstract
616
* @param {number} len Amount of alpha characters to fetch.
7-
* @return {Array<number>} An array with char-codes (ascii) for the random alpha characters.
17+
* @return {Uint8Array} An array with char-codes (ascii) for the random alpha characters.
818
*/
9-
alpha (len) {}
19+
alpha (len) {
20+
return this.getRandom(len).map(i => (Math.round(i % 25) + (i > 128 ? 65 : 97)));
21+
}
1022

1123
/**
1224
* Fetch a list of random char codes of numeric characters.
1325
*
14-
* @abstract
1526
* @param {number} len Amount of number characters to fetch.
16-
* @return {Array<number>} An array with char-codes (ascii) for the random numeric characters.
27+
* @return {Uint8Array} An array with char-codes (ascii) for the random numeric characters.
1728
*/
18-
numbers (len) {}
29+
numbers (len) {
30+
return this.getRandom(len).map(i => (Math.round(i % 10) + 48));
31+
}
1932

2033

2134
/**
2235
* Fetch a list of random char codes of special characters.
2336
*
24-
* @abstract
2537
* @param {number} len Amount of special characters to fetch.
26-
* @return {Array<number>} An array with char-codes (ascii) for the random special characters.
38+
* @return {Uint8Array} An array with char-codes (ascii) for the random special characters.
2739
*/
28-
special (len) {}
40+
special (len) {
41+
return this.getRandom(len).map(i => {
42+
return i < 32
43+
? Math.round(i % 15) + 33
44+
: i < 65
45+
? Math.round(i % 7) + 58
46+
: i < 128
47+
? Math.round(i % 4) + 91
48+
: Math.round(i % 4) + 123;
49+
});
50+
}
2951
}

src/NodeGenerator.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Generator from './Generator';
2+
import { randomBytes } from 'crypto';
3+
4+
export default class NodeGenerator extends Generator {
5+
6+
getRandom (len) {
7+
return randomBytes(len);
8+
}
9+
}

src/WebGenerator.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Generator from './Generator';
2+
3+
export default class WebGenerator extends Generator {
4+
5+
getRandom (len) {
6+
const list = new Uint8Array(len);
7+
if (window.crypto) {
8+
window.crypto.getRandomValues(list);
9+
return list;
10+
}
11+
12+
// Fallback to random if no crypto on window.
13+
for (let i = 0; i < len; i++) {
14+
list[i] = Math.random();
15+
}
16+
17+
return list;
18+
}
19+
}

src/node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import rand from './rand';
2+
3+
const generator = () => {
4+
5+
6+
};
7+
8+
export default rand(generator);

src/rand.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { shuffle } from './Util'
2+
3+
export default (generator) => (length, options = {
4+
alpha: true,
5+
numbers: true,
6+
special: true,
7+
minAlpha: 1,
8+
minNumbers: 1,
9+
minSpecial: 1,
10+
}) => {
11+
12+
const lists = {
13+
alpha: (options.alpha ? generator.alpha(length) : []),
14+
special: (options.special ? generator.special(length) : []),
15+
numbers: (options.numbers ? generator.numbers(length) : []),
16+
combined: []
17+
};
18+
19+
const result = [];
20+
result.push(...lists.alpha.splice(0, options.minAlpha));
21+
result.push(...lists.special.splice(0, options.minSpecial));
22+
result.push(...lists.numbers.splice(0, options.numbers));
23+
24+
lists.combined = shuffle([].concat(
25+
...lists.alpha,
26+
...lists.special,
27+
...lists.numbers)
28+
);
29+
30+
// Concat rest.
31+
result.push(lists.combined.splice(0, length - (
32+
(options.special ? options.minSpecial : 0)
33+
+
34+
(options.alpha ? options.minAlpha : 0)
35+
+
36+
(options.numbers ? options.minNumbers : 0)
37+
)));
38+
39+
return shuffle(result);
40+
}
41+

src/web.js

Whitespace-only changes.

0 commit comments

Comments
 (0)