RosettaCodeData/Task/Caesar-cipher/Jsish/caesar-cipher.jsish

34 lines
989 B
Plaintext

/* Caesar cipher, in Jsish */
"use strict";
function caesarCipher(input:string, key:number):string {
return input.replace(/([a-z])/g,
function(mat, p1, ofs, str) {
return Util.fromCharCode((p1.charCodeAt(0) + key + 26 - 97) % 26 + 97);
}).replace(/([A-Z])/g,
function(mat, p1, ofs, str) {
return Util.fromCharCode((p1.charCodeAt(0) + key + 26 - 65) % 26 + 65);
});
}
provide('caesarCipher', 1);
if (Interp.conf('unitTest')) {
var str = 'The five boxing wizards jump quickly';
; str;
; 'Enciphered:';
; caesarCipher(str, 3);
; 'Enciphered then deciphered';
; caesarCipher(caesarCipher(str, 3), -3);
}
/*
=!EXPECTSTART!=
str ==> The five boxing wizards jump quickly
'Enciphered:'
caesarCipher(str, 3) ==> Wkh ilyh eralqj zlcdugv mxps txlfnob
'Enciphered then deciphered'
caesarCipher(caesarCipher(str, 3), -3) ==> The five boxing wizards jump quickly
=!EXPECTEND!=
*/