Add shuffle-chars and shuffle-words transformers.
Chris Pressey
10 years ago
108 | 108 | transformer['repeat-chars'] = module.exports; |
109 | 109 | module.exports = { |
110 | 110 | makeTransformer: function(cfg) { |
111 | return function(str, state) { | |
112 | var s = ""; | |
113 | var len = str.length; | |
114 | for (var i = 0; i < len; i++) { | |
115 | var index = Math.floor(Math.random() * str.length); | |
116 | s += str.charAt(index); | |
117 | str = str.slice(0, index) + str.slice(index + 1, str.length); | |
118 | } | |
119 | return s; | |
120 | }; | |
121 | }, | |
122 | parameters: {}, | |
123 | description: "Randomly re-order all characters found" | |
124 | }; | |
125 | ||
126 | transformer['shuffle-chars'] = module.exports; | |
127 | module.exports = { | |
128 | makeTransformer: function(cfg) { | |
129 | return function(str, state) { | |
130 | var words = str.split(/\s+/); | |
131 | var acc = []; | |
132 | var len = words.length; | |
133 | for (var i = 0; i < len; i++) { | |
134 | var index = Math.floor(Math.random() * words.length); | |
135 | acc.push(words[index]); | |
136 | words = words.slice(0, index).concat(words.slice(index + 1, words.length)); | |
137 | } | |
138 | return acc.join(' '); | |
139 | }; | |
140 | }, | |
141 | parameters: {}, | |
142 | description: "Randomly re-order all words found" | |
143 | }; | |
144 | ||
145 | transformer['shuffle-words'] = module.exports; | |
146 | module.exports = { | |
147 | makeTransformer: function(cfg) { | |
111 | 148 | cfg.chance = parseInt(cfg.chance || "100", 10); |
112 | 149 | return function(str, state) { |
113 | 150 | var s = ""; |