Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View dbrockman's full-sized avatar

David Brockman dbrockman

View GitHub Profile
@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;
//
// 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)
//
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();
export default createFromSeed(randomSeed());
export function createFromSeed(seed) {
if (!Number.isInteger(seed) || seed <= 0) {
throw new TypeError('seed must be an int > 0');
}
let value = seed;
const next = () => {
const hi = 16807 * (value >> 16);
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) {
/**
Axial coordinates
# convert cube to axial
q = x
r = z
# convert axial to cube
x = q
z = r
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)];
}
### ALIAS
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias §='exit'
# Get week number
alias week='date +%V'

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: