Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@Phryxia
Phryxia / dfa.ts
Last active August 26, 2022 05:41
TypeScript implementation of a simple DFA(Deterministic Finite Automata) and NFA(Non-deterministic Finite Automata).
export type StateId = number
export type Char = '0' | '1'
export interface DFA {
statesNumber: number
rules: Record<Char, StateId>[]
startState: StateId
acceptStates: StateId[]
}
@Phryxia
Phryxia / cloneableStringIterator.ts
Last active August 24, 2022 11:18
TypeScript implementation for cloneable string iterator
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,
@Phryxia
Phryxia / disjointSet.ts
Last active August 16, 2024 06:27
TypeScript implementation of disjoint set (union and find algorithm)
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])
@Phryxia
Phryxia / binarySearch.ts
Last active September 20, 2023 09:23
TypeScript implementation of binary search and it's variant (less/less or equal)
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
@Phryxia
Phryxia / useAsyncEffect.ts
Created November 17, 2022 06:28
TypeScript implementation of useAsyncEffect hook for React
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()
@Phryxia
Phryxia / types.ts
Last active December 6, 2022 17:03
Some useful type manipulations
// 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> =
@Phryxia
Phryxia / cartessian.ts
Last active December 6, 2022 17:39
TypeScript implementation of cartessian product which infers its type perfectly.
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>[]
@Phryxia
Phryxia / retry.ts
Last active December 8, 2022 06:28
TypeScript utility which handle promise retrial
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
@Phryxia
Phryxia / useConfirm.tsx
Created January 6, 2023 02:31
Typescript React custom confirm alert hook using Promise
// 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) {
@Phryxia
Phryxia / deepValues.ts
Last active February 21, 2023 06:11
TypeScript implementation of extracting every leaf values of given object into array
/**
* @param obj anything
* @param isSafe whether protect mutual dependency cycle case
* @returns every primitive properties of nested objects or arrays
*/
function deepValues(obj: any, isSafe: boolean = false): any[] {
const out = [] as any[]
const dup = [] as (any[] | object)[]
function traverse(current: any) {