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 / 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 / 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 / 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 / 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 / random
Last active July 31, 2021 02:38
simple bash script for generating random bytes
#!/usr/bin/env bash
read -r -d '' usage <<EOF
Usage:
random [options] bytes
random -h|--help
Generate random bytes from /dev/random.
Options:
@jhunterkohler
jhunterkohler / promise.js
Created July 31, 2021 15:25
A partially implemented A+ compliant Promise class
// const GlobalPromise = global.Promise;
// delete global.Promise;
function typename(value) {
if (typeof value == "object" && typeof value != "null") {
let name;
try {
name = value.constructor?.name;
@jhunterkohler
jhunterkohler / table.js
Last active August 1, 2021 05:57
'jest-each'-like tagged template literal tables for pretty JavaScript source code constants
export function mktable(strings, ...args) {
strings = strings[0].split("|");
if (!strings.length || !args.length || args.length % strings.length) {
throw new TypeError("Label and argument count do not match");
}
const labels = new Set();
const table = new Array(args.length / strings.length)
@jhunterkohler
jhunterkohler / functional.js
Last active December 11, 2021 17:34
Some lodash-esc functional utilities; mostly manipulating path strings.
/*
* Copyright (C) 2021 John Hunter Kohler <[email protected]>
*/
/**
* Returns true if `value` is non-primitive (ECMA script standard, i.e., null is
* a primitive, function is an object).
*
* @param {any} value
* @returns {bool}
@jhunterkohler
jhunterkohler / frozen-set.js
Created September 7, 2021 19:04
FrozenSet: An overly simply helper class
export class FrozenSet extends Set {
clone() {
return new FrozenSet(this);
}
}
FrozenSet.prototype.add = undefined;
FrozenSet.prototype.delete = undefined;
FrozenSet.prototype.clear = undefined;
@jhunterkohler
jhunterkohler / regex_operators.h
Last active October 13, 2021 06:10
An overcomplete set of user-defined literals for std::regex.
/*
* Copyright (C) 2021 Hunter Kohler <[email protected]>
*
* WARNING: AUTOGENERATED FILE, DO NOT EDIT DIRECTLY
*/
#ifndef REGEX_OPERATORS_H_
#define REGEX_OPERATORS_H_
#include <string>
#include <regex>