Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / getHost.js
Last active August 29, 2015 14:20
get host from nodejs http request
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'
@JosePedroDias
JosePedroDias / example.js
Last active August 29, 2015 14:21
simple async.map() examples
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
@JosePedroDias
JosePedroDias / mergeArrayOfArrays.js
Created May 19, 2015 22:07
merge array of arrays in JS
var mergeArrayOfArrays = function(arr) {
var merged = [];
return merged.concat.apply(merged, arr);
};
@JosePedroDias
JosePedroDias / examples.js
Last active August 29, 2015 14:21
sort by object attribute (both numeric and alphabetic)
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},
/**
* @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
var seq = function(n){
var arr = new Array(n);
for (var i = 0; i < n; ++i) {
arr[i] = i;
}
return arr;
};
@JosePedroDias
JosePedroDias / index.js
Created July 2, 2015 17:45
requirebin sketch
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'])
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];
};
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';
@JosePedroDias
JosePedroDias / cyclejs.md
Last active August 29, 2015 14:24
my notes on Cycle.js - rewrote basic examples, wrote some test impls, took notes

Cycle.js

Basic examples without ES6

Toggle a checkbox