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:11261918
Created April 24, 2014 17:04
Find the closest match in an array of numbers
function findClosestToDesired(values, desired) {
return values
.slice(0)
.sort(function (a, b) {
return Math.abs(desired - a) - Math.abs(desired - b);
})
.shift();
}
// findClosestToDesired([ 1, 3, 7, 15 ], 6) === 7
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Balls.</title>
<style>
canvas { background-color: black; }
</style>
</head>
<body>

Keybase proof

I hereby claim:

  • I am atesgoral on github.
  • I am atesgoral (https://keybase.io/atesgoral) on keybase.
  • I have a public key whose fingerprint is 8D59 5FAD E029 0A18 3357 08BF EA57 0ED6 6ACF 59B3

To claim this, I am signing this object:

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 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;