Skip to content

Instantly share code, notes, and snippets.

View creationix's full-sized avatar
💚
<3

Tim Caswell creationix

💚
<3
View GitHub Profile
@creationix
creationix / mazemaker.js
Created November 5, 2009 22:32
Maze generator for node.js
Array.prototype.shuffle = function () {
var i, j, tempi, tempj;
i = this.length;
if ( i === 0 ) {
return false;
}
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = this[i];
@creationix
creationix / class_compact.js
Created August 19, 2010 19:18
Interesting code to bridge classic constructor objects with my pure prototype objects
var Class = require('./class');
// This global modifier allows you to treat constructor based classes as if
// they were prototype based.
// Define an extend method for constructor functions that works like prototype extends.
Object.defineProperty(Function.prototype, "extend", {value: function extend(obj) {
// Clone the functions's prototype
var props = {}, proto = this.prototype;
Object.getOwnPropertyNames(proto).forEach(function (key) {
@creationix
creationix / .Xmodmap
Created January 9, 2011 06:13
CR48 Ubuntu key mappings
keycode 67 = XF86AudioPrev XF86_Switch_VT_1 F1
keycode 68 = XF86AudioNext XF86_Switch_VT_2 F2
keycode 69 = F5 XF86_Switch_VT_3 F3
keycode 70 = F11 XF86_Switch_VT_4 F4
keycode 71 = XF86AudioPlay XF86_Switch_VT_5 F5
keycode 72 = XF86MonBrightnessDown XF86_Switch_VT_6 F6
keycode 73 = XF86MonBrightnessUp XF86_Switch_VT_7 F7
keycode 74 = XF86AudioMute XF86_Switch_VT_8 F8
keycode 75 = XF86AudioLowerVolume XF86_Switch_VT_9 F9
keycode 76 = XF86AudioRaiseVolume XF86_Switch_VT_10 F10
@creationix
creationix / Add-some-mutability-to-headers-in-node-for-easy-midd.patch
Created February 8, 2011 06:54
Add mutable, implicit headers for easy middleware
From 761f0f03b16f4df0112f4990c38b4f59b2786d04 Mon Sep 17 00:00:00 2001
From: Tim Caswell <tim@creationix.com>
Date: Thu, 10 Feb 2011 02:18:13 -0800
Subject: [PATCH] Add support for mutable/implicit headers for http.
This works for both ServerResponse and ClientRequest.
Adds three new methods as a couple properties to to OutgoingMessage objects.
Tests by Charlie Robbins.
Change-Id: Ib6f3829798e8f11dd2b6136e61df254f1564807e
@creationix
creationix / module.js
Created April 19, 2011 04:27
A super simple module system for browsers. Assumes all source files are concatenated and in browser.
define.defs = {};
define.modules = {};
function define(name, fn) {
define.defs[name] = fn;
}
function require(name) {
if (define.modules.hasOwnProperty(name)) return define.modules[name];
if (define.defs.hasOwnProperty(name)) {
var fn = define.defs[name];
define.defs[name] = function () { throw new Error("Circular Dependency"); };
@creationix
creationix / EventLoop.js
Created June 21, 2011 05:56
Userspace EventLoop for JavaScript
// Make a user-space event loop for doing smart event propigation with zero*
// race conditions.
//
// *(If you are unable to get at the event-source, an automatic setTimeout will
// be created, and there is a small chance some other native event may get in
// before it.)
function makeLoop() {
// Private //
@creationix
creationix / app.js
Created June 22, 2011 04:06
Simple HTML App template
function Box(width, height) {
this.width = width;
this.height = height;
}
Box.prototype.getArea = function () {
return this.width * this.height;
}
var box = new Box(3, 5);
@creationix
creationix / custommain.js
Created July 16, 2011 19:54
A proposed hook into node.js to make deploying node based applications easy and not require re-compiles.
startup.customRuntimeMain = function () {
var path = NativeModule.require('path');
var custom = path.resolve(process.cwd(), process.argv[0] + ".js");
if (!path.existsSync(custom)) return;
process.argv[1] = custom;
};
@creationix
creationix / glmatrix.js
Created August 30, 2011 21:17
Spinning cube and pyramid in node webgl
// glMatrix v0.9.5
glMatrixArrayType=typeof Float32Array!="undefined"?Float32Array:typeof WebGLFloatArray!="undefined"?WebGLFloatArray:Array;vec3={};vec3.create=function(a){var b=new glMatrixArrayType(3);if(a){b[0]=a[0];b[1]=a[1];b[2]=a[2]}return b};vec3.set=function(a,b){b[0]=a[0];b[1]=a[1];b[2]=a[2];return b};vec3.add=function(a,b,c){if(!c||a==c){a[0]+=b[0];a[1]+=b[1];a[2]+=b[2];return a}c[0]=a[0]+b[0];c[1]=a[1]+b[1];c[2]=a[2]+b[2];return c};
vec3.subtract=function(a,b,c){if(!c||a==c){a[0]-=b[0];a[1]-=b[1];a[2]-=b[2];return a}c[0]=a[0]-b[0];c[1]=a[1]-b[1];c[2]=a[2]-b[2];return c};vec3.negate=function(a,b){b||(b=a);b[0]=-a[0];b[1]=-a[1];b[2]=-a[2];return b};vec3.scale=function(a,b,c){if(!c||a==c){a[0]*=b;a[1]*=b;a[2]*=b;return a}c[0]=a[0]*b;c[1]=a[1]*b;c[2]=a[2]*b;return c};
vec3.normalize=function(a,b){b||(b=a);var c=a[0],d=a[1],e=a[2],g=Math.sqrt(c*c+d*d+e*e);if(g){if(g==1){b[0]=c;b[1]=d;b[2]=e;return b}}else{b[0]=0;b[1]=0;b[2]=0;return b}g=1/g;b[0]=c*g;b[1]=d*g;b[2]=e*g;return b};vec3.cross=function(a,b,c
@creationix
creationix / route.js
Created December 5, 2011 19:12
Sanity check for async template idea
Creationix.route("GET", "/", function (req, res, params, next) {
render("frontindex", {
title: query("index", "title"),
links: query("index", "links"),
articles: loadArticles
}, function (err, html) {
if (err) return next(err);
res.writeHead(200, {
"Content-Length": Buffer.byteLength(html),
"Content-Type": "text/html; charset=utf-8"