Skip to content

Instantly share code, notes, and snippets.

define(['compose'], function(Compose){
var cache = {};
exports = Compose(Compose, function(){
var id = this.id = MyClass.nextId();
cache[id] = {};
}, {
someMethod: function(){
var id = this.id,
@sfoster
sfoster / gist:1197446
Created September 6, 2011 12:43
node jsonp wrapper for static json files
var express = require("express"),
jsonp = require("connect-jsonp"),
path = require('path'),
argv = require('optimist').argv;
// delegation the way I like it
var delegate = function(proto, mixin){
var o = Object.create(proto);
if(mixin){
@sfoster
sfoster / SceneSpec.js
Created August 12, 2011 23:43
Jasmine + AMD
define(['lib/Scene'], function(Scene){
describe("Scene", function() {
var scene;
beforeEach(function() {
scene = new Scene();
});
it("should instantiate ok", function() {
@sfoster
sfoster / gist:1128716
Created August 5, 2011 22:51
hooking up xml2json to convert XML piped in via stdin to JSON on stdout
// hooking up xml2json to convert XML piped in via stdin to JSON on stdout
// e.g.:
// curl http://www.gutenberg.org/feeds/today.rss | node index.js > gutenberg.json
var x2j = require("xml2json");
process.stdin.resume();
process.stdin.setEncoding('utf8');
var xml = '';
@sfoster
sfoster / fileWizard.js
Created July 8, 2011 12:06
Wizard-style prompts from node.js
/**
* Module dependencies.
*/
var fs = require('fs'),
path = require('path');
spawn = require('child_process').spawn,
exec = require('child_process').exec;
@sfoster
sfoster / gist:1018660
Created June 10, 2011 11:36
Screenshot sequence w. node.js and screencapture
(function(){
var sys = require('sys');
var filestem = process.ARGV.length > 2 ? process.ARGV.length[2] : "screen";
var spawn = require('child_process').spawn,
timer = null,
startTime, stopTime;
function outfile(d) {
@sfoster
sfoster / gist:1018071
Created June 10, 2011 00:56
perl script to extract tab delimited entries from Firebug's ConsoleExport html output
#!/usr/bin/perl
# quick, throwaway script to extract out info messages from the HTML dump of the
# firebug console which is produced by the ConsoleExport Firefox/Firebug
# plugin.
# I was interested in info message of the form.. so its pretty specific
# console.info("some label:87ms", 113, 200);
# TODO provide a way to extract log/warn/info/error/all entry types
@sfoster
sfoster / simple mocking
Created April 6, 2011 17:09
Simple mechanism for temporarily replacing a method's implementation e.g. for mocking during tests
var mockMethod = function(obj, methName, fn) {
var orig = obj[methName];
var handle = [obj, methName, orig];
obj[methName] = fn;
return handle;
}, unMockMethod = function(handle) {
handle[0][handle[1]] = handle[2];
};
// usage:
@sfoster
sfoster / getValue snippet
Created January 4, 2011 10:13
dojo.data store (e.g. ServiceStore extension) getValue method implementation that allows mapping of attribute names to potentially deep properties
getValue: function(/*Object*/ item, /*String*/property, /*value?*/defaultValue){
// summary:
// Gets the value of an item's 'property'
//
// item:
// The item to get the value from
// property:
// property to look up value for
// defaultValue:
// the default value
dojo.provide("myns.data.FeedStore");
dojo.require("dojox.data.ServiceStore");
dojo.require("dojox.rpc.Service");
dojo.declare("myns.data.FeedStore", dojox.data.ServiceStore, {
// summary:
// Flexible, extensible store for normalizing read-only JSON feed data
service: null,