Skip to content

Instantly share code, notes, and snippets.

@johnsmith17th
johnsmith17th / server.js
Last active December 16, 2015 19:51
Run a http server with express 3.0.
// dependecies
var express = require('express');
var app = module.exports = express();
// configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
@johnsmith17th
johnsmith17th / objectify.js
Last active December 16, 2015 19:50
Convert array to object, each element of array is as a key in object, and ignores the non-string elements.
var util = require('util');
/**
* Convert array to object,
* each element of array is as key in object,
* and ignores the non-string elements.
*
* @example
* [ 'a', 'b', 'c' ] => { 'a':1, 'b':1, 'c':1 }
*
@johnsmith17th
johnsmith17th / params.js
Last active December 16, 2015 19:49
Get parameter with key from express request context, a decorator function of value can be pass.
/**
* Wrap express.request.param with decorator.
*
* @param {HttpRequest} req
* @param {String} name
* @param {Function} [decorator]
* @param {Mixed} [defaultValue]
* @returns value of parameter
*/
@johnsmith17th
johnsmith17th / pagination.js
Last active December 16, 2015 19:49
Mongoose pagination plug-in.
/**
* This module is from mongoose-pagination.
*
* Copyright (c) 2012 Moveline Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@johnsmith17th
johnsmith17th / http_errors.js
Created April 30, 2013 08:56
Create http error ( for json response ) in nodejs.
var util = require("util");
/**
* HttpError.
*
* @param code {Number} : http status code
* @param status {String} : http status description
* @param message {String} : message of error
* @constructor
*/
@johnsmith17th
johnsmith17th / camelCase.js
Created April 20, 2013 03:11
To convert string to camel case in javascript.
function toCamelCase(str) {
return str.toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) {
return match.charAt(match.length-1).toUpperCase();
});
}
@johnsmith17th
johnsmith17th / getter_setter.js
Created April 18, 2013 15:03
Defining Getters and Setters in NodeJS
var sys = require('sys')
function Person(age){
this.__defineGetter__('age', function(){
return age
})
this.__defineSetter__('age', function(arg){
age = arg
})
@johnsmith17th
johnsmith17th / capitalize.js
Created April 16, 2013 01:54
Capitalize and uncapitalize of string.
function capitalize(str) {
return (str.charAt(0).toUpperCase() + str.slice(1));
}
function uncapitalize(str) {
return (str.charAt(0).toLowerCase() + str.slice(1));
}
@johnsmith17th
johnsmith17th / realizeUrl.js
Created April 16, 2013 01:26
Work around with url with params. for routing. Found in node-restify.
/**
* Returns a string representation of a URL pattern ,
* with its parameters filled in by the passed hash.
*
* If a key is not found in the hash for a param, it is left alone.
*
* @param {Object} a hash of parameter names to values for substitution.
*/
function realizeUrl(pattern, params) {
var p = pattern.replace(/\/:([^/]+)/g, function (match, k) {