Skip to content

Instantly share code, notes, and snippets.

View jtmthf's full-sized avatar

Jack Moore jtmthf

View GitHub Profile
type NestedMap<K extends any[], V> = Map<K[number], NestedMap<K, V>>;
export class MultiKeyMap<K extends any[], V> implements Map<K, V> {
#data: NestedMap<K, V> = new Map();
#depth = 0;
constructor(iterable?: [K, V][]) {
if (iterable) {
for (const [keys, value] of iterable) {
this.set(keys, value);
function* chunk<T>(
source: Iterable<T>,
size = 1,
): Iterable<IterableIterator<T>> {
const iterator = source[Symbol.iterator]();
let done = false;
function* take() {
let count = 0;
while (count++ < size) {
interface LinkedList<T> {
readonly value: T;
readonly next?: LinkedList<T>;
}
function linkedListFromIterable<T>(
source: Iterable<T>,
): LinkedList<T> | undefined {
const [first, iterable] = head(source);
if (first.done) {
class DataRow<T> {
private constructor(public readonly value: T) {}
static of<T>(value: DataRow<T>): DataRow<T>;
static of<T>(value: T): DataRow<T>;
static of<T>(value: T | DataRow<T>): DataRow<T> {
return value instanceof DataRow ? value : new DataRow(value);
}
}
export const $ = document.querySelector.bind(document);
export const $$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name: string, fn: EventListenerOrEventListenerObject) {
this.addEventListener(name, fn);
};
(NodeList.prototype as any).__proto__ = Array.prototype;
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
import {
createEntityAdapter,
createSlice,
configureStore,
EntitySelectors,
EntityId,
} from '@reduxjs/toolkit';
type Book = { bookId: string; title: string; authorId: string }
import { useRef, useCallback, DependencyList } from 'react';
function useBlockingCallback<T extends (...args: any[]) => any>(
callback: T,
initiallyBlocked = false,
deps: DependencyList = [],
): [T, (blocked?: boolean) => void] {
const blockRef = useRef(initiallyBlocked);
const handler = useCallback(
import { useRef, useCallback, DependencyList } from "react";
export function useCallbackOnce<T extends (...args: any[]) => any>(
callback: T,
deps: DependencyList
): T {
const calledRef = useRef(false);
const handler = useCallback(
((...args) => {
import { JsonValue } from "type-fest";
function toSerializable(value: unknown): JsonValue | undefined {
switch (typeof value) {
case "string":
case "boolean":
return value;
case "number":
return !isFinite(value) ? null : value;
case "object": {
@jtmthf
jtmthf / pick.ts
Created September 24, 2020 23:17
export const pick = <
T extends Record<keyof unknown, unknown>,
K extends keyof T
>(
object: T,
paths: readonly K[],
): Pick<T, K> =>
Object.assign({}, ...paths.map((path) => ({ [path]: object[path] })));