2022-02-02 00:52:01 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const vowels = "aeiou".split("");
|
|
|
|
const consonants = "bcdfghjklmnpqrstvwxyz".split("");
|
|
|
|
|
|
|
|
function random_item(arr) {
|
|
|
|
return arr[Math.floor(Math.random() * arr.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function(word) {
|
|
|
|
const chars = word.toLowerCase().split("");
|
|
|
|
const targetpos = Math.floor(Math.random() * word.length);
|
|
|
|
|
2022-02-02 01:53:27 +00:00
|
|
|
let mode = "replace";
|
|
|
|
if(Math.random() < 0.1) mode = "add";
|
|
|
|
if(Math.random() > 0.9) mode = "remove";
|
2022-02-02 00:52:01 +00:00
|
|
|
|
2022-02-02 01:53:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-02 00:52:01 +00:00
|
|
|
return chars.join("");
|
|
|
|
}
|