Skip to content

Instantly share code, notes, and snippets.

Avatar
🕉️
all good

José Pedro Dias JosePedroDias

🕉️
all good
View GitHub Profile
@JosePedroDias
JosePedroDias / jsonp.js
Created Aug 28, 2015
JSONP (not perfect)
View jsonp.js
var pendingsJsonpCbs = [];
var jsonp = function(uri, cb) {
pendingsJsonpCbs.push(cb);
var scriptEl = document.createElement('script');
scriptEl.setAttribute('type', 'text/javascript');
scriptEl.setAttribute('src', uri + '&jsonp=parseJSON');
document.head.appendChild(scriptEl);
};
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 / 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) {}
};
@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;
You can’t perform that action at this time.