-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
47 lines (38 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let getRandomValues = require("./random");
module.exports = ({
syllablesCount = 3,
minSyllableLength = 2,
maxSyllableLength = 3,
hasNumbers = true,
titlecased = true,
separators = "",
vowels = "aeiouy",
consonants = "bcdfghjklmnpqrstvwxz"
} = {}) =>
produce(syllablesCount, i => {
let length =
minSyllableLength + random(maxSyllableLength - minSyllableLength + 1);
let syllable = produce(length, index => {
let alpha = index % 2 ? vowels : consonants;
let char = alpha[random(alpha.length)];
return titlecased && !index ? char.toUpperCase() : char;
});
if (hasNumbers) syllable += random(10);
return i && separators
? separators[random(separators.length)] + syllable
: syllable;
});
let produce = (number, callback) => {
for (var i = 0, result = ""; i < number; i++) result += callback(i);
return result;
};
let buffer = [];
let bufferSize = 0xffff;
let bufferIndex = bufferSize;
let random = limit => {
if (bufferIndex >= bufferSize) {
buffer = getRandomValues(bufferSize);
bufferIndex = 0;
}
return buffer[bufferIndex++] % limit;
};