-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.js
33 lines (27 loc) · 1.09 KB
/
String.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
String.implement({
// replaces all occurences of KEY with VALUE and returns the result
// data = {"KEY1": "VALUE1", "KEY2": "VALUE2", ... , "KEYn": "VALUEn"}
encode: function(data){
return this.replace(new RegExp(Hash.getKeys(data).map(String.escapeRegExp).join('|'), 'g'), Hash.prototype.get.bind(data));
},
// Works like String.encode but in reverse
decode: function(data){
return this.encode(Hash.getKeys(data).reverse().associate(Hash.getValues(data).reverse()));
},
// PHP`s strtr(string, string, string) like function
translate: function(from, to){
return this.replace(new RegExp(Array.map(from, String.escapeRegExp).join('|'), 'g'), function(chr){
return to[from.indexOf(chr)];
});
},
rot13: function(){
return this.replace(/[a-z]/gi, function(chr){
var code = chr.charCodeAt(0);
var base = (code < 91) ? 65 : 97;
return String.fromCharCode((code - base + 13) % 26 + base);
});
}
});
var htmlEntities = {'ä': 'ä', 'ö': 'ö', 'ü': 'ü'};
alert('Grüne Bäume.'.encode(htmlEntities));
alert('all your base are belong to us'.translate('aeiou', 'AEIOU'));