-
Notifications
You must be signed in to change notification settings - Fork 3
/
rot.js
83 lines (74 loc) · 2.23 KB
/
rot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*! https://mths.be/rot v0.1.0 by @mathias | MIT license */
;(function(root) {
// Detect free variables `exports`
var freeExports = typeof exports == 'object' && exports;
// Detect free variable `module`
var freeModule = typeof module == 'object' && module &&
module.exports == freeExports && module;
// Detect free variable `global`, from Node.js or Browserified code,
// and use it as `root`
var freeGlobal = typeof global == 'object' && global;
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
root = freeGlobal;
}
/*--------------------------------------------------------------------------*/
var lowercase = 'abcdefghijklmnopqrstuvwxyz';
var uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var regexLowercase = /[a-z]/;
var regexUppercase = /[A-Z]/;
var rot = function(string, n) {
if (n == null) {
// use ROT-13 by default
n = 13;
}
n = Number(n);
string = String(string);
if (n == 0) {
return string;
}
if (n < 0) { // decode instead of encode
n += 26;
}
var length = string.length; // note: no need to account for astral symbols
var index = -1;
var result = '';
var character;
var currentPosition;
var shiftedPosition;
while (++index < length) {
character = string.charAt(index);
if (regexLowercase.test(character)) {
currentPosition = lowercase.indexOf(character);
shiftedPosition = (currentPosition + n) % 26;
result += lowercase.charAt(shiftedPosition);
} else if (regexUppercase.test(character)) {
currentPosition = uppercase.indexOf(character);
shiftedPosition = (currentPosition + n) % 26;
result += uppercase.charAt(shiftedPosition);
} else {
result += character;
}
}
return result;
};
rot.version = '0.1.0';
// Some AMD build optimizers, like r.js, check for specific condition patterns
// like the following:
if (
typeof define == 'function' &&
typeof define.amd == 'object' &&
define.amd
) {
define(function() {
return rot;
});
} else if (freeExports && !freeExports.nodeType) {
if (freeModule) { // in Node.js or RingoJS v0.8.0+
freeModule.exports = rot;
} else { // in Narwhal or RingoJS v0.7.0-
freeExports.rot = rot;
}
} else { // in Rhino or a web browser
root.rot = rot;
}
}(this));