Skip to content

Instantly share code, notes, and snippets.

View atesgoral's full-sized avatar

Ateş Göral atesgoral

View GitHub Profile
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" ];
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 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
/**
* 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);
}
#!/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]
function getQueryParams(qs) {
qs = qs.replace(/\+/g, " ");
var params = {},
re = /[?&]?([^=]+)=([^&]*)/g,
tokens;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
function escapeXml(s) {
return s.replace(/([&<>'"])/g,
function (c) {
return "&" + {
"&": "amp",
"<": "lt",
">": "gt",
"'": "apos",
'"': "quot"
}[c] + ";";
/**
* 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;
@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) {
@atesgoral
atesgoral / LICENSE.txt
Created May 21, 2011 08:48 — forked from 140bytes/LICENSE.txt
Variable-argument string formatter with {token} syntax that supports object properties and argument indices
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Ates Goral <http://magnetiq.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE