-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.js
225 lines (201 loc) · 5.74 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
/**
* jsSMS - A Sega Master System/Game Gear emulator in JavaScript
* Copyright (C) 2012 Guillaume Marty (https://github.com/gmarty)
* Based on JavaGear Copyright (c) 2002-2008 Chris White
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
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);
},
/**
* 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: {
/**
* @param {...string} var_args
*/
log: function(var_args) {
if (DEBUG)
window.console.log.apply(window.console, arguments);
},
/**
* @param {...string} var_args
*/
error: function(var_args) {
if (DEBUG)
window.console.error.apply(window.console, arguments);
},
/**
* @param {string} label
*/
time: function(label) {
if (DEBUG)
window.console.time(label);
},
/**
* @param {string} label
*/
timeEnd: function(label) {
if (DEBUG)
window.console.timeEnd(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 (typeof child === 'object' && child !== null) {
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 function() {
return window.performance.now();
}
} 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 hex = (dec).toString(16).toUpperCase();
if (hex.length % 2) {
hex = '0' + 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;
},
/**
* 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));
}
};