-
Notifications
You must be signed in to change notification settings - Fork 55
/
00_prototype.js
executable file
·148 lines (116 loc) · 3.13 KB
/
00_prototype.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
if(typeof(update)=='undefined'){
function update(t, s){
for(var p in s)
t[p] = s[p];
return t;
}
}
Math.hypot = function(x, y){
return Math.sqrt(x*x + y*y);
}
Number.prototype.toHexString = function(){
return ('0' + this.toString(16)).slice(-2);
};
update(String.prototype, {
pad :function(len, ch){
len = len-this.length;
if(len<=0) return this;
return (ch || ' ').repeat(len) + this;
},
indent : function(num, c){
c = c || ' ';
return this.replace(/^/mg, c.repeat(num))
},
link: function(href){
return '<a href="' + href + '">' + this + '</a>';
},
trim : function(){
return this.replace(/^\s+|\s+$/g, '');
},
wrap : function(c){
return c+this+c;
},
repeat : function(n){
return new Array(n+1).join(this);
},
extract : function(re, group){
group = group==null? 1 : group;
var res = this.match(re);
return res ? res[group] : '';
},
decapitalize : function(){
return this.substr(0, 1).toLowerCase() + this.substr(1);
},
capitalize : function(){
return this.substr(0, 1).toUpperCase() + this.substr(1);
},
toByteArray : function(charset){
return new UnicodeConverter(charset).convertToByteArray(this, {});
},
md5 : function(charset){
var crypto = new CryptoHash(CryptoHash.MD5);
var data = this.toByteArray(charset);
crypto.update(data, data.length);
return crypto.finish(false).split('').map(function(char){
return char.charCodeAt().toHexString();
}).join('');
},
sha1 : function(charset){
var crypto = new CryptoHash(CryptoHash.SHA1);
var data = this.toByteArray(charset);
crypto.update(data, data.length);
return crypto.finish(true);
},
extract : function(re, group){
group = group==null? 1 : group;
var res = this.match(re);
return res ? res[group] : '';
},
convertToUnicode : function(charset){
return new UnicodeConverter(charset).ConvertToUnicode(this);
},
convertFromUnicode : function(charset){
return new UnicodeConverter(charset).ConvertFromUnicode(this);
},
trimTag : function(){
return this.replace(/<!--[\s\S]+?-->/gm, '').replace(/<[\s\S]+?>/gm, '');
},
includesFullwidth : function(){
return (/[^ -~¡-ß]/).test(this);
},
// http://code.google.com/p/kanaxs/
toHiragana : function(){
var c, i = this.length, a = [];
while(i--){
c = this.charCodeAt(i);
a[i] = (0x30A1 <= c && c <= 0x30F6) ? c - 0x0060 : c;
};
return String.fromCharCode.apply(null, a);
},
toKatakana : function(){
var c, i = this.length, a = [];
while(i--){
c = this.charCodeAt(i);
a[i] = (0x3041 <= c && c <= 0x3096) ? c + 0x0060 : c;
};
return String.fromCharCode.apply(null, a);
},
toRoma : function(){
var res = '';
var s = this.toKatakana();
for(var i = 0, kana, table = String.katakana ; i < s.length ; i += kana.length){
kana = s.substr(i, 2);
roma = table[kana];
if(!roma){
kana = s.substr(i, 1);
roma = table[kana];
}
if(!roma){
roma = kana;
}
res += roma;
}
res = res.replace(/ltu(.)/g, '$1$1');
return res;
},
});