Skip to content

Instantly share code, notes, and snippets.

View jed's full-sized avatar

Jed Schmidt jed

View GitHub Profile
@jed
jed / cloneFn.js
Created December 24, 2009 18:34
clones an existing function. scope not included.
function cloneFn( fn ) {
var
str = fn.toString(),
args = str.match( /\((.*)\)/ )[1].split(/\W+/),
body = str.match( /{((?:.|\n)+)}/ )[1];
return Function.apply( this, args.concat( body ) );
};
@jed
jed / Dispatcher.js
Created January 5, 2010 17:07
a function that dispatches to other functions based on signature
function Dispatcher() {
var signatures = [], functions = [];
function fn() {
var
args = arguments,
argNo,
arity = args.length,
captures = this.pathname.match(
new RegExp( "^(.*)(" + captures.join(")(.*)(") + ")(.*)$" )
).slice( 1 );
captures[ pos * 2 + 1 ] = val;
this.pathname = captures.join("");
#!/usr/bin/env node-bench
var list = [
"foo",
"bar",
"baz",
"quux",
"asdf",
"qwerty",
@jed
jed / promises-roundup.js
Created January 30, 2010 19:59
brainstorming promise syntax
// a few ideas for unifying async "promises"
// there are three types of callbacks:
function callback( data ){ sys.puts( "called on both success and error" ) }
function success( data ){ sys.puts( "called on success only" ) }
function error( data ){ sys.puts( "called on error only" ) }
// for any async function, offer a single optional callback.
posix.cat( "config.json", callback );
@jed
jed / fab3.js
Created February 13, 2010 23:51
// thoughts for v3 of the (fab) API.
//
// each handler gets two function arguments:
// (1) a stream back to the last app, and
// (2) a stream forward to the next app, or a 404 if there is none.
//
// (i haven't figured out elegant names, so let's just call them respond for and request)
//
// all information about the request is streamed, so that you need to
//
@jed
jed / app.js
Created February 16, 2010 13:20
module.exports = require( "fab" )
()
( "/page1", "page1" )
( "/page2", "page2" )
()
/*
about (fab) v3
==============
the first version of (fab) was mostly a toy project, to see how function
chaining could help create a concise javascript DSL for building web
apps.
the second version of (fab) smoothed out some edges and focused on the
require("http").createServer( function( req, res ) {
res.write( "this will silently hang because sendHeader was not called" )
res.close();
}).listen( 8000 )
var
assert = require( "assert" ),
test = require( "fab/tests" );
function myBinaryApp( app ) {
return function() {
require( "sys" ).puts( "myBinaryApp called..." )
return app.call( this );
}
}