Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created September 28, 2022 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleybliss/36b7b7ac351e76fffff2f45e73c80be2 to your computer and use it in GitHub Desktop.
Save wesleybliss/36b7b7ac351e76fffff2f45e73c80be2 to your computer and use it in GitHub Desktop.
Replace letters in a key with either uniform alpha or random letters
const randomInt = (from, to) => Math.floor(Math.random() * (to - from + 1) + from)
const rs = (input, random = false) => {
const len = input.length
const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
const lower = 'abcdefghijklmnopqrstuvwxyz'.split('')
let lastUpper = -1
let lastLower = -1
let output = ''
for (let i = 0; i < len; i++) {
const char = input.charAt(i)
if (!upper.includes(char) && !lower.includes(char)) {
output += char
continue
}
const isUpper = char === char.toUpperCase()
if (random) {
const ri = randomInt(0, upper.length - 1)
output += isUpper ? upper[ri] : lower[ri]
} else {
if (isUpper) {
lastUpper++
if (!upper[lastUpper])
lastUpper = 0
output += upper[lastUpper]
} else {
lastLower++
if (!lower[lastLower])
lastLower = 0
output += lower[lastLower]
}
}
}
return output
}
console.log(rs('irqrLyYWqBNjSDM3', true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment