Nodejs String Rot13 rot13(s)

Here you can find the source of rot13(s)

Method Source Code

// Encrypt a text file using ROT-13 substitution cipher

// http://www.google.com/search?q=rot13+javascript+stackoverflow
String.prototype.rot13 = function(s)
 {
    return (s ? s : this).split('').map(function(_)
     {/*from  w  w  w  . j  a va  2s.  c  om*/
        if (!_.match(/[A-za-z]/)) return _;
        c = Math.floor(_.charCodeAt(0) / 97);
        k = (_.toLowerCase().charCodeAt(0) - 83) % 26 || 26;
        return String.fromCharCode(k + ((c == 0) ? 64 : 96));
     }).join('');
 }

if (system.args.length !== 3) {
    system.print("Usage: rot13.js inputfile outputfile");
    system.exit(-1);
}

if (system.args[1] === system.args[2]) {
    system.print("Output file can't be the same as input file");
    system.exit(-1);
}

try {
    var input = fs.open(system.args[1], 'r');
    var output = fs.open(system.args[2], 'w');
    try {
        while (true)
            output.writeLine(input.next().rot13());
    }
    catch (e) { } // EOF exception
}
catch (err) {
    system.print("FAIL:", err);
    system.exit(-1);
}
finally {
    input.close();
    output.close();
}

Related

  1. rot13()
    String.prototype.rot13 = function(){
      return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
      });
    };
    
  2. rot13()
    var mail = "asdf@asdf.pbz"
    String.prototype.rot13 = function(){
      return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
      });
    };
    
  3. rot13()
    String.prototype.rot13 = function(){
        return this.replace(/[a-zA-Z]/g, function(c){
            return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
        });
    };
    
  4. rot13()
    String.prototype.rot13 = function () {
      return this.replace(/[a-zA-Z]/g, function (c) {
        return String.fromCharCode(((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26));
      });
    };
    var includeEmailLink = function () {
      var anchor = document.getElementById('hello');
      var href = anchor.getAttribute('href');
      return anchor.setAttribute('href', href.rot13());
    ...
    
  5. rot13()
    String.prototype.rot13 = function(){
      return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
      });
    };
    
  6. rot13(str)
    function rot13(str) { 
        var arr = [];
        for (var i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90) {
                arr.push(str.charAt(i));
            } else if (str.charCodeAt(i) > 77) {
                arr.push(String.fromCharCode(str.charCodeAt(i) - 13));
            } else {
                arr.push(String.fromCharCode(str.charCodeAt(i) + 13));
    ...
    
  7. rot13(str)
    function rot13(str) { 
      var codeZ ='Z'.charCodeAt(0);
      var codeA ='A'.charCodeAt(0);
      var arrStr =[];
      var strarr =[];
      var codeChar;
      console.log(codeZ);
      console.log(codeA);
      for (var i = 0; i < str.length; i++){
    ...