Skip to content

Instantly share code, notes, and snippets.

View jhunterkohler's full-sized avatar
🇺🇸
Working from the USA

Hunter Kohler jhunterkohler

🇺🇸
Working from the USA
  • United States
View GitHub Profile
@jhunterkohler
jhunterkohler / resolvable.js
Created July 30, 2021 10:16
control your promises
export class Resolvable extends Promise {
constructor() {
let _resolve;
let _reject;
super((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});
@jhunterkohler
jhunterkohler / is-cyclic.js
Last active July 29, 2021 03:34
does javascript object have cycles
/**
* @param {any} value
* @param {Set<any> | WeakSet<any>} visited
* @returns {boolean}
*/
function detect(value, visited) {
if (
(typeof value == "object" && value != null) ||
typeof value == "function"
) {
@jhunterkohler
jhunterkohler / deep-copy.js
Last active July 29, 2021 01:07
Deep copy js object
export function deepcopy(value) {
return __deepcopy(value, new WeakMap());
}
export default deepcopy;
const TypedArray = Reflect.getPrototypeOf(Int8Array);
function identity(value) {
return value;
@jhunterkohler
jhunterkohler / function-flow.js
Last active July 23, 2021 23:49
Chain input/outputs of functions together
export function flow(...fn) {
return function (...args) {
let ret = fn[0].apply(this, args);
let index = 1;
let { length } = fn;
while (index < length) {
ret = fn.call(this, ret);
index++;
}
@jhunterkohler
jhunterkohler / cowsay
Last active July 31, 2021 14:39
Very simple cowsay
#!/usr/bin/env bash
set -e
say() {
local -r msg=$(tr -d "\n" <<<"$@")
local -ir len=${#msg}
local hr pad
printf -v hr -- "-%.0s" $(seq $((len > 40 ? 42 : len + 2)))
@jhunterkohler
jhunterkohler / util.js
Created July 3, 2021 14:36
Callable classes in javascript
export function log(value, options) {
return console.dir(value, {
showHidden: true,
depth: null,
colors: true,
...options
});
}
export function isKey(key) {
@jhunterkohler
jhunterkohler / memoize.js
Last active July 4, 2021 11:18
Simple memoizing function and weak value map
const memoize = (() => {
const wvm = new WeakValueMap();
return function (func, hash = JSON.stringify) {
return function () {
const key = hash.apply(this, arguments);
if (wvm.has(key)) {
@jhunterkohler
jhunterkohler / breakable-for-each.js
Created June 30, 2021 02:52
Array with a forEach loop that can be broken out of.
class BreakableArray extends Array {
forEach(callbackFn, thisArg) {
let index = 0;
while(index < this.length) {
let _break = false;
callbackFn.call(thisArg, this[i], index, this, () => _break = true);
if(_break) {
break;
@jhunterkohler
jhunterkohler / env.regex
Last active June 20, 2021 16:40
Environment File Key-Value Pair Regex
^([A-Za-z_][A-Za-z0-9_]*)=(?:([^\s'"#]\S*)|(['"])((?:(?!\3)(?:.|\n)|(?<!\\)(?:\\\\)*\\\3)*)\3|())\s*?(?:#.*)?$
@jhunterkohler
jhunterkohler / running.ascii
Created May 19, 2021 01:58
Guy running down hallway in ascii art. Mildly frightening.
\ /
\ They're behind /
\ you /
] [ ,'|
] [ / |
]___ ___[ ,' |
] ]\ /[ [ |: |
] ] \ / [ [ |: |
] ] ] [ [ [ |: |
] ] ]__ __[ [ [ |: |