Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / genAvconvCmd.js
Created May 9, 2014 18:20
outputs avconv configuration
// 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 June 22, 2014 11:15
to and from queryString
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]) : '';
<!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 August 29, 2015 14:03
crowdprocess example
module.exports = {
"email": "<your email here>",
"password": "<your pass here>"
};
@JosePedroDias
JosePedroDias / parallelCbs.js
Last active August 29, 2015 14:03
parallel tasks in pure JS - sample running here http://jsbin.com/weyiy/latest
// 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 July 2, 2014 13:24
returns object with network interface name -> IP address
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 July 2, 2014 14:17
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.
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 / myCoExperiment.js
Last active August 29, 2015 14:03
copying the co idea but supporting only yielding thunks. run with node >= 0.11.2: `node --harmony myCoExperiment.js`
'use strict';
/*jshint esnext:true */
let log = function(msg) { console.log(msg); };
let now = function() { return Date.now(); };
@JosePedroDias
JosePedroDias / argumentsSnippet.js
Last active August 29, 2015 14:03
performant arguments handling
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 / tpl.js
Last active August 29, 2015 14:03
dumb tpl expansions
var fetchFnComment = function(f) {
return f.toString()
.replace(/^[^\/]+\/\*!?/, '')
.replace(/\*\/[^\/]+$/, '');
};
var applyTemplate = function(targetEl, tpl, model) {
tpl = fetchFnComment(tpl);
for (var k in model) {
tpl = tpl.replace( new RegExp('({{'+k+'}})', 'g'), model[k] );