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
| // You need to hook based modal system. If it's not, you may tweak upon your system. | |
| import { useModal } from './ModalProvider' | |
| export interface UseConfirmOptions { | |
| title: string | |
| confirmText: string | |
| cancelText: string | |
| } | |
| export function useConfirm(props: UseConfirmOptions) { |
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 RetryOption { | |
| name?: string | |
| isintermediateLogged: boolean | |
| interval?: number // second | |
| maxTries: number | |
| } | |
| export async function retry<T>( | |
| job: () => Promise<T> | T, | |
| { name, isintermediateLogged, interval = 0, maxTries }: RetryOption |
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 CartessianType<T> = | |
| T extends [] ? | |
| [] : | |
| T extends [(infer R)[], ...infer Rest] ? | |
| [R, ...CartessianType<Rest>] : | |
| T extends (infer R)[][] ? | |
| [R] : | |
| never | |
| function cartessianProduct<T extends unknown[]>(...sets: T): CartessianType<T>[] |
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
| // why [A]? check https://github.com/microsoft/TypeScript/issues/31751 | |
| type Equal<A, B> = | |
| [A] extends [B] ? | |
| [B] extends [A] ? | |
| true : | |
| false : | |
| false | |
| // [A, ...?] -> A | |
| type First<T> = |
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 { DependencyList, EffectCallback, useEffect } from 'react' | |
| type Destructor = () => void | |
| type AsyncDestructor = () => Promise<void> | |
| type AsyncEffectCallback = () => Promise<void | Destructor | AsyncDestructor> | |
| export function useAsyncEffect(callback: EffectCallback | AsyncEffectCallback, deps?: DependencyList) { | |
| useEffect(() => { | |
| const mayPromise = callback() |
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 function createExactFinder<T>(comp: (a: T, b: T) => number) { | |
| return (list: T[], target: T) => { | |
| let l = 0 | |
| let r = list.length - 1 | |
| while (l <= r) { | |
| const m = Math.ceil((l + r) / 2) | |
| const c = comp(list[m], target) | |
| if (c === 0) return m |
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 function createDisjointSet(initialSize: number) { | |
| const roots = Array.from(new Array(initialSize), (_, index) => index) | |
| const sizes = roots.map(() => 1) | |
| let groups = initialSize | |
| // return the id of the set which given index-th element exists. | |
| // but set id may changes if further union operations are done. | |
| function find(index: number): number { | |
| if (roots[index] === index) return index | |
| return roots[index] = find(roots[index]) |
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 interface StringIterator extends IterableIterator<string> { | |
| clone(): StringIterator | |
| } | |
| export function createStringIterator(s: string, index: number = 0): StringIterator { | |
| return { | |
| next(): IteratorResult<string> { | |
| if (index >= s.length) { | |
| return { | |
| done: true, |
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 type StateId = number | |
| export type Char = '0' | '1' | |
| export interface DFA { | |
| statesNumber: number | |
| rules: Record<Char, StateId>[] | |
| startState: StateId | |
| acceptStates: StateId[] | |
| } |
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 { cloneElement, PropsWithChildren, ReactElement } from 'react' | |
| export default function Link({ children, href }: PropsWithChildren<any>) { | |
| if (typeof children === 'object') { | |
| const reactChild = children as ReactElement | |
| if (reactChild.type === 'a' && href) { | |
| return cloneElement(reactChild, { href }) | |
| } | |
| } |