-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomtext.js
30 lines (22 loc) · 878 Bytes
/
randomtext.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
const REGEX_GROUP = /\[([^[]+?\|[^[]+?)\]/;
const DEBUG = false;
// ==============================================
function randomText(src) {
if (DEBUG) console.log(`call randomText: ${src}`);
if (!src || typeof src !== 'string') return src;
if (!src.match(REGEX_GROUP)) return src;
const regexResult = src.match(REGEX_GROUP);
if (DEBUG) console.log(`regex:`);
if (DEBUG) console.log(regexResult);
const fullMatch = regexResult[0];
const group = regexResult[1].split('|');
const idx = Math.round(Math.random() * (group.length -1));
const subst = group[idx].trim();
if (DEBUG) console.log(`group len: ${group.length}, random index: ${idx}`);
if (DEBUG) console.log(group);
if (DEBUG) console.log(`selected: '${subst}'`);
let res = src.replace(fullMatch, subst);
res = randomText(res);
return res;
}
module.exports.randomtext = randomText;