Skip to content

Instantly share code, notes, and snippets.

View dbrockman's full-sized avatar

David Brockman dbrockman

View GitHub Profile
function cachedAsyncFn(fn) {
var cached, queue;
return function (callback) {
if (cached) {
process.nextTick(function () {
callback.apply(null, cached);
});
} else if (queue) {
queue.push(callback);
} else {
@dbrockman
dbrockman / jenkins-hash.js
Last active December 15, 2015 21:39
Jenkins hash function on strings
function jenkins_hash(s) {
var hash = 0, i = 0, l = s.length;
for (; i < l; ++i) {
hash += s.charCodeAt(i);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
@dbrockman
dbrockman / randomString.js
Created June 6, 2013 18:26
Function that returns a random string of specified length.
function randomString(length) {
var s = ((performance.now() % 1) + Math.random()).toString(35);
return s.substr(-Math.max(0, Math.min(length, s.length - 2)));
}
@dbrockman
dbrockman / getBoundsAtLatLngWithZoom.js
Created December 10, 2013 22:03
Google maps latlng bounds with center and zoom level in map viewport
function getBoundsAtLatLngWithZoom(map, center, zoom) {
var p = map.getProjection();
if (!p) {
return null;
}
var el = $(map.getDiv());
var zf = Math.pow(2, zoom) * 2;
var dw = (el.width() | 0) / zf;
var dh = (el.height() | 0) / zf;
var cpx = p.fromLatLngToPoint(center);
@dbrockman
dbrockman / flatten2D.js
Created December 16, 2013 14:46
Flatten 2D array
function flatten2D(array) {
return array.reduce(function (result, item) {
return result.concat(item);
}, []);
}
var digits = '0123456789' +
'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'-_';
var radix = digits.length;
function shorten_number(n) {
var s = '';
while (n > 0) {
s = digits.charAt(n % radix) + s;
@dbrockman
dbrockman / countTilesInRegion.m
Created February 11, 2014 08:56
How to count the number of tiles in a map region with min/max zoom level
- (NSUInteger)countTilesInRegionSouthWest:(CLLocationCoordinate2D)southWest
northEast:(CLLocationCoordinate2D)northEast
minZoom:(int)minZoom
maxZoom:(int)maxZoom
{
double minLat = southWest.latitude;
double maxLat = northEast.latitude;
double minLon = southWest.longitude;
double maxLon = northEast.longitude;

Keybase proof

I hereby claim:

  • I am dbrockman on github.
  • I am dbrckmn (https://keybase.io/dbrckmn) on keybase.
  • I have a public key whose fingerprint is 1D11 8D7A 9177 F946 DD3A C5A7 4EEA C966 0D82 F1EB

To claim this, I am signing this object:

### ALIAS
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias §='exit'
# Get week number
alias week='date +%V'
function modulo(a, b) {
var r = a % b;
return (r * b < 0) ? r + b : r;
}
function nthArrayItem(array, n) {
return array[modulo(n, array.length)];
}