Skip to content

Instantly share code, notes, and snippets.

View glenjamin's full-sized avatar

Glen Mailer glenjamin

View GitHub Profile
@glenjamin
glenjamin / client.js
Created December 13, 2011 10:45 — forked from makeusabrew/client.js
Asynchronous recursive self executing ping function
/**
* Assume our client object manages all communication with the server and other wonderful stuff
*/
var Client = function(socket) {
var that = {};
that.ping = function(limit, callback) {
var results = [];
return _ping(1, results, limit, callback);
};
@glenjamin
glenjamin / gist:1214146
Created September 13, 2011 15:40 — forked from bronson/gist:1214062
which is the nicer validation syntax?
// What's the best syntax to validate a JSON object?
var addr = { NamePrefixText: "Dr.", FirstName: "Lex", LastName: "Luthor",
Address: ["5 Upper Ter"], CityName: "SF" };
Validate(addr, Address); // returns true if valid, errors go in an error object like AR
// Here are some possibilities:
// implicit type
var Address = {
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
var db = new Db('mongo-test', new Server("localhost", Connection.DEFAULT_PORT, {}), {native_parser:false});
var context = this;
var onConnect = function(err, db) {
db.dropDatabase(function() {
var async = require('async');
exports.callPolygen = callPolygen;
function callPolygen(grm, callback) {
async.waterfall([
function find(next) {
findGrm(grm, next);
},
function exec(path, next) {
execPolygen(path, next);
@glenjamin
glenjamin / demo.js
Created May 31, 2011 22:14
Demonstrating the JavaScript object model
var assert = require('assert');
var util = require('util');
var inheritance = require('./inheritance');
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return "Hello "+this.name;
@glenjamin
glenjamin / hoisting.js
Created May 29, 2011 12:19
Taking advantage of javascript function hoisting for more readable class definitions
var util = require('util');
function Parent(){}
// Note we declare inheritance and static properties *before* the constructor
util.inherits(Child, Parent);
Child.static_property = 1;
function Child(){};
var c = new Child();
@glenjamin
glenjamin / static.js
Created May 29, 2011 12:06
"Static" methods in javascript
// To be run under NodeJS
var util = require('util');
function Parent() {}
Parent.tableName = 'parentTable';
Parent.prototype.static_method = function() {
return this.constructor.tableName;
}
function Child() {}