Skip to content

Instantly share code, notes, and snippets.

View colingourlay's full-sized avatar

Colin Gourlay colingourlay

View GitHub Profile
@colingourlay
colingourlay / debounce.js
Created February 7, 2014 05:23
Debounce
function debounce(fn, wait) {
var timeout;
return function() {
var context = this, // preserve context
args = arguments, // preserve arguments
later = function() { // define a function that:
timeout = null; // * nulls the timeout (GC)
fn.apply(context, args); // * calls the original fn
};
@colingourlay
colingourlay / Y Combinator
Created January 6, 2014 03:55
Y Combinator in JavaScript
var Y = function (incubate) {
return function (scrutinize) {
return function (hacker) {
if (hacker.gender == 'male' || scrutinize(hacker)) {
incubate(hacker.startup);
}
return hacker;
};
};
};
@colingourlay
colingourlay / Array.apply
Last active May 28, 2018 09:35
Function.prototype.apply 's handling of an object instead of an array. It forces the object through Array.prototype.slice function as 'this' to create an array, pretty much what we do to turn an arguments object into a real array.
Array.apply(null, [1, 2, 3])
> [1, 2, 3]
Array.apply(null, [1, 2, 3]).length
> 3
Array.apply(null, {length:3})
> [undefined, undefined, undefined]
Array.apply(null, {length:3}).length
@colingourlay
colingourlay / 0_reuse_code.js
Created November 6, 2013 02:44
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@colingourlay
colingourlay / onlyWhen.js
Created November 4, 2013 02:03
onlyWhen - run a function with its original arguments once a condition is met.
/**
* Creates a wrapped version of a funtion that will run with the original
* arguments, once a condition is met. If the condition is not met, it will
* be checked on a set interval until it is met.
*
* @param {Boolean} condition function you expect to eventually return a truthy value
* @param {Function} func original function to resolve
* @param {Number} interval time to wait between checks
* @return {Function} wrapped function you call can call as it it was the original function
*/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Single-Column Responsive Email Template</title>
<style>
@media only screen and (min-device-width: 541px) {
.content {
@colingourlay
colingourlay / LICENSE
Last active January 5, 2021 19:43
Standalone getScript. This performs the same ability as jQuery.getScript, including the optional callback, but doesn't support the Promises implementation shared with all the other jQuery.ajax methods.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@colingourlay
colingourlay / example.txt
Last active December 26, 2015 04:49
Grunt task for cycling through each item in a directory on an interval and creating a symlink which points to it. This is helpful for cycling through logs, or an archive of data captured over time. Coupled with a server, it serves as a good simulated data endpoint. This should be defined after grunt.initConfig inside your Gruntfile, or you can r…
$ grunt link-cycler --root=archive/json --interval=30000 --link=current --verbose --initial=10
Running "link-cycler" task
Cycling through last 3 of 12 children, 1 per 30000ms...
Child 10/12:
Linking archive/json/current to archive/json/2013-09-23_12-28-32
Child 11/12:
@colingourlay
colingourlay / index.js
Created February 16, 2013 08:57
voxel.js game
var createGame = require('voxel-engine')
function sphereWorld(x, y, z) {
// return the index of the material you want to show up
// 0 is air
if (x*x + y*y + z*z > 15*15) return 0
return 3
}
var game = createGame({
@colingourlay
colingourlay / roman.js
Created April 18, 2012 22:24
Roman numerals generator
(function () {
function romanify(value) {
var numbers, numerals, result, i, len;
numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
result = "";
for (i = 0, len = numbers.length; i < len; i++) {