Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / arr-stat.js
Last active April 29, 2025 22:13
JavaScript statistical functions for arrays: max, min, range, midrange, sum, mean / average, median, modes, variance, standard deviation, mean absolute deviation, z scores
var arr = {
max: function(array) {
return Math.max.apply(null, array);
},
min: function(array) {
return Math.min.apply(null, array);
},
range: function(array) {
@Daniel-Hug
Daniel-Hug / call.js
Last active February 10, 2025 18:16
chainable wrapper for JS setTimeout and setInterval
// call.js
// chainable setTimeout and setInterval JavaScript wrapper
//
// By Daniel Hug: https://gist.github.com/Daniel-Hug/9062765
// MIT license: http://hug.mit-license.org/
//
// use:
// var timer = call(fn, scope, args).after(1000).start();
// var interval = call(fn, scope, args).every(1000).fire().start();
// timer.stop();
@Daniel-Hug
Daniel-Hug / function-bind.js
Last active January 6, 2025 22:37 — forked from dsingleton/function-bind.js
Polyfill for Function.prototype.bind
Function.prototype.bind=(function(){}).bind||function(b){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");}function c(){}var a=[].slice,f=a.call(arguments,1),e=this,d=function(){return e.apply(this instanceof c?this:b||window,f.concat(a.call(arguments)));};c.prototype=this.prototype;d.prototype=new c();return d;};
@Daniel-Hug
Daniel-Hug / collision-detection.js
Last active October 9, 2024 13:10
JS functions: check if 2 rectangles intersect, are touching, or if one contains the other
// Check if rectangle a contains rectangle b
// Each object (a and b) should have 2 properties to represent the
// top-left corner (x1, y1) and 2 for the bottom-right corner (x2, y2).
function contains(a, b) {
return !(
b.x1 < a.x1 ||
b.y1 < a.y1 ||
b.x2 > a.x2 ||
b.y2 > a.y2
);
@Daniel-Hug
Daniel-Hug / dabblet.css
Created January 14, 2012 02:22
A "deeper" indented text effect with the :before and :after pseudo-elements.
/**
* A "deeper" indented text effect with the :before and :after pseudo-elements.
*/
html, body {
height: 100%;
}
body {
margin: 0;
@Daniel-Hug
Daniel-Hug / get-text-nodes.js
Last active April 7, 2022 20:09
Get the text nodes which are descendants of the passed parent
// Get the text nodes which are descendants of the passed parent
function getTextNodes(parent){
var all = [];
for (parent = parent.firstChild; parent; parent = parent.nextSibling) {
if (['SCRIPT','STYLE'].indexOf(parent.tagName) >= 0) continue;
if (parent.nodeType === Node.TEXT_NODE) all.push(parent);
else all = all.concat(getTextNodes(parent));
}
return all;
}
@Daniel-Hug
Daniel-Hug / change-tag.js
Last active November 12, 2021 02:42
JS function: replace tag name but keep element contents. Source: http://stackoverflow.com/a/15086834/552067
// Replace tag name but keep element contents
function changeTag(el, newTagName, keepAttributes) {
var newEl = document.createElement(newTagName);
// Copy the children
while (el.firstChild) {
newEl.appendChild(el.firstChild); // *Moves* the child
}
// Copy the attributes
@Daniel-Hug
Daniel-Hug / probabilities.js
Last active October 5, 2020 18:28
JS probability functions
// probability that the event with the passed probability will NOT occur
function complement(p) {
return 1 - p;
}
// probability that a and b will happen when neither outcome
// is affected by the other (accepts 1 or more arguments)
function intersectionOfIndependentEvents(a, b) {
var ret = a;
for (var i = 1; i < arguments.length; i++) {
@Daniel-Hug
Daniel-Hug / delegate-event.js
Last active September 14, 2020 05:05
Vanilla JS equivalent of jQuery's .live(): use event delegation to handle events whose target matches a selector, closest(): get nearest parent element matching selector
// get nearest parent element matching selector
var closest = (function() {
var el = HTMLElement.prototype;
var matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
return function closest(el, selector) {
return matches.call(el, selector) ? el : closest(el.parentElement, selector);
};
})();
@Daniel-Hug
Daniel-Hug / dabblet.css
Created May 4, 2012 01:19
Pure CSS aspect ratio with no spacer images or js! :)
/*
* Pure CSS aspect ratio with no spacer images or js! :)
*/
body {
width: 36%;
margin: 8px auto;
}
div.stretchy-wrapper {