Skip to content

Instantly share code, notes, and snippets.

Avatar

Jed Schmidt jed

View GitHub Profile
@jed
jed / cloneFn.js
Created Dec 24, 2009
clones an existing function. scope not included.
View cloneFn.js
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 Jan 5, 2010
a function that dispatches to other functions based on signature
View Dispatcher.js
function Dispatcher() {
var signatures = [], functions = [];
function fn() {
var
args = arguments,
argNo,
arity = args.length,
View gist:276760
captures = this.pathname.match(
new RegExp( "^(.*)(" + captures.join(")(.*)(") + ")(.*)$" )
).slice( 1 );
captures[ pos * 2 + 1 ] = val;
this.pathname = captures.join("");
View foreach-bench.js
#!/usr/bin/env node-bench
var list = [
"foo",
"bar",
"baz",
"quux",
"asdf",
"qwerty",
@jed
jed / promises-roundup.js
Created Jan 30, 2010
brainstorming promise syntax
View promises-roundup.js
// 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 );
View fab3.js
// 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
//
View app.js
module.exports = require( "fab" )
()
( "/page1", "page1" )
( "/page2", "page2" )
()
View fabv3.js
/*
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
View sendHeaderTest.js
require("http").createServer( function( req, res ) {
res.write( "this will silently hang because sendHeader was not called" )
res.close();
}).listen( 8000 )
View fabAppTemplate.js
var
assert = require( "assert" ),
test = require( "fab/tests" );
function myBinaryApp( app ) {
return function() {
require( "sys" ).puts( "myBinaryApp called..." )
return app.call( this );
}
}