Skip to content

Instantly share code, notes, and snippets.

@madhums
madhums / nodejs-install-update.sh
Last active October 1, 2017 10:25
Script to install update node.js
#!/bin/sh
# Usage:
# curl https://gist.githubusercontent.com/madhums/3791075/raw/38675884dfb7e3acd5672092807da54509748266/nodejs-install-update.sh | sh -s 0.8.10
# This script will simply install/update node.js to the version you specify.
# It will be installed/updated to $HOME/local/node
NODEJS=$1
@madhums
madhums / serializeHash.js
Created October 4, 2012 23:51
jquery form serialize hash
var serializeHash = function () {
var attrs = {};
$.each($(this).serializeArray(), function(i, field) {
attrs[field.name] = field.value;
});
return attrs;
};
@madhums
madhums / gh-like.css
Created October 6, 2012 03:26 — forked from robsimmons/gh-like.css
GitHub-esque syntax highlight CSS
/*
Some simple Github-like styles, with syntax highlighting CSS via Pygments.
*/
body{
font-family: helvetica, arial, freesans, clean, sans-serif;
color: #333;
background-color: #fff;
border: none;
line-height: 1.5;
margin: 2em 3em;
@madhums
madhums / promises.md
Created October 24, 2012 09:15 — forked from domenic/promises.md
You're Missing the Point of Promises

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
    // the rest of your code goes here.
});
@madhums
madhums / gist:4135481
Created November 23, 2012 12:52 — forked from cjohansen/gist:4135065
Naming this in nested functions

tl;dr

If you must nest functions in a way that requires access to multiple this', alias outer this to something meaningful - describe the value it's holding. Treat this as the invisible first argument.

In general though, avoiding the situation (nested functions and frivolous use of this) will frequently produce clearer results.

Naming this in nested functions

I was accidentally included in a discussion on how to best name this in nested functions in JavaScript. +1's were given to this suggestion of using _this.

Giving style advice on naming nested this without a meaningful context isn't too helpful in my opinion. Examples below have been altered to have at least some context, although a completely contrived and stupid one.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
mongoose.connect('localhost', 'testing_1088');
mongoose.connection.on('error', function () {
@madhums
madhums / express.mobile.js
Last active December 18, 2015 11:59
render mobile layouts using express.js
// Render mobile views
// if the request is coming from mobile and if you have a view
// with users.mobile.jade, then that view will be rendered
app.use(function (req, res, next) {
res._render = res.render
res.render = function (template, locals, cb) {
var ua = req.header('user-agent')
var fs = require('fs')
@madhums
madhums / flatten.js
Created January 20, 2014 20:28
flatten an array containing prev, next nested objects
function (obj, arr) {
var index = arr.push(obj);
if (obj.prev) {
arr.splice.apply(arr, [index - arr.length, 0].concat(this.timeline(obj.prev, [])));
}
if (obj.next) {
arr.splice.apply(arr, [index + arr.length, 0].concat(this.timeline(obj.next, [])));
}
return arr;
}
@madhums
madhums / gist:71eaa203f11a428cb8a2
Created June 14, 2014 21:04
remove files of a type recursively
$ find app/views -name "*.jade" -type f|xargs rm -f