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/. */
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
const make = require('../make')
|
|
|
|
const utils = require('../utils')
|
|
|
|
const random = require('../random')
|
|
|
|
|
|
|
|
class mutate extends utils {
|
|
|
|
static text (str) {
|
2017-06-08 04:15:20 +00:00
|
|
|
let mutator = function (m) {
|
2017-08-16 19:59:14 +00:00
|
|
|
return random.chance(4) ? m : make.text.any()
|
2017-06-08 04:15:20 +00:00
|
|
|
}
|
|
|
|
return str.replace(/[a-zA-Z]+?/g, mutator)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
2017-06-08 04:15:20 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
static numbers (str) {
|
2017-06-08 04:15:20 +00:00
|
|
|
let mutator = function (m) {
|
2017-08-16 19:59:14 +00:00
|
|
|
return random.chance(4) ? m : make.number.any()
|
2017-06-08 04:15:20 +00:00
|
|
|
}
|
|
|
|
return str.replace(/-?\d+(\.\d+)?/g, mutator)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
2017-06-08 04:15:20 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
static units (str) {
|
2017-08-16 19:57:57 +00:00
|
|
|
let mutator = function (m, p1) {
|
2017-08-16 19:59:14 +00:00
|
|
|
if (random.chance(4)) {
|
2017-08-16 19:57:57 +00:00
|
|
|
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)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
2017-06-08 04:15:20 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
static random (str) {
|
2017-06-08 04:15:20 +00:00
|
|
|
let mutator = function (m) {
|
2017-08-16 19:59:14 +00:00
|
|
|
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)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
2017-06-08 04:15:20 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
static any (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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
2018-03-19 04:54:40 +00:00
|
|
|
module.exports = mutate
|