Merge: Windows: fix bootstrap
[nit.git] / share / nitdoc / js / lib / utils.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 /*
20 * Utils module
21 */
22 String.prototype.startsWith = function(prefix, caseSensitive) {
23 if(caseSensitive) {
24 return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0;
25 }
26 return this.indexOf(prefix) === 0;
27 }
28
29 // Compare two strings using Sorensen-Dice Coefficient
30 // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
31 String.prototype.dice = function(other) {
32 var length1 = this.length - 1;
33 var length2 = other.length - 1;
34 if(length1 < 1 || length2 < 1) return 0;
35
36 var bigrams2 = [];
37 for(var i = 0; i < length2; i++) {
38 bigrams2.push(other.substr(i, 2));
39 }
40
41 var intersection = 0;
42 for(var i = 0; i < length1; i++) {
43 var bigram1 = this.substr(i, 2);
44 for(var j = 0; j < length2; j++) {
45 if(bigram1 == bigrams2[j]) {
46 intersection++;
47 bigrams2[j] = null;
48 break;
49 }
50 }
51 }
52 return (2.0 * intersection) / (length1 + length2);
53 }
54
55 /* base64 */
56
57 String.prototype._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
58
59 // public method for encoding
60 String.prototype.base64Encode = function () {
61 var output = "";
62 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
63 var i = 0;
64
65 input = this._utf8_encode();
66
67 while (i < input.length) {
68
69 chr1 = input.charCodeAt(i++);
70 chr2 = input.charCodeAt(i++);
71 chr3 = input.charCodeAt(i++);
72
73 enc1 = chr1 >> 2;
74 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
75 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
76 enc4 = chr3 & 63;
77
78 if (isNaN(chr2)) {
79 enc3 = enc4 = 64;
80 } else if (isNaN(chr3)) {
81 enc4 = 64;
82 }
83
84 output = output +
85 this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
86 this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
87
88 }
89 return output;
90 };
91
92 // public method for decoding
93 String.prototype.base64Decode = function () {
94 var output = "";
95 var chr1, chr2, chr3;
96 var enc1, enc2, enc3, enc4;
97 var i = 0;
98
99 input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
100
101 while (i < input.length) {
102
103 enc1 = this._keyStr.indexOf(input.charAt(i++));
104 enc2 = this._keyStr.indexOf(input.charAt(i++));
105 enc3 = this._keyStr.indexOf(input.charAt(i++));
106 enc4 = this._keyStr.indexOf(input.charAt(i++));
107
108 chr1 = (enc1 << 2) | (enc2 >> 4);
109 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
110 chr3 = ((enc3 & 3) << 6) | enc4;
111
112 output = output + String.fromCharCode(chr1);
113
114 if (enc3 != 64) {
115 output = output + String.fromCharCode(chr2);
116 }
117 if (enc4 != 64) {
118 output = output + String.fromCharCode(chr3);
119 }
120
121 }
122 return output._utf8_decode();;
123 };
124
125 // private method for UTF-8 encoding
126 String.prototype._utf8_encode = function () {
127 string = this.replace(/\r\n/g,"\n");
128 var utftext = "";
129
130 for (var n = 0; n < string.length; n++) {
131
132 var c = string.charCodeAt(n);
133
134 if (c < 128) {
135 utftext += String.fromCharCode(c);
136 }
137 else if((c > 127) && (c < 2048)) {
138 utftext += String.fromCharCode((c >> 6) | 192);
139 utftext += String.fromCharCode((c & 63) | 128);
140 }
141 else {
142 utftext += String.fromCharCode((c >> 12) | 224);
143 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
144 utftext += String.fromCharCode((c & 63) | 128);
145 }
146
147 }
148 return utftext;
149 };
150
151 // private method for UTF-8 decoding
152 String.prototype._utf8_decode = function () {
153 var string = "";
154 var i = 0;
155 var c = c1 = c2 = 0;
156
157 while ( i < this.length ) {
158
159 c = this.charCodeAt(i);
160
161 if (c < 128) {
162 string += String.fromCharCode(c);
163 i++;
164 }
165 else if((c > 191) && (c < 224)) {
166 c2 = this.charCodeAt(i+1);
167 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
168 i += 2;
169 }
170 else {
171 c2 = this.charCodeAt(i+1);
172 c3 = this.charCodeAt(i+2);
173 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
174 i += 3;
175 }
176
177 }
178 return string;
179 };
180
181 // JQuery Case Insensitive :icontains selector
182 $.expr[':'].icontains = function(obj, index, meta, stack){
183 return (obj.textContent.replace(/\[[0-9]+\]/g, "") || obj.innerText.replace(/\[[0-9]+\]/g, "") || jQuery(obj).text().replace(/\[[0-9]+\]/g, "") || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
184 };
185
186 var Utils = {
187 // Extract anchor part (after #) from URL string
188 extractAnchor: function(url) {
189 var index = url.indexOf("#");
190 if (index >= 0) {
191 return url.substring(index + 1);
192 }
193 return null;
194 },
195
196 delayEvent: function(handler, event) {
197 if(this.delayEvent.timeout) {
198 clearTimeout(this.delayEvent.timeout);
199 }
200 this.delayEvent.timeout = setTimeout(function() {
201 handler.call(event);
202 }, 50);
203 }
204 };