Skip to content

Instantly share code, notes, and snippets.

View fiveisprime's full-sized avatar
🖤

Matt Hernandez fiveisprime

🖤
View GitHub Profile
@fiveisprime
fiveisprime / app.js
Last active August 29, 2015 13:57
Koa Scaffold
var koa = require('koa');
var path = require('path');
var route = require('koa-route');
var render = require('koa-swig');
var app = koa();
render(app, {
root: path.join(__dirname, 'views'),
autoescape: true,
'.source.js':
'console.log':
'prefix': 'l'
'body': 'console.log(${1:arguments});$2'
'console.error':
'prefix': 'e'
'body': 'console.error($1);$2'
'require':
'prefix': 'r'
'body': 'require(\'$1\')$2'
@fiveisprime
fiveisprime / transform.js
Last active November 29, 2017 10:40
Replace `_id` with string `id` and remove `__v` when calling `toObject()` in Mongoose.
if (!MySchema.options.toObject) {
PlanSchema.options.toObject = {};
}
MySchema.options.toObject.transform = function(doc, ret) {
// Set the id from the retrun object value which will be a string.
ret.id = ret._id;
delete ret._id;
delete ret.__v;
@fiveisprime
fiveisprime / promise.js
Created February 19, 2014 18:14
A promise that also accepts a callback.
exports.get = function(fn) {
var deferred = Q.defer();
request('http://example.com', function(err, response) {
if (err) {
return deferred.reject(err);
}
if (response.statusCode !== 200) {
return deferred.reject(new Error(response.body || 'Response failed with code ' + response.statusCode));
@fiveisprime
fiveisprime / nonce.js
Last active August 29, 2015 13:56
OAuth 1.0 nonce.
module.exports = function() {
const NONCE_CHARS = [
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1',
'2','3','4','5','6','7','8','9'
];
const NONCE_SIZE = 32;
@fiveisprime
fiveisprime / dabblet.css
Created January 27, 2014 03:29
Pretty form/input labels
/**
* Pretty form/input labels
*/
body {
font-family: 'helvetica', sans-serif;
font-size: 16px;
}
form {
@fiveisprime
fiveisprime / app.js
Last active January 3, 2016 05:59
Cloud storage.
var express = require('express')
, formidable = require('formidable')
, fs = require('fs')
, path = require('path')
, app = express()
, storedFiles = [];
//
// Serve the cloud storage contents.
//
@fiveisprime
fiveisprime / app.js
Last active January 2, 2016 10:39
Redirect Express to www.
app.all(/.*/, function(req, res, next) {
var host = req.header('host');
if (host.match(/^www\..*/i)) return next();
res.redirect(301, 'http://www.' + host);
});
@fiveisprime
fiveisprime / Makefile
Last active January 1, 2016 23:09
Makefile with lint (JSHint), test (mocha), and coverage. Uses locally install modules - make sure that jshint, mocha, and istanbul are devDependencies.
SRC = $(wildcard *.js)
test: $(SRC)
@node_modules/.bin/jshint $^
@node_modules/.bin/istanbul test node_modules/.bin/_mocha \
-R spec -- \
--require should \
--reporter spec
# Uncomment if your tests are in a directory named test.
@fiveisprime
fiveisprime / index.js
Created December 27, 2013 23:24
Tracer configuration
var logger = require('tracer').colorConsole({
format: '{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})'
});