mutate: add 10% change of adding and 10% of removing
This commit is contained in:
parent
ec58e75d83
commit
277b500f21
1 changed files with 33 additions and 17 deletions
|
@ -8,26 +8,42 @@ function random_item(arr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function(word) {
|
export default function(word) {
|
||||||
console.log(`***** MUTATE *****`)
|
|
||||||
const chars = word.toLowerCase().split("");
|
const chars = word.toLowerCase().split("");
|
||||||
const targetpos = Math.floor(Math.random() * word.length);
|
const targetpos = Math.floor(Math.random() * word.length);
|
||||||
const targetchar = chars[targetpos];
|
|
||||||
console.log("TARGET", targetchar, "POS", targetpos);
|
|
||||||
let pool = consonants.concat(vowels);
|
|
||||||
if(vowels.includes(targetchar))
|
|
||||||
pool = vowels;
|
|
||||||
if(consonants.includes(targetchar))
|
|
||||||
pool = consonants;
|
|
||||||
|
|
||||||
pool = [...pool]; // Shallow copy to avoid splice mutating the original array
|
let mode = "replace";
|
||||||
console.log("POOL BEFORE", pool);
|
if(Math.random() < 0.1) mode = "add";
|
||||||
if(pool.includes(targetchar)) {
|
if(Math.random() > 0.9) mode = "remove";
|
||||||
console.log("REMOVING TARGET");
|
|
||||||
pool.splice(pool.indexOf(targetchar), 1);
|
switch(mode) {
|
||||||
|
case "replace":
|
||||||
|
const targetchar = chars[targetpos];
|
||||||
|
console.log("TARGET", targetchar, "POS", targetpos);
|
||||||
|
let pool = consonants.concat(vowels);
|
||||||
|
if(vowels.includes(targetchar))
|
||||||
|
pool = vowels;
|
||||||
|
if(consonants.includes(targetchar))
|
||||||
|
pool = consonants;
|
||||||
|
|
||||||
|
pool = [...pool]; // Shallow copy to avoid splice mutating the original array
|
||||||
|
console.log("POOL BEFORE", pool);
|
||||||
|
if(pool.includes(targetchar)) {
|
||||||
|
console.log("REMOVING TARGET");
|
||||||
|
pool.splice(pool.indexOf(targetchar), 1);
|
||||||
|
}
|
||||||
|
console.log("POOL AFTER", pool);
|
||||||
|
|
||||||
|
let newchar = random_item(pool);
|
||||||
|
chars.splice(targetpos, 1, newchar);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "add":
|
||||||
|
chars.splice(targetpos, 0, random_item(consonants.concat(vowels)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "remove":
|
||||||
|
chars.splice(targetpos, 1);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
console.log("POOL AFTER", pool);
|
|
||||||
|
|
||||||
let newchar = random_item(pool);
|
|
||||||
chars.splice(targetpos, 1, newchar);
|
|
||||||
return chars.join("");
|
return chars.join("");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue