Skip to content

Instantly share code, notes, and snippets.

View atesgoral's full-sized avatar

Ateş Göral atesgoral

View GitHub Profile
@atesgoral
atesgoral / gist:837988
Created February 22, 2011 00:22
Very simple mock library
/**
* Note: Has hard-coded dependency on a CommonJS Unit Test 1.0 assertion module,
* but doesn't have to.
*/
function Mock(intf) {
var expectations, failures, expect = {};
for (var p in intf) {
if (intf.hasOwnProperty(p) && typeof intf[p] === "function") {
(function ($p) {
/**
* Hack to allow jQuery to work within XHTML documents that define an xmlns
*/
/**
* Use the given object to override the given methods in its prototype
* with namespace-aware equivalents
*/
function addNS(obj, methods) {
var proto = obj.constructor.prototype;
function escapeXml(s) {
return s.replace(/([&<>'"])/g,
function (c) {
return "&" + {
"&": "amp",
"<": "lt",
">": "gt",
"'": "apos",
'"': "quot"
}[c] + ";";
function getQueryParams(qs) {
qs = qs.replace(/\+/g, " ");
var params = {},
re = /[?&]?([^=]+)=([^&]*)/g,
tokens;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
#!/usr/local/bin/python2.4
import sys, email, urllib
print 'reQall Router invoked'
msg = email.message_from_file(sys.stdin)
content = msg['subject'].split(': ', 1)[1]
/**
* Left-pad a number with zeros so that it's at least the given
* number of characters long
* @param n The number to pad
* @param len The desired length
*/
function leftPad(n, len) {
return (new Array(len - String(n).length + 1)).join("0").concat(n);
}
function sigFigs(n, sig) {
var mult = Math.pow(10,
sig - Math.floor(Math.log(n) / Math.LN10) - 1);
return Math.round(n * mult) / mult;
}
alert(sigFigs(1234567, 3)); // Gives 1230000
alert(sigFigs(0.06805, 3)); // Gives 0.0681
alert(sigFigs(5, 3)); // Gives 5
Date.prototype.format = function (fmt) {
var date = this;
return fmt.replace(
/\{([^}:]+)(?::(\d+))?\}/g,
function (s, comp, pad) {
var fn = date["get" + comp];
if (fn) {
var v = (fn.call(date) +
function xmlEscape(s) {
return s.replace(/[<>&"]/g, function (c) {
return "&"
+ { "<": "lt", ">": "gt", "&": "amp", "\"": "quot" }[c]
+ ";";
});
}
var xml = [ "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ];