Skip to content

Instantly share code, notes, and snippets.

View alexbosworth's full-sized avatar

Alex Bosworth alexbosworth

View GitHub Profile
@alexbosworth
alexbosworth / node-jquery.js
Created September 27, 2010 21:05
A port of jQuery to Node.js (required for all my other scripts)
// node-jquery - port of jquery 1.4.2 (http://jquery.com/)to node.js by alex bosworth (http://alexbosworth.net/)
function now() {
return (new Date).getTime();
}
var window = {},
jsc = now(),
rscript = /<script(.|\s)*?\/script>/gi,
rselectTextarea = /select|textarea/i,
@alexbosworth
alexbosworth / end_script_timer.js
Created October 7, 2010 06:21
a runtime calculator for scripts
// for node-jquery.js (http://gist.github.com/599831)
// add this to a script to show how long it took to complete
process.on('exit', $.proxy(function () {
var sec = Math.round((new Date().getTime() - this.start.getTime()) / 1000);
console.log('completed', 'in ' + sec + ' seconds');
}, {start:new Date()}));
@alexbosworth
alexbosworth / amazon-sdb.js
Created October 13, 2010 10:14
An AWS SimpleDb library
/*
* Alex Bosworth
*
* A straightforward S3 library
*
* USE: var s3 = new S3(AWS_KEY, AWS_SECRET, {defaultBucket : MY_BUCKET});
* s3.put(KEY, {data:{},headers:{}}, [bucket]);
* s3.get(KEY, [bucket]).on('success', function(data) { console.log(data); });
* (more operations: buckets, info, list)
*
@alexbosworth
alexbosworth / parseGetArgs.js
Created October 20, 2010 07:34
in progress GET object parser
// SCRIPT IN PROGRESS! BEWARE SUPER HACKY/UGLY CODE BELOW
// takes an associative array of args "a[b][c]" = d into a : { b : { c : d } }
// use: var getArgs = parseGetArgs(url.parse(request.url, true).query);
function parseGetArgs(args) {
if (!args) return null;
var result = {};
@alexbosworth
alexbosworth / Asynchronous Patterns
Created October 25, 2010 15:23
Asynchronous Patterns For Node.JS
** Steps **
Problem:
Map asynchronous callbacks onto a multi-step process.
Centralize flow logic
object {
steps : [function, function, function]
advance : function() { this.shift(function)(); }
}
@alexbosworth
alexbosworth / node.page.structure.js
Created December 16, 2010 01:05
an example of doing page classes in node.js
function echo(string) { console.log(string); }
process.on('uncaughtException', function (err) {
echo(err.stack);
echo('uncaught Exception: ' + err);
});
var KEYS = require('keychain'),
$ = require('node-jquery'),
S3 = require('amazon-s3').S3;
Sdb.prototype.getQueryString = function(query) {
if (!query.domain) throw new Error('no domain specified');
if (!query.fields || !query.fields.length) query.fields = '*';
var str = query.fields.join() + ' from ' + query.domain;
if (query.selectors && query.selectors.length)
str+= ' where ' + query.selectors.join(' intersection ');
@alexbosworth
alexbosworth / gist:1153365
Created August 18, 2011 05:41
node jquery with some added flavor bits
function now() {
return (new Date).getTime();
}
var window = {},
jsc = now(),
rscript = /<script(.|\s)*?\/script>/gi,
rselectTextarea = /select|textarea/i,
rinput = /color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week/i,
jsre = /=\?(&|$)/,
@alexbosworth
alexbosworth / deferred.js
Created August 30, 2011 02:09
Deferred for Node.js
// Deferreds are useful for chaining: doSomething().success(doSomethingElse).failure(stopStuff);
function Deferred() {
this._successCbk = function() {},
this._failureCbk = function() {};
return this;
}
Deferred.prototype.success = function(cbk) {
this._successCbk = cbk;
@alexbosworth
alexbosworth / responder.js
Created September 9, 2011 01:21
Micro Express.js
exports.respond = function(req, res) {
var responders = [];
for (var i = 2, arg; arg = arguments[i]; i++) responders.push(arguments[i]);
var next = function() {
var responder = responders.shift();
if (typeof(responder) != 'function') return;