Skip to content

Instantly share code, notes, and snippets.

Avatar
🕉️
all good

José Pedro Dias JosePedroDias

🕉️
all good
View GitHub Profile
@JosePedroDias
JosePedroDias / serveMedia.js
Last active Aug 29, 2015
serve media (static server with ranged request support)
View serveMedia.js
/**
* serves media files in the directory.
* by default won't refresh on file changes, use nodemon if you want that behavior
* 2014, jose.pedro.dias@gmail.com
*/
var http = require('http'),
fs = require('fs'),
@JosePedroDias
JosePedroDias / genAvconvCmd.js
Created May 9, 2014
outputs avconv configuration
View genAvconvCmd.js
// argument input_file
// process inFile and outFile
var inFile = process.argv.pop();
var cutAt = inFile.lastIndexOf('.');
var inExt = inFile.substr(cutAt + 1);
var inName = inFile.substr(0, cutAt);
var outFile = [inName, 'mp4'].join('.');
@JosePedroDias
JosePedroDias / queryString.js
Created Jun 22, 2014
to and from queryString
View queryString.js
var parseQueryString = function(url) {
var aParams = {};
if (url.match(/\?(.+)/i)) {
var queryStr = url.replace(/^(.*)\?([^\#]+)(\#(.*))?/g, '$2');
if (queryStr.length > 0) {
var i, I, pairVar, aQueryStr = queryStr.split(/[;&]/);
I = aQueryStr.length;
for (i = 0; i < I; ++i) {
pairVar = aQueryStr[i].split('=');
aParams[decodeURIComponent(pairVar[0])] = (typeof(pairVar[1]) !== 'undefined' && pairVar[1]) ? decodeURIComponent(pairVar[1]) : '';
View example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSONTree example</title>
<link type="text/css" rel="stylesheet" href="jsontree.css"></link>
<script type="text/javascript" src="jsontree.js"></script>
</head>
<body>
@JosePedroDias
JosePedroDias / cp-credentials.js
Last active Aug 29, 2015
crowdprocess example
View cp-credentials.js
module.exports = {
"email": "<your email here>",
"password": "<your pass here>"
};
@JosePedroDias
JosePedroDias / parallelCbs.js
Last active Aug 29, 2015
parallel tasks in pure JS - sample running here http://jsbin.com/weyiy/latest
View parallelCbs.js
// process items in parallel thingy
var parallelCbs = function(items, itemFn, cb) {
var left = items.length;
var res = new Array(left);
var cb2 = function(err, res) { // to enforce the main cb is not called more than once
if (this.called) { return; }
this.called = true;
this.cb(err, res);
}.bind({cb:cb, called:false});
@JosePedroDias
JosePedroDias / getMyIPs.js
Created Jul 2, 2014
returns object with network interface name -> IP address
View getMyIPs.js
var os = require('os');
var getMyIPs = function() {
var ips = {};
var ifaces = os.networkInterfaces();
var onDetails = function(details) {
if (details.family === 'IPv4') {
ips[ dev ] = details.address;
}
@JosePedroDias
JosePedroDias / removeCycles.js
Created Jul 2, 2014
receives a JS object which may have cycles and returns another one with repeated cycles replaced by '[removed]' string. Use return instead if you don't mind them not being signalled.
View removeCycles.js
var removeCycles = function(o) {
var seen = [];
var s = JSON.stringify(o, function(k, v) {
if (v !== null && typeof v === 'object') {
if (seen.indexOf(v) !== -1) {
//return;
v = '[removed]';
}
else {
@JosePedroDias
JosePedroDias / argumentsSnippet.js
Last active Aug 29, 2015
performant arguments handling
View argumentsSnippet.js
var ctx = this; // this can be skipped if you don't care about the context
var argsL = arguments.length;
var args = new Array(argsL); // optimizable arguments-to-array
for(var i = 0; i < argsL; ++i) {
args[i] = arguments[i];
}
@JosePedroDias
JosePedroDias / myCoExperiment.js
Last active Aug 29, 2015
copying the co idea but supporting only yielding thunks. run with node >= 0.11.2: `node --harmony myCoExperiment.js`
View myCoExperiment.js
'use strict';
/*jshint esnext:true */
let log = function(msg) { console.log(msg); };
let now = function() { return Date.now(); };
You can’t perform that action at this time.