Skip to content

Instantly share code, notes, and snippets.

View A1rPun's full-sized avatar
🌊
Fractalicious

Thom Bakker A1rPun

🌊
Fractalicious
  • The Netherlands
View GitHub Profile
@A1rPun
A1rPun / tests.js
Last active August 17, 2017 09:49
Waterfall on promise
waterfall(['a', 'bbbb', 'cc'], function(item){
return function(resolve){
setTimeout(function(){
console.log(item);
resolve();
}, item.length * 1000);
};
});
@A1rPun
A1rPun / getHashCode.js
Last active August 17, 2017 09:49
Javascript value equality
function getHashCode(obj) {
var hashCode = 0;
if (typeof obj === 'object')
for (var prop in obj) {
var code = getHashCode(prop) + getHashCode(obj[prop]);
hashCode += code * code;
}
else
for(var str = "" + obj, i = str.length; i--;)
hashCode += 100 / (i + str.charCodeAt(i));
@A1rPun
A1rPun / Timers.js
Last active August 17, 2017 09:50
setTimeout setInterval timer handler. Access them via `window.timers`
(function(w){
var oldST = w.setTimeout;
var oldSI = w.setInterval;
var oldCI = w.clearInterval;
var timers = [];
w.timers = timers;
w.setTimeout = function(fn, delay){
var id = oldST(function(){
fn && fn();
removeTimer(id);
@A1rPun
A1rPun / getParametersFromFunction.js
Last active August 17, 2017 09:52
Javascript get parameter names from function reference
function getParameters(func) {
var funcString = func.toString();
var indexStart = funcString.indexOf('(') + 1;
var indexEnd = funcString.indexOf(')');
return funcString
.substring(indexStart, indexEnd)
.trim()
.split(/\s*,\s*/);
}
@A1rPun
A1rPun / $q queue
Last active April 8, 2016 10:46
$q queue
var que = (function (q) {
/**
* Pause & queue a set of dynamically added async requests until the `resume` promise is resolved.
*
* @method que
* @param {Function} pause A function that returns a boolean which results the que instance to pause. Will be executed with every add() or resolved promise.
* @param {Function} resume A function that returns a promise which tells the que instance to resume when it resolves. It does nothing when the promise is rejected.
* @return {Object} que instance
*/
function que(pause, resume) {
@A1rPun
A1rPun / createObservedArray
Last active November 26, 2015 15:06
Create an observed array
/*
* @function createObservedArray
* @params function observer A callback function returning the name of the used Array function.
* @returns Array The observed array
* @note This is just a simple way to observe an array, do not use in production ;)
*/
function createObservedArray(observer) {
var array = [],
methods = ['pop', 'push', 'shift', 'unshift', 'slice', 'splice', 'reduce'];
for (var i = methods.length; i--;) {
@A1rPun
A1rPun / convertStringToInput
Created April 10, 2015 11:35
Convert a string to keys on your keyboard
function convertStringToInput(str){
var result = [];
for (var i = 0, l = str.length; i < l; i++) {
var charCode = str.charCodeAt(i);
result.push(charCode > 255 ? '+' + charCode.toString(16) : charCode);
}
return result.join(' ');
}
@A1rPun
A1rPun / Base10 to HEX color
Last active February 4, 2022 04:29
JavaScript convert base10 color to HEX color
/**
* Converts a BGR base10 color to an RGB Hex value.
*
* @method colorToHexString
* @param {Integer} dColor A color as integer.
* @returns {String} The RGB hex code as string.
*/
function colorToHexString(dColor) {
return '#' + ("000000" + (((dColor & 0xFF) << 16) + (dColor & 0xFF00) + ((dColor >> 16) & 0xFF)).toString(16)).slice(-6);
}
@A1rPun
A1rPun / Customizable Promise
Last active August 29, 2015 14:16
Customizable Promise
function Promise(o, fnNames){
fnNames = fnNames || ['done', 'fail', 'always'];
var done = fnNames[0],
fail = fnNames[1],
always = fnNames[2];
o.states = { none: 0 };
o.states[done] = 1;
o.states[fail] = 2;
@A1rPun
A1rPun / Batch colors (HEX)
Last active August 29, 2015 14:12
Use this in combination with .bat and the command `COLOR ##`
0 = Black
8 = Gray
1 = Blue
9 = Light Blue
2 = Green
A = Light Green
3 = Aqua
B = Light Aqua
4 = Red
C = Light Red