-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.js
335 lines (305 loc) · 8.1 KB
/
utils.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'use strict';
// Fix console inconsistencies on browsers.
(function() {
if (!('console' in window)) {
window.console = {
log: function() {},
error: function() {},
};
} else if (!('bind' in window.console.log)) {
// native functions in IE9 might not have bind.
window.console.log = (function(fn) {
return function(msg) {
return fn(msg);
};
})(window.console.log);
window.console.error = (function(fn) {
return function(msg) {
return fn(msg);
};
})(window.console.error);
}
})();
JSSMS.Utils = {
/**
* Generate a random integer.
*
* @param {number} range Generate random numbers from 0 to range.
* A range of 4 would generate numbers between 0 and 3.
* @return {number} A random integer.
*/
rndInt: function(range) {
return Math.round(Math.random() * range);
},
Uint8Array: (function() {
/**
* @param {number|ArrayBufferView|Array.<number>|ArrayBuffer} length or array or buffer.
* @return {Uint8Array}
*/
if (SUPPORT_TYPED_ARRAYS) {
return Uint8Array;
} else {
/**
* @param {number} length
* @return {Array}
*/
return Array;
}
})(),
Uint16Array: (function() {
/**
* @param {number|ArrayBufferView|Array.<number>|ArrayBuffer} length or array or buffer.
* @return {Uint16Array}
*/
if (SUPPORT_TYPED_ARRAYS) {
return Uint16Array;
} else {
/**
* @param {number} length
* @return {Array}
*/
return Array;
}
})(),
/**
* Simple polyfill for DataView and ArrayBuffer.
* \@todo We must use Uint8Array for browsers supporting them but not DataView.
*/
Array: (function() {
if (SUPPORT_DATAVIEW) {
/**
* @param {number} length
* @return {DataView}
*/
return function(length) {
return new DataView(new ArrayBuffer(length));
};
} else {
/**
* @param {number} length
* @return {Array}
*/
return Array;
}
})(),
/**
* Copies an array from the specified source array, beginning at the
* specified position, to the specified position of the destination array.
*/
copyArrayElements: (function() {
if (SUPPORT_DATAVIEW) {
/**
* @param {DataView} src The source DataView.
* @param {number} srcPos The specified position of the source array.
* @param {DataView} dest The destination DataView.
* @param {number} destPos The specified position of the destination array.
* @param {number} length The length of the source array portion to copy.
*/
return function(src, srcPos, dest, destPos, length) {
while (length--) {
dest.setInt8(destPos + length, src.getInt8(srcPos + length));
}
};
} else {
/**
* @param {Array.<number>} src The source array.
* @param {number} srcPos The specified position of the source array.
* @param {Array.<number>} dest The destination array.
* @param {number} destPos The specified position of the destination array.
* @param {number} length The length of the source array portion to copy.
*/
return function(src, srcPos, dest, destPos, length) {
while (length--) {
dest[destPos + length] = src[srcPos + length];
}
};
}
})(),
/**
* A proxy for console that is activated in DEBUG mode only.
*/
console: {
log: (function() {
if (DEBUG) {
return window.console.log.bind(window.console);
}
return function(var_args) {};
})(),
error: (function() {
if (DEBUG) {
return window.console.error.bind(window.console);
}
return function(var_args) {};
})(),
/**
* @todo Develop a polyfill for non supporting browsers like IE.
*/
time: (function() {
if (DEBUG && window.console.time) {
return window.console.time.bind(window.console);
}
return function(label) {};
})(),
/**
* @todo Develop a polyfill for non supporting browsers like IE.
*/
timeEnd: (function() {
if (DEBUG && window.console.timeEnd) {
return window.console.timeEnd.bind(window.console);
}
return function(label) {};
})(),
},
/**
* Apply a function recursively on an object and its children.
*
* @param {Object} object
* @param {Function} fn
* @return {Object} object.
*/
traverse: function(object, fn) {
var key, child;
/*// Return false to stop the recursive process.
if ( === false) {
return;
}*/
fn.call(null, object);
for (key in object) {
if (object.hasOwnProperty(key)) {
child = object[key];
if (Object(child) === child) {
object[key] = JSSMS.Utils.traverse(child, fn);
}
}
}
return object;
},
/**
* Return the current timestamp in a fast way.
*
* @return {number} The current timestamp.
*/
getTimestamp: (function() {
if (window.performance && window.performance.now) {
return window.performance.now.bind(window.performance);
} else {
return function() {
return new Date().getTime();
};
}
})(),
/**
* Get a hex from a decimal. Pad with 0 if necessary.
*
* @param {number} dec A decimal integer.
* @return {string} A hex representation of the input.
*/
toHex: function(dec) {
var minus = dec < 0;
var hex = Math.abs(dec)
.toString(16)
.toUpperCase();
if (hex.length % 2) {
hex = '0' + hex;
}
if (minus) {
return '-0x' + hex;
}
return '0x' + hex;
},
/**
* Determine support and prefix of HTML5 features. Returns the prefix of the
* implementation, or false otherwise.
*
* @param {Array.<string>} arr An array of prefixes.
* @param {Object=} obj An object to check the prefix against, default to `window.document`.
* @return {string|boolean} The implementation prefix or false.
*/
getPrefix: function(arr, obj) {
var prefix = false;
if (obj === undefined) {
obj = document;
}
arr.some(function(prop) {
if (prop in obj) {
prefix = prop;
return true;
}
return false;
});
return prefix;
},
/**
* Given a file name, returns the extension.
*
* @param {string} fileName The filename, possibly including a path.
* @return {string} The extension of the file without the leading dot.
*/
getFileExtension: function(fileName) {
if (typeof fileName !== 'string') {
return '';
}
return fileName
.split('.')
.pop()
.toLowerCase();
},
/**
* Given a file name, returns the filename, without path or extension.
*
* @param {string} fileName The filename, possibly including a path.
* @return {string} The filename of the file.
*/
getFileName: function(fileName) {
if (typeof fileName !== 'string') {
return '';
}
var parts = fileName.split('.');
parts.pop();
return parts
.join('.')
.split('/')
.pop();
},
/**
* CRC32 algorithm.
* @see http://stackoverflow.com/questions/18638900/javascript-crc32
* @see http://jsperf.com/js-crc32
*
* @param {string} str
* @returns {number}
*/
crc32: function(str) {
// Lazy initialisation pattern by David Bruant (@DavidBruant).
var crcTable = (function makeCRCTable() {
var c = 0;
var crcTable = new Uint32Array(256);
for (var n = 0; n < 256; n++) {
c = n;
for (var k = 0; k < 8; k++) {
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
crcTable[n] = c;
}
return crcTable;
})();
this.crc32 = function(str) {
var crc = 0 ^ -1;
for (var i = 0; i < str.length; i++) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xff];
}
return (crc ^ -1) >>> 0;
};
return this.crc32(str);
},
/**
* Return true if current browser is IE. Not used at the moment.
*
* @return {boolean}
*/
isIE: function() {
return (
/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)
);
},
};