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); | |
}; |
View callbackFnCatchingExceptions.js
var functionWithCallbackWorkflow = function(arg1, arg2, cb) { | |
try { | |
// DO STUFF, generating res | |
} catch (ex) { | |
return cb(ex); | |
} | |
cb(null, res); | |
}; |
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 |
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; | |
}; |
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' |
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; |
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'), |
OlderNewer