This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,6 @@
function rot13(c) {
return c.replace(/([a-m])|([n-z])/ig, function($0,$1,$2) {
return String.fromCharCode($1 ? $1.charCodeAt(0) + 13 : $2 ? $2.charCodeAt(0) - 13 : 0) || $0;
});
}
rot13("ABJURER nowhere") // NOWHERE abjurer

View file

@ -0,0 +1,59 @@
function rot13(value){
if (!value)
return "";
function singleChar(c) {
if (c.toUpperCase() < "A" || c.toUpperCase() > "Z")
return c;
if (c.toUpperCase() <= "M")
return String.fromCharCode(c.charCodeAt(0) + 13);
return String.fromCharCode(c.charCodeAt(0) - 13);
}
return _.map(value.split(""), singleChar).join("");
}
describe("Rot-13", function() {
it("Given nothing will return nothing", function() {
expect(rot13()).toBe("");
});
it("Given empty string will return empty string", function() {
expect(rot13("")).toBe("");
});
it("Given A will return N", function() {
expect(rot13("A")).toBe("N");
});
it("Given B will return O", function() {
expect(rot13("B")).toBe("O");
});
it("Given N will return A", function() {
expect(rot13("N")).toBe("A");
});
it("Given Z will return M", function() {
expect(rot13("Z")).toBe("M");
});
it("Given ZA will return MN", function() {
expect(rot13("ZA")).toBe("MN");
});
it("Given HELLO will return URYYB", function() {
expect(rot13("HELLO")).toBe("URYYB");
});
it("Given hello will return uryyb", function() {
expect(rot13("hello")).toBe("uryyb");
});
it("Given hello1 will return uryyb1", function() {
expect(rot13("hello1")).toBe("uryyb1");
});
});