Last active
May 22, 2021 12:43
-
-
Save Tsugami/30e51a3a4b757ba0a0ae517e0c3e6df6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isUpper = (str) => str === str.toUpperCase(); | |
const cloneArray = (arr) => [...arr]; | |
const cloneAndUpdateArray = (arr, value, index) => { | |
const newArr = cloneArray(arr); | |
newArr.splice(index, 1, value); | |
return newArr; | |
}; | |
const genCharChances = (words, chances, index, upper = false) => { | |
return chances.reduce((acc, cur) => { | |
const value = upper ? cur.toUpperCase() : cur; | |
const result = words.map((arr) => cloneAndUpdateArray(arr, value, index)); | |
return [...acc, ...result]; | |
}, []); | |
}; | |
const genChances = (input, prox) => { | |
return input | |
.split("") | |
.reduce((acc, cur, index, src) => { | |
const chances = prox[cur.toLowerCase()]; || [cur] | |
const words = [...acc, src]; | |
const result = genCharChances(words, chances, index, isUpper(cur)); | |
return acc.concat(result); | |
}, []) | |
.map((v) => v.join("")); | |
}; | |
const result = genChances("pPp", { p: ["o", "l"] }); | |
console.log("resultado", result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment