Skip to content

Instantly share code, notes, and snippets.

View JosePedroDias's full-sized avatar

José Pedro Dias JosePedroDias

View GitHub Profile
@JosePedroDias
JosePedroDias / print_stack_trace.js
Created February 10, 2014 21:04
print stack trace on JS anywhere
var printStackTrace = function(msg) {
try {
var ex = new Error(msg);
console.log(ex);
} catch (ex) {}
};
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);
};
@JosePedroDias
JosePedroDias / ajax.js
Last active August 29, 2015 13:56
simple AJAX
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 August 29, 2015 13:57
try...catch around functionWithCallbackWorkflow so it doesn't mess with your logic
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 February 11, 2024 23:06
recipe to expose a JS module for CommonJS, AMD and global namespace
(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 March 17, 2014 14:09
minifies css rules (string). naive approach (doesn't interpret anything)
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 March 19, 2014 02:25
generates image using node canvas module on server
// 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
'\n'.charCodeAt(0); // 10
String.fromCharCode(10); // '\n'
@JosePedroDias
JosePedroDias / parseXSV.js
Last active August 29, 2015 13:57
Proper TSV/CSV parsing. toObjects uses first line as header and converts lines into objects. sep defaults to '\t', use ',' instead for CSV.
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 August 29, 2015 14:01
serve media (static server with ranged request support)
/**
* serves media files in the directory.
* by default won't refresh on file changes, use nodemon if you want that behavior
* 2014, [email protected]
*/
var http = require('http'),
fs = require('fs'),