Skip to content

Instantly share code, notes, and snippets.

View creationix's full-sized avatar
💚
<3

Tim Caswell creationix

💚
<3
View GitHub Profile
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
@creationix
creationix / file.js
Created September 17, 2009 16:59
NodeJS File object
var File = {
open: function (path, mode) {
var fd, queue, enc;
enc = 'utf8';
queue = [];
// This is called after each step in the queue finishes, when the queue is empty, then we close the file descriptor.
function run_next() {
if (queue.length > 0) {
@creationix
creationix / instance_eval_playground.js
Created October 1, 2009 20:30
Various implementations of instance_eval for the Javascript Object prototype
include("/utils.js");
// "this" scope is the object, closures work like normal. Basically this is a nowmal "call" use for functions
Object.prototype.instance_eval = function (input) {
// Convert the function to a string so that we can rebind it
if (typeof input === 'function') {
return input.call(this);
}
@creationix
creationix / turtle.js
Created October 2, 2009 01:16
The Ruby Turtle DSL metaprogramming example ported to JavaScript
// Add some ruby-like methods to some of the builtins
Object.prototype.instance_eval = function (block) {
// Convert the function to a string so that we can rebind it
if (typeof block === 'function') {
block = "(" + block + ").call(this)";
}
// Eval using "this" as the "with" scope
return eval("with(this) { " + block + "}");
};
@creationix
creationix / Fix-the-link-to-the-new-Contribute-section.patch
Created October 13, 2009 18:10
Fix-the-link-to-the-new-Contribute-section.patch
@creationix
creationix / Dry-up-the-open-.patch
Created October 28, 2009 18:08
patch for file.js in node.js
From 57e3be04890597d1757d629c7c2e63a9ae5b3ead Mon Sep 17 00:00:00 2001
From: Tim Caswell <[email protected]>
Date: Wed, 28 Oct 2009 11:49:22 -0500
Subject: [PATCH] DRY up the open, write, read, and close methods on the File prototype.
---
lib/file.js | 28 ++++++++--------------------
1 files changed, 8 insertions(+), 20 deletions(-)
diff --git a/lib/file.js b/lib/file.js
@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 / Deferred.js
Created November 20, 2009 16:41
Simple re-implementation of Dojo's Deferred constructor
function Deferred() {
this.chain = [];
}
Deferred.prototype.addBoth = function (cb) {
this.chain.push([cb, cb]);
return this;
}
Deferred.prototype.addCallback = function (cb) {
this.chain.push([cb, null]);
return this;
// Add ruby-like JSON ability to the standard JSON code.
(function () {
// A recursive function that traverses the raw object looking for special
// serialized objects to be de-serialized. Used by `JSON.load`
var json_object_load = function (obj) {
var key;
if (typeof obj.json_class === 'string') {
return (eval(obj.json_class)).jsonCreate(obj);
}
@creationix
creationix / json-ext.js
Created November 20, 2009 17:44
A small extension to the built in JSON library for Javascript that makes it able to "load" and "dump" any object.
// Add ruby-like JSON ability to the standard JSON code.
(function () {
// A recursive function that traverses the raw object looking for special
// serialized objects to be de-serialized. Used by `JSON.load`
var json_object_load = function (obj) {
var key;
if (typeof obj.json_class === 'string') {
return (eval(obj.json_class)).jsonCreate(obj);
}