git @ Cat's Eye Technologies Lexeduct / 94318de
Add replace-chars transformer. Chris Pressey 6 years ago
2 changed file(s) with 27 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1717 'chars': ["The set of characters to remove", ""],
1818 'chance': ["Probability (0-100) of applying to any individual character", "100"]
1919 },
20 description: "Remove all occurrences of the specified characters"
20 description: "Remove occurrences of the specified characters"
2121 };
0 module.exports = {
1 makeTransformer: function(cfg) {
2 cfg.chance = parseInt(cfg.chance || "100", 10);
3 return function(str, state) {
4 var s = "";
5 for (var i = 0; i < str.length; i++) {
6 var c = str.charAt(i);
7 if (cfg.search.indexOf(c) > -1 &&
8 Math.floor(Math.random() * 100) < cfg.chance) {
9 s += cfg.replace.charAt(
10 Math.floor(Math.random() * cfg.replace.length)
11 );
12 } else {
13 s += c;
14 }
15 }
16 return s;
17 };
18 },
19 parameters: {
20 'search': ["The set of characters to look for", ""],
21 'replace': ["The set of characters to substitute in place", ""],
22 'chance': ["Probability (0-100) of applying to any individual character", "100"]
23 },
24 description: "Replace occurrences of the specified characters"
25 };