Skip to content

Instantly share code, notes, and snippets.

View fiveisprime's full-sized avatar
🖤

Matt Hernandez fiveisprime

🖤
View GitHub Profile
@fiveisprime
fiveisprime / default.js
Created December 19, 2013 15:18
Express router.
var express = require('express')
, routes = require('./server/routes')
, path = require('path')
, app = express();
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'hbs');
app.set('views', path.resolve(__dirname, 'views'));
app.disable('x-powered-by');
@fiveisprime
fiveisprime / index.js
Last active December 30, 2015 00:49
Function binding with Async.
var async = require('async');
//
// Constructor function.
//
function Test() {}
//
// Start the async operation.
//
@fiveisprime
fiveisprime / package.json
Created November 30, 2013 18:41
Custom node modules preinstall.
{
"name": "my-application",
"version": "0.0.0",
"description": "Sample application that has custom modules.",
"main": "index.js",
"scripts": {
"preinstall": "npm install ./custom_modules/custom && npm install ./custom_modules/other-custom"
}
}
@fiveisprime
fiveisprime / health.js
Created November 27, 2013 16:03
Health endpoint.
app.get('/health', function(req, res) {
res.send({
pid: process.pid,
memory: process.memoryUsage(),
uptime: process.uptime()
});
});
@fiveisprime
fiveisprime / bind.js
Last active December 29, 2015 01:39
Simplify callbacks using function.bind or var self = lame;
var level = require('level');
//
// Store data from the collector to a leveldb database.
//
var DbController = function(db, collector) {
this.db = level(db);
this.collector = collector;
//

Designing for Testability

An overview of design practices for building modules/applications to best facilitate unit testing.

Goal

The primary goal of the unit test is to be able to exercise as much functionality as possible within a certain module (unit). The best way to achieve this is to completely separate functionality into units (or modules) with any external dependencies being injected at initialization.

Example:

@fiveisprime
fiveisprime / find.js
Last active December 23, 2015 13:29
Walk a directory recursively but in order from top to bottom to find the specified file synchronously.
var fs = require('fs')
, path = require('path');
function find(dir, search, exclude) {
var full = null
, stat = null
, directories = []
, directory, contents, i, j;
exclude = exclude || [];
var async = require('async')
, Q = require('q')
, fs = require('fs');
//
// Typical node style function that uses a callback (fn).
//
var piratize = function(file, fn) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) return fn(err, null);
@fiveisprime
fiveisprime / repl.js
Last active December 22, 2015 19:19
Easy interaction with node.js REPL.
var local = require('repl').start('> ');
local.context.set = function(name) {
return function(err, value) {
if (err) throw err;
local.context[name] = value;
};
};
//
@fiveisprime
fiveisprime / server.js
Created August 31, 2013 18:23
Example API using Hapi.
//
// Example API using Hapi.
// http://hapijs.com
//
var Hapi = require('hapi');
var quotes = [
{
author: 'Audrey Hepburn'