Skip to content

Instantly share code, notes, and snippets.

View weaver's full-sized avatar

Ben Weaver weaver

View GitHub Profile
@weaver
weaver / amap.js
Created August 10, 2010 02:59
Asynchronous map #nodejs
// amap -- asynchronous map
//
// Maps fn over list and passes the result to callback. For example,
//
// function loadEntries(folder, callback) {
// fs.readdir(folder, function(err, files) {
// if (err) throw err;
// amap(files, callback, function(name, index, next) {
// load(path.join(folder, name), next);
// });
@weaver
weaver / password-test.js
Created August 10, 2010 20:52
Hash passwords for storage. #nodejs
var assert = require('assert'),
sys = require('sys'),
pwd = require('password'),
vows = require('vows');
vows.describe('Password').addBatch({
'The default method': method(pwd),
'The bcrypt method': method(pwd.bcrypt),
'The sha512 method': method(pwd.sha512)
}).export(module);
@weaver
weaver / make-selector.js
Created October 14, 2010 18:54
Create an absolute jQuery selector for a DOM element.
// Create an absolute jQuery selector for a DOM element.
//
// + el - Element to select.
//
// Returns String selector.
function makeSelector(el) {
var tag, index, stack = [];
for (; el.parentNode; el = el.parentNode) {
tag = el.tagName;
@weaver
weaver / grabme.py
Created October 15, 2010 17:53
Download some grab.by images into the current directory.
#!/usr/bin/env python
## Download some grab.by images into the current directory.
## Requires curl command-line utility.
##
## The bounds should be given in base62 (e.g. in the range 0 - ZZZZ).
##
## Example:
##
## mkdir /tmp/grab
@weaver
weaver / .gitignore
Created February 6, 2011 18:39
Node vs Scheme vs Ruby vs Python web server benchmarks.
gambit-server/web-server
snap-hello-server/bin
snap-hello-server/dist/
@weaver
weaver / stack1.js
Created February 10, 2011 23:00
Use stack introspection to determine the filename of a calling script.
// An example of how to use stack introspection to determine the
// filename of a calling script.
//
// node stack1.js
//
function blah() {
require('./stack2');
}
.packages
*.pyc
@weaver
weaver / json-date.py
Created March 11, 2011 20:39
Convert dates to strings
import json
from datetime import datetime
class Foo(object):
def __init__(self, when):
self.when = when
def __json__(self):
return self.__dict__
@weaver
weaver / express-nested-server-middleware-bug.js
Created March 14, 2011 21:08
When nested middleware is used, the `res.app` and `req.app` properties aren't restored when outerNext is called.
// In Express 1.0.8, the `res.app` and `req.app` properties aren't restored when
// outerNext() is called by nested server middleware.
//
// Run this script, then try to visit something handled by the `notFound()` middleware
// (e.g. `http://localhost:3000/mumble`. An assertion error is raised because `res.app`
// is equal to the `middleware()` instead of `app`.
var Assert = require('assert'),
Express = require('express'),
app = Express.createServer();
@weaver
weaver / .gitignore
Created April 6, 2011 23:10
Express + Formidable, works with bodyParser and sets req.body correctly.
node_modules