Skip to content

Instantly share code, notes, and snippets.

🕉
all good

José Pedro Dias JosePedroDias

Block or report user

Report or block JosePedroDias

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JosePedroDias
JosePedroDias / print_stack_trace.js
Created Feb 10, 2014
print stack trace on JS anywhere
View print_stack_trace.js
var printStackTrace = function(msg) {
try {
var ex = new Error(msg);
console.log(ex);
} catch (ex) {}
};
View jsonp.js
window.jsonpCbs = {};
var jsonp = function(uri, cb) {
var key = 'cb' + Math.floor( Math.random() * 100000 );
var cb2 = function() {
delete window.jsonpCbs[key];
cb.apply(null, arguments);
};
View ajax.js
var ajax = function(uri, cb) {
var xhr = new XMLHttpRequest();
//xhr.withCredentials = true; // uncomment to send cookies and stuff
xhr.open('POST', uri, true); // GET OR POST...
var cbInner = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
return cb(null, JSON.parse(xhr.response));
}
cb('error requesting ' + uri);
};
@JosePedroDias
JosePedroDias / callbackFnCatchingExceptions.js
Last active Aug 29, 2015
try...catch around functionWithCallbackWorkflow so it doesn't mess with your logic
View callbackFnCatchingExceptions.js
var functionWithCallbackWorkflow = function(arg1, arg2, cb) {
try {
// DO STUFF, generating res
} catch (ex) {
return cb(ex);
}
cb(null, res);
};
@JosePedroDias
JosePedroDias / generic_export_recipe.js
Last active Aug 29, 2015
recipe to expose a JS module for CommonJS, AMD and global namespace
View generic_export_recipe.js
(function(context) {
'use strict';
var modName = 'stuff'; // used only if mod is a function and no CommonJS or AMD supported
// define your module here... can be a function or an object
var mod = {};
// export
if (typeof module === 'object' && module.exports) { // CommonJS
@JosePedroDias
JosePedroDias / minifycss.js
Created Mar 17, 2014
minifies css rules (string). naive approach (doesn't interpret anything)
View minifycss.js
var minifyCss = function(s) {
s = s.replace('\n', ''); // remove newlines
s = s.replace(/\s+/g, ' '); // several whitespace to 1 spacebar
s = s.replace(/\s*([:;,{}])\s*/g, '$1'); // strips whitespace around :;,{} characters
s = s.replace(/\/\*(.|[\r\n])*?\*\//g, ''); // uses non-greedy matching *? to remove comments
return s;
};
@JosePedroDias
JosePedroDias / node-canvas-http.js
Created Mar 19, 2014
generates image using node canvas module on server
View node-canvas-http.js
// https://github.com/learnboost/node-canvas
// https://github.com/LearnBoost/node-canvas/wiki
// sudo apt-get install libcairo2-dev
// npm install canvas
var http = require('http');
var url = require('url');
var Canvas = require('canvas');
// http://stage.sl.pt:8080/?t=hi%20there
View chars-charcodes.js
'\n'.charCodeAt(0); // 10
String.fromCharCode(10); // '\n'
@JosePedroDias
JosePedroDias / parseXSV.js
Last active Aug 29, 2015
Proper TSV/CSV parsing. toObjects uses first line as header and converts lines into objects. sep defaults to '\t', use ',' instead for CSV.
View parseXSV.js
var parseXSV = function(str, toObjects, sep) {
if (!sep) { sep = '\t'; }
var chars = str.split('');
var lst = [];
var elem = [];
var field = [];
var inComplexField = false;
@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'),
You can’t perform that action at this time.