This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| createEntityAdapter, | |
| createSlice, | |
| configureStore, | |
| EntitySelectors, | |
| EntityId, | |
| } from '@reduxjs/toolkit'; | |
| type Book = { bookId: string; title: string; authorId: string } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] }))); |