octo-deno/lib/utils/mutate.js

65 lines
1.5 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/. */
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) {
return random.chance(4) ? m : make.text.any()
2017-06-08 04:15:20 +00:00
}
return str.replace(/[a-zA-Z]+?/g, mutator)
}
2017-06-08 04:15:20 +00:00
static numbers (str) {
2017-06-08 04:15:20 +00:00
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)
}
2017-06-08 04:15:20 +00:00
static units (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
static random (str) {
2017-06-08 04:15:20 +00:00
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)
}
2017-06-08 04:15:20 +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 04:54:40 +00:00
module.exports = mutate