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 / 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 / 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 / deburglar.js
Last active August 17, 2017 09:50
Get duplicate requests in JavaScript
// deburglar.getDupes();
var deburglar = (function () {
var dict = {};
var maybeTheOriginalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url) {
if (!dict[url])
dict[url] = 0;
dict[url]++;
return maybeTheOriginalOpen.apply(this, arguments);
};
@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 / $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 / EncodeDecode
Last active November 16, 2015 23:03
Encode/Decode XML meta characters
function htmlDecode(s) {
var el = document.createElement("div");
el.innerHTML = s;
return el.textContent;
}
function htmlEncode(s) {
var el = document.createElement("div");
el.textContent = s;
return el.innerHTML;
@A1rPun
A1rPun / scrollspy.js
Last active November 16, 2015 23:02 — forked from pascaldevink/scrollspy.js
/*
Copyright (C) 2021 Pascal de Vink (Tweakers.net)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@A1rPun
A1rPun / animation
Last active November 16, 2015 23:02
Minimal css animation setup
#me {
-webkit-animation: rotation 2s infinite linear;
-moz-animation: rotation 2s infinite linear;
-o-animation: rotation 2s infinite linear;
animation: rotation 2s infinite linear;
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotatex(0deg);}
to {-webkit-transform: rotatex(359deg);}