Skip to content

Instantly share code, notes, and snippets.

@jpwilliams
Last active November 23, 2016 17:05
Show Gist options
  • Save jpwilliams/9d223efbd83c445df794a80a450e8098 to your computer and use it in GitHub Desktop.
Save jpwilliams/9d223efbd83c445df794a80a450e8098 to your computer and use it in GitHub Desktop.
Scramble strings, retaining their readability. This is pointless.
function scramble (message) {
message = message || ''
var ret = ''
var re = /([a-zA-Z]+)|([^a-zA-Z]+)/g
var match
while (match = re.exec(message)) {
if (match[2]) {
ret += match[2]
continue
}
if (match[1].length < 4) {
ret += match[1]
continue
}
var piece = match[1].slice(0, 1)
piece += match[1].slice(1, -1).split('').sort(() => {
return (0.5 - Math.random())
}).join('')
piece += match[1].slice(-1)
ret += piece
}
return ret
}
scramble('There is absolutely no reason anybody would ever want to do this, but I can do it.')
// Outputs:
// Trhee is absuoltely no raeson adnobyy would ever want to do tihs, but I can do it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment