RosettaCodeData/Task/Rot-13/Jsish/rot-13.jsish

42 lines
1.2 KiB
Plaintext

#!/usr/local/bin/jsish
/* ROT-13 in Jsish */
function rot13(msg:string) {
return msg.replace(/([a-m])|([n-z])/ig, function(m,p1,p2,ofs,str) {
return String.fromCharCode(
p1 ? p1.charCodeAt(0) + 13 : p2 ? p2.charCodeAt(0) - 13 : 0) || m;
});
}
provide('rot13', Util.verConvert("1.0"));
/* rot13 command line utility */
if (isMain()) {
/* Unit testing */
if (Interp.conf('unitTest') > 0) {
; rot13('ABJURER nowhere 123!');
; rot13(rot13('Same old same old'));
return;
}
/* rot-13 of data lines from given filenames or stdin, to stdout */
function processFile(fname:string) {
var str;
if (fname == "stdin") fname = "./stdin";
if (fname == "-") fname = "stdin";
var fin = new Channel(fname, 'r');
while (str = fin.gets()) puts(rot13(str));
fin.close();
}
if (console.args.length == 0) console.args.push('-');
for (var fn of console.args) {
try { processFile(fn); } catch(err) { puts(err, "processing", fn); }
}
}
/*
=!EXPECTSTART!=
rot13('ABJURER nowhere 123!') ==> NOWHERE abjurer 123!
rot13(rot13('Same old same old')) ==> Same old same old
=!EXPECTEND!=
*/