octo-deno/lib/utils/mutate.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-06-08 04:15:20 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
utils.mutate = {
text: function (str) {
let mutator = function (m) {
return random.chance(4) ? m : make.text.any()
2017-06-08 04:15:20 +00:00
}
return str.replace(/[a-zA-Z]+?/g, mutator)
},
numbers: function (str) {
let mutator = function (m) {
return random.chance(4) ? m : make.number.any()
2017-06-08 04:15:20 +00:00
}
return str.replace(/-?\d+(\.\d+)?/g, mutator)
},
units: function (str) {
let mutator = function (m, p1) {
if (random.chance(4)) {
return m
} else {
return p1 + make.unit.unit()
}
2017-06-08 04:15:20 +00:00
}
2017-08-16 20:16:29 +00:00
return str.replace(/(\d+)(px|em|ex|ch|rem|mm|cm|in|pt|pc|%')/g, mutator)
2017-06-08 04:15:20 +00:00
},
random: function (str) {
let mutator = function (m) {
if (random.chance(20)) {
2017-06-08 04:15:20 +00:00
if (str.match(/[0-9]/g)) {
return make.number.any()
} else {
return make.text.any()
}
} else {
return m
}
}
return str.replace(/./g, mutator)
},
any: function (str) {
2017-06-08 04:18:33 +00:00
switch (random.number(4)) {
2017-06-08 04:15:20 +00:00
case 0:
return utils.mutate.text(str)
case 1:
return utils.mutate.numbers(str)
case 2:
return utils.mutate.units(str)
2017-06-08 04:18:33 +00:00
case 3:
return utils.mutate.random(str)
2017-06-08 04:15:20 +00:00
}
}
}