JavaScript str_rot13
Perform the rot13 transform on a string
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 20 | function str_rot13 (str) { // Perform the rot13 transform on a string // // version: 1008.1718 // discuss at: http://phpjs.org/functions/str_rot13 // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Ates Goral (http://magnetiq.com) // + bugfixed by: Onno Marsman // + improved by: Rafał Kukawski (http://blog.kukawski.pl) // * example 1: str_rot13('Kevin van Zonneveld'); // * returns 1: 'Xriva ina Mbaariryq' // * example 2: str_rot13('Xriva ina Mbaariryq'); // * returns 2: 'Kevin van Zonneveld' // * example 3: str_rot13(33); // * returns 3: '33' return (str+'').replace(/[a-z]/gi, function(s){ return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()<'n'?13:-13)); }); } |
Examples
» Example 1
Running
1 | str_rot13('Kevin van Zonneveld'); |
Should return
1 | 'Xriva ina Mbaariryq' |
» Example 2
Running
1 | str_rot13('Xriva ina Mbaariryq'); |
Should return
1 | 'Kevin van Zonneveld' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have str_rot13 goodness in JavaScript.
I forgot to cast the input value to string, please change
str.replace(/[a-z]/gi
to
(str + '').replace(/[a-z]/gi
I think it's important.
@ Rafał Kukawski: Excellent work! Tests pass, we fixes jslint issue, and and function is tighter. Thanks a lot!
http://github.com/kvz/phpjs/commit/f4fcf895c16ecce83550efc9c190c82ed118c06c
@ Rafał Kukawski: Excellent work! Tests pass, we fixes jslint issue, and and function is tighter. Thanks a lot!
http://github.com/kvz/phpjs/commit/f4fcf895c16ecce83550efc9c190c82ed118c06c
My proposition (uses the conditional operator)
function rot13(x){
return x.replace(/[a-z]/gi,function(s){
return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()<'n'?13:-13))})
}
@ Eric & Brett Zamir: Both sites (php.js & my blog) have a pagerank of 5 currently. I guess it's just a matter of time for PHP.JS to receive more backlinks from other sites & get a higher ranking for people who want to find our javascript snippets.
Not sure why it didn't make it, though at least str_rot13 with or without JavaScript made it to the first page. Thanks to your prompting, I went ahead and did a little further SEO investigation of the site and sent the recommendations to Kevin (though he's already done a fine job of bringing his site to sometimes even rival the PHP site when you search for PHP functions)
Great!, only like 3 lines of code, I like this alot and its just what I'm looking for. Why on earth is this page not listed on Google for #1 spot of "rot13 javascript"?, on Google all I could find where old rot13 implementations that has like 40 lines of code written years ago using schemes for javascript 1.0. Glad I found this website! Thanks alot.
Alternative implementation that doesn't use conditionals:
[CODE="Javascript"]
function str_rot13(str) {
// * example 1: str_rot13("Hello World!");
// * returns 1: "Uryyb Jbeyq!"
// * example 2: str_rot13(str_rot13("Hello World!"));
// * returns 2: "Hello World!"
return str.replace(/[A-Za-z]/g, function (c) { return String.fromCharCode((((c = c.charCodeAt(0)) & 223) - 52) % 26 + (c & 32) + 65); });
}
[/CODE]


Kevin van Zonneveld
Apr 23rd
http://github.com/kvz/phpjs/commit/0341548f49543d24a4f33f3261f5d844c3e12bbb