Skip to content

Instantly share code, notes, and snippets.

@furf
Last active April 2, 2019 23:44
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 furf/8b22ed39c861ad17c0685b836754d4d8 to your computer and use it in GitHub Desktop.
Save furf/8b22ed39c861ad17c0685b836754d4d8 to your computer and use it in GitHub Desktop.
function reduceRot13Cipher(map, c, i, abc) {
const off = 13;
const len = abc.length;
const j = (i + off) % len;
map[c] = abc[j];
return map;
}
const rot13 = (() => {
const abc = 'abcdefghijklmnopqrstuvwxyz';
const map = {};
abc.split('').reduce(reduceRot13Cipher, map);
abc.toUpperCase().split('').reduce(reduceRot13Cipher, map);
return function rot13(msg) {
const dec = msg.split('');
const enc = dec.map(c => (c in map) ? map[c] : c);
return enc.join('');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment