Skip to content

Instantly share code, notes, and snippets.

@jmerrifield
jmerrifield / index.js
Created March 28, 2014 21:55
Sample Express app with domain wrapping
var app = require('express')()
, server = require('http').createServer(app)
app.use(function (req, res, next) {
console.log('Handling', req.path, process.pid)
var d = require('domain').create()
d.add(req)
d.add(res)
@jmerrifield
jmerrifield / index.js
Last active August 29, 2015 14:08
Using promises to delay an action until arbitrary async preconditions are met.
var when = require('when')
// An array of promises that must resolve before we can submit
var preSubmitActions = []
function loadSomeScript() {
// An example use case. We want to delay the submit until
// some script has loaded. We start loading the script in postRender
// so it may or may not be finished by the time someone clicks submit
@jmerrifield
jmerrifield / index.js
Created December 10, 2014 17:15
Isomorphic cookie module
var cookie = require('cookie')
module.exports.forRequest = function (req) {
return {
get: function (name) { return req.cookies[name] },
set: function (name, value, options) {
req.res.cookie(name, value, options)
}
}
}
@jmerrifield
jmerrifield / README.md
Created June 24, 2015 18:52
Be careful DRYing up test code

A test for a module performing trivial transformation on data from an external service:

it('returns the first element in the response', function () {
  var serviceResponse = [{a: 1}]
  serviceStub.returns(serviceResponse)
  
  expect(subject()).to.equal(serviceResponse[0])
})
@jmerrifield
jmerrifield / README.md
Created September 30, 2015 00:11
Lodash of the day: Generating command line args

Creating an array where under some conditions we include extra pairs of values.

Original

var mysqlArgs = ['-h', mysqlConfig.host, '-d', mysqlConfig.database];

if (mysqlConfig.user) {
  mysqlArgs.push('-U', mysqlConfig.user);
}
@jmerrifield
jmerrifield / index.sh
Created November 16, 2016 16:10
List latest snapshot for each zfs dataset
zfs list -H -o name -r zmirror | while read -r line; do zfs list -t snapshot -o name -s creation -r "$line" | tail -1; done