This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r = /(https?:\/\/)?([^ \/]+)/; | |
var getHost = function(url, headers) { | |
return headers.host || r.exec(url)[2]; | |
}; | |
// usage, assuming req is a HTTP nodejs request: | |
// getHost(req.url, req.headers) | |
// examples: | |
// getHost('/asd/zxc', {host:'127.0.0.1:9999'}); // returns '127.0.0.1:9999' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
// work items to process | |
var tasks = [ | |
{name:'john', age:20}, | |
{name:'anne', age:15}, | |
{name:'igor', age:66} | |
]; | |
// function which takes an item and calls wcb once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mergeArrayOfArrays = function(arr) { | |
var merged = []; | |
return merged.concat.apply(merged, arr); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [ | |
{name:'John', age:30}, | |
{name:'Anne', age:12}, | |
{name:'Zoe', age: 2} | |
]; | |
arr.sort( sortByNumericAttribute('age') ); | |
/* | |
[ {name:'Zoe', age: 2}, | |
{name:'Anne', age:12}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @function {ObjectEl} ? creates a flash object | |
* @param {Object} o options | |
* @param {String} o.swf flash resource uri | |
* @param {DOMElement} o.container where to put the flash object (either parent or replacement, depending of inseadOf option) | |
* @param {String} [o.id] id and name for the object element | |
* @param {Number[2]} [o.dims] width and height, in pixels | |
* @param {Object} [o.params] flash params. supports allowfullscreen, allowscriptaccess, bgcolor, quality, wmode | |
* @param {Object} [o.flashvars] flash variables. will be passed via param flashvars transformed as query string | |
* @param {Boolean} [o.insteadOf] if trueish, changes behavior from append to container to replace container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var seq = function(n){ | |
var arr = new Array(n); | |
for (var i = 0; i < n; ++i) { | |
arr[i] = i; | |
} | |
return arr; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FlowGraph = require('flowgraph') | |
var FlowGraphView = require('flowgraph-editor') | |
var insertCss = require('insert-css') | |
// define your graph | |
graph = new FlowGraph() | |
graph.addNode('A', ['in'], ['out']) | |
graph.addNode('B') // default ports are in and out | |
graph.addNode('C', ['1', '2', '3'], ['stdout', 'stderr']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var humanSize = function(val) { // starts with bytes | |
var i = -1; | |
var units = 'B KB MB GB'.split(' '); | |
do { | |
val = val / 1024; | |
i++; | |
} while (val > 1024); | |
return Math.max(val, 0.1).toFixed(1) + units[i]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = ['a', undefined, 'b']; | |
// ["a", undefined, "b"] | |
arr.map(function(a) { return 'X'; }); | |
// ["X", "X", "X"] | |
var arr2 = new Array(3); | |
// [undefined × 3] | |
arr2[0] = 'a'; |