This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var digits = '0123456789' + | |
'abcdefghijklmnopqrstuvwxyz' + | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + | |
'-_'; | |
var radix = digits.length; | |
function shorten_number(n) { | |
var s = ''; | |
while (n > 0) { | |
s = digits.charAt(n % radix) + s; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten2D(array) { | |
return array.reduce(function (result, item) { | |
return result.concat(item); | |
}, []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomString(length) { | |
var s = ((performance.now() % 1) + Math.random()).toString(35); | |
return s.substr(-Math.max(0, Math.min(length, s.length - 2))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function jenkins_hash(s) { | |
var hash = 0, i = 0, l = s.length; | |
for (; i < l; ++i) { | |
hash += s.charCodeAt(i); | |
hash += (hash << 10); | |
hash ^= (hash >> 6); | |
} | |
hash += (hash << 3); | |
hash ^= (hash >> 11); | |
hash += (hash << 15); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cachedAsyncFn(fn) { | |
var cached, queue; | |
return function (callback) { | |
if (cached) { | |
process.nextTick(function () { | |
callback.apply(null, cached); | |
}); | |
} else if (queue) { | |
queue.push(callback); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(n) { | |
return n < 2 ? n : fibonacci(n - 2) + fibonacci(n - 1); | |
} | |
function fibonacci(n) { | |
var prev, curr = 0, next = 1; | |
while (n-- > 0) { | |
prev = curr; | |
curr = next; | |
next = prev + curr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts degrees to radians. | |
* @param {number} angleDegrees Angle in degrees. | |
* @return {number} Angle in radians. | |
**/ | |
function degreesToRadians(angleDegrees) { | |
return angleDegrees * Math.PI / 180; | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fisher-Yates shuffle | |
function shuffle(array) { | |
for (var n = array.length, k, x; 1 < n;) { | |
k = Math.floor(Math.random() * n--); | |
x = array[n]; | |
array[n] = array[k]; | |
array[k] = x; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Takes a number and clamps it to within the provided bounds. | |
#define Clamp(_num, _min, _max) MIN((_max), MAX((_min), (_num))) | |
// Check if the two floats are equal. | |
#define FloatEqual(A, B) (ABS((A) - (B)) < FLT_EPSILON) | |
// Check if the two floats are NOT equal. | |
#define FloatNotEqual(A, B) (ABS((A) - (B)) > FLT_EPSILON) | |
// Check if the float is equal to zero. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// high-resolution time diff in milliseconds | |
function hrmstime(t) { | |
if (t) { | |
t = process.hrtime(t); | |
return (t[0] * 1e9 + t[1]) / 1e6; | |
} | |
return process.hrtime(); | |
} | |