-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
95 lines (87 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var Random = require('random-js'),
xtend = require('xtend'),
traverse = require('traverse');
var random = new Random(Random.engines.mt19937().seed(0));
/**
* @param _ {number}
* @returns {null}
*/
module.exports.seed = function(_) {
random = new Random(Random.engines.mt19937().seed(_));
};
module.exports.mutate = {
object: mutateObject,
string: mutateString
};
function mutateObject(obj) {
return function generate() {
// copy the object so that modifications
// are not additive
var copy = xtend({}, obj);
traverse(copy).forEach(transformObjectValue);
return copy;
};
}
function transformObjectValue(val) {
if (random.bool(0.1)) {
mutateVal.call(this, val);
} else if (random.bool(0.05)) {
if (this.level) {
this.remove();
}
}
}
function mutateVal(val) {
switch(typeof val) {
case 'boolean':
this.update(!val);
break;
case 'number':
this.update(val + random.real(-1000, 1000));
break;
case 'string':
this.update(mutateString(val));
break;
default:
if (Array.isArray(val)) this.update(mutateArray(val));
break;
}
}
/**
* @param val {string} a string value
* @returns {string}
*/
function mutateString(val) {
var arr = val.split('');
if (random.bool(0.05)) {
arr = arr.reverse();
}
if (random.bool(0.25)) {
arr.splice(
random.integer(1, arr.length),
random.integer(1, arr.length));
}
if (random.bool(0.25)) {
var args = [random.integer(1, arr.length), 0]
.concat(random.string(random.integer(1, arr.length)).split(''));
arr.splice.apply(arr, args);
}
val = arr.join('');
return val;
}
/**
* @param val {array} an array
* @returns {array}
*/
function mutateArray(val) {
if (random.bool(0.1)) {
val = val.reverse();
}
if (random.bool(0.05)) {
val = val.slice(random.integer(0, val.length));
}
if (random.bool(0.05)) {
val = val.slice(0, random.integer(0, val.length));
}
return val;
}