Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:03
Show Gist options
  • Save st98/ff745a124c3a87ad1a31 to your computer and use it in GitHub Desktop.
Save st98/ff745a124c3a87ad1a31 to your computer and use it in GitHub Desktop.
ROT(13|47) の実装、変数の名前とかてけとー。
var rot = (function () {
function tr(str, from, to) {
return str.replace(new RegExp('[' + from + ']', 'g'), function (key) {
return to[from.indexOf(key)] || key;
});
}
function range(min, max) {
var result = '';
min = min.charCodeAt(0);
max = max.charCodeAt(0);
for (; min <= max; min++) {
result += String.fromCharCode(min);
}
return result;
}
var _rot13 = {
from: range('A', 'Z') + range('a', 'z'),
to: range('N', 'Z') + range('A', 'M') +
range('n', 'z') + range('a', 'm')
};
function rot13(str) {
return tr(str, _rot13.from, _rot13.to);
}
var _rot47 = {
from: range('!', '~'),
to: range('P', '~') + range('!', 'O')
};
function rot47(str) {
return tr(str, _rot47.from, _rot47.to);
}
return {
rot13: rot13,
rot47: rot47
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment