Skip to content

Instantly share code, notes, and snippets.

View jasoncrawford's full-sized avatar

Jason Crawford jasoncrawford

View GitHub Profile
@jasoncrawford
jasoncrawford / client.js
Last active December 18, 2015 10:49
Start of a simple promise-based HTTP client, based on Q-IO and inspired by SuperAgent
var http = require('q-io/http');
var BufferStream = require('q-io/buffer-stream');
// Response
function Response(promise) {
this.promise = promise;
this.status = this.promise.get('status');
@jasoncrawford
jasoncrawford / gist:5873309
Created June 27, 2013 01:35
Augment Mongoose with Q promises. In some cases the method is an alternative to an existing promise-returning methods, like `qexec` vs. Mongoose's native `exec`. In other cases it's providing an operation that Mongoose doesn't give promises for, such as `qsave` and `qcreate`.
var mongoose = require('mongoose');
// Workaround for the fact that chai-as-promised isn't working with Mongoose promises:
// https://github.com/domenic/chai-as-promised/issues/30
mongoose.Query.prototype.qexec = function () {
return Q.npost(this, 'exec', arguments);
}
mongoose.Model.qcreate = function () {
return Q.npost(this, 'create', arguments);
@jasoncrawford
jasoncrawford / console.js
Last active December 24, 2015 15:18
Node REPL that doesn't clobber _
var repl = require('repl')
var vm = require('vm');
var _;
var server = repl.start({
eval: function (cmd, context, filename, callback) {
try {
var match = cmd.match(/^\((.*)\n\)$/);
var code = match ? match[1] : cmd;
@jasoncrawford
jasoncrawford / model.js
Last active September 5, 2016 16:25
Simple relations for Backbone models. Not as full-featured as Backbone-relational (http://backbonerelational.org/) or Supermodel (http://pathable.github.io/supermodel/), but pretty lightweight and concise. Disclaimer: I excerpted this from some other code I wrote and haven't tested it independently. Use at your own risk
var relationEvents = ['add', 'change', 'remove', 'reset', 'sort', 'destroy', 'request', 'sync'];
var Model = exports.Model = Backbone.Model.extend({
hasMany: {
// Subclasses can override to set relations like this:
//
// key: function () { return new Collection([], options); },
},
hasOne: {
@jasoncrawford
jasoncrawford / routing.js
Last active November 23, 2018 15:12
Backbone flash mechanism
var Backbone = require('backbone');
// Flash ///////////////////////////////////////////////////////////////////////////////////////////
// The flash is a way for one controller to pass a small amount of information to the next
// controller, through a navigation event.
//
// The flash holds arbitrary parameters, and is cleared by the router after each navigation event.
var flash = exports.flash = {
params: {},

Keybase proof

I hereby claim:

  • I am jasoncrawford on github.
  • I am jasoncrawford (https://keybase.io/jasoncrawford) on keybase.
  • I have a public key whose fingerprint is 4AAB 4EB9 28FB 0CF1 045B 8BCC 5491 0264 6F74 0339

To claim this, I am signing this object:

@jasoncrawford
jasoncrawford / gist:4243e656851287c1d8c2
Created November 15, 2014 22:11
Openname verification
Verifying that +jasoncrawford is my openname (Bitcoin username). https://onename.io/jasoncrawford
var fs = require('fs');
var _ = require('underscore');
var csv = require('csv');
var Twitter = require('twitter');
function die(error) {
console.error('fatal error');
if (error) console.error('error', error);
if (error.stack) console.error('stacktrace', error.stack);
process.exit(-1);
@jasoncrawford
jasoncrawford / appError.js
Last active November 18, 2015 19:56
JS custom errors
var util = require('util');
var extendable = require('./extendable');
// AppError is an error constructor that inherits from Error but is more easily extendable.
// You can inherit from it using 'extend' just like with Backbone constructors:
//
// var SpecialError = AppError.extend({
// name: 'SpecialError',
// initialize: function (foo, bar) {
// this.message = foo + " " + bar + "!";
@jasoncrawford
jasoncrawford / pwdgen.js
Created November 5, 2016 00:48
XKCD-style password generator
// Generates “correct horse battery staple”-style password: xkcd.com/936
// Usage: node pwdgen.js <file>
// File should contain common words, one per line.
// Params:
var minWordLength = 4;
var desiredEntropyBits = 30;
var fs = require('fs');
var crypto = require('crypto');