-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (33 loc) · 1 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
'use strict'
// replace takes in the regex match and performs the splitting and random permutation selection
function replace (text, m) {
// split strings
let perms = m[2].split('|');
// generate random number via length
let rnd = randomNumber(0, perms.length);
// grab new item
let spun = perms[rnd];
// replace {surrounded} text with new item
text = text.replace(m[1], spun);
return text
}
// spin is the global function that we call to initiate the recursion
function spin(text) {
const regex = /(\{((([^\{\}]+)|())*)\})/;
let m;
// check if spins are still being found
if ((m = regex.exec(text)) !== null) {
// replace current text cluster and spin text
text = replace(text, m)
} else {
// return plain text
return text;
}
// spin again
return spin(text)
}
// dirty random number function
function randomNumber(min, max) {
return Math.floor(Math.random() * (+max - +min)) + +min;
}
module.exports = spin