Skip to content

Instantly share code, notes, and snippets.

View dbrockman's full-sized avatar

David Brockman dbrockman

View GitHub Profile
@dbrockman
dbrockman / degrees-radians.h
Created February 12, 2013 21:52
Convert degrees <-> radians C macros
// Converts degrees to radians.
#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
// Converts radians to degrees.
#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
@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 / wrapLineAtDivisibleColumn.js
Last active December 24, 2019 22:05
Wrap line at the highest column (<= 80) divisible by the length of the line
function wrapLineAtDivisibleColumn(line) {
var column = 80;
while (line.length / column % 1) --column;
return line.replace(new RegExp('.{' + column + '}(?!$)', 'g'), '$&\n');
}
//
// TargetClosure is a bridge from closures to objc targets/selectors
// Create a TargetClosure with a closure and pass it as the target, then pass target.selector as the selector.
//
// Example:
//
// let target = TargetClosure { doYourThing() }
// oldSchoolFunction(target: target, action: target.selector)
//
const encode_regex = /[\+=\/]/g;
const decode_regex = /[\._\-]/g;
// Buffer -> Base64 String -> Url Safe Base64 String
export function encode(buffer) {
return buffer.toString('base64').replace(encode_regex, encodeChar);
}
// Url Safe Base64 String -> Base64 String -> Buffer
export function decode(string) {
@dbrockman
dbrockman / insertAt-removeAt.js
Created March 7, 2013 18:29
JS array insertAt and removeAt function
function insertAt(arr, val, i) {
arr.splice(i, 0, val);
}
function removeAt(arr, i) {
return arr.splice(i, 1).length === 1;
}
function genericInsertAt(arr, val, i) {
Array.prototype.splice.call(arr, i, 0, val);
}
@dbrockman
dbrockman / remove-substring.js
Created January 9, 2018 09:43
Remove all occurrences of a substring from a string
function removeSubstring(string, substring) {
let result = string;
if (substring) {
let index = result.indexOf(substring);
while (index >= 0) {
result = result.substring(0, index) + result.substring(index + substring.length);
index = result.indexOf(substring, index);
}
}
return result;
@interface NSIndexSet (Operations)
// http://en.wikipedia.org/wiki/Union_(set_theory)
- (NSIndexSet *)unionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Intersection_(set_theory)
- (NSIndexSet *)intersectionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement
- (NSIndexSet *)relativeComplementIn:(NSIndexSet *)universe;
public class Queue<T> {
public private(set) var size: Int = 0
private var head: QueueNode<T>? = nil
private var tail: QueueNode<T>? = nil
public func enqueue(_ value: T) {
let newTail = QueueNode(value: value)
if let oldTail = tail {
newTail.prev = oldTail
oldTail.next = newTail
@dbrockman
dbrockman / promise.js
Created August 11, 2016 07:58
Untested Promise implementation that I wrote just for fun. Almost certainly completely broken :)
import Queue from './queue';
const STATE_PENDING = 'pending';
const STATE_RESOLVED = 'resolved';
const STATE_REJECTED = 'rejected';
const states = new WeakMap();
const values = new WeakMap();
const queues = new WeakMap();