Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@Phryxia
Phryxia / roroOptions.ts
Created November 18, 2024 13:58
Smart option assigining code for TypeScript
/**
* Inject all key values written in userOptions
* If a key value doesn't exist, use the value from defaultOptions
* In this case, even if userOptions[key] is undefined, it overwrites if given
* If you want behavior identical to Object.assign, set `isUndefinedIgnored` to `true`
*
* @param userOptions 사용자가 입력할 부분적인 값
* @param defaultOptions 기본적으로 제공할 값
* @param isUndefinedIgnored undefined 값을 무시할 지 여부
* @returns
@Phryxia
Phryxia / usePromise.ts
Created September 24, 2024 05:51
Listen for promises and give the last promise value as react state
import { useLayoutEffect, useRef, useState } from 'react'
export function usePromise<T>(promise: Promise<T>) {
const [isLoading, setIsLoading] = useState(true)
const [value, setValue] = useState<T>()
const valueVersion = useRef(0)
useLayoutEffect(() => {
const valueOnClosure = ++valueVersion.current
@Phryxia
Phryxia / useAsyncEffect.ts
Created September 23, 2024 15:42
Convenient useAsyncEffect for react.js
import { useEffect } from 'react';
type UseAsyncEffectCleaner = () => Promise<void> | void;
type UseAsyncEffectCallback = () =>
| Promise<UseAsyncEffectCleaner | void>
| UseAsyncEffectCleaner
| void;
export function useAsyncEffect(fn: UseAsyncEffectCallback, deps?: any[]) {
return useEffect(() => {
@Phryxia
Phryxia / treeIterator.ts
Last active September 19, 2024 05:33
TypeScript implementation of tree traversing iterator
interface TreeNode<T> {
value: T
children?: TreeNode<T>[]
}
function preTraverse<T>(node: TreeNode<T>): IterableIterator<T> {
const stack = [{ node, index: -1 }]
return {
[Symbol.iterator]() {
@Phryxia
Phryxia / deepObjectType.ts
Last active June 28, 2024 06:06
TypeScript snippets for nested object types
export type DeepKeys<T, K = keyof T, R extends string[] = []> =
K extends keyof T ?
T[K] extends object ?
T[K] extends unknown[] | [unknown, ...unknown[]] ?
[...R, K] | [...R, K, KeyOfArrayLike<T[K]>]
: [...R, K] | [...R, K, ...DeepKeys<T[K], keyof T[K], R>]
: [...R, K]
: []
type KeyOfArrayLike<T> =
@Phryxia
Phryxia / keyOfArrayLike.ts
Created June 28, 2024 05:46
TypeScript snippet for extracting key type of array and tuple
type KeyOfArrayLike<T> =
T extends [] ?
never
: T extends [unknown, ...infer R] ?
R['length'] | KeyOfArrayLike<R>
: T extends unknown[] ?
number
: never
@Phryxia
Phryxia / matchTree.ts
Last active June 26, 2024 08:28
TypeScript implementation of modified KMP algorithm for tree model
export type Tree<T, K> = T & {
id: K
children: Tree<T, K>[]
}
// Based on KMP algorithm
export function matchTree<T, K>(
root: Tree<T>,
prop: (value: Tree<T>) => K,
selector: K[],
@Phryxia
Phryxia / curry.ts
Created May 27, 2024 03:00
TypeScript implementation of currying for generalized axis
type Remove<R extends unknown[], I extends number, Out extends unknown[] = [], Count extends unknown[] = []>
= R extends []
? Out
: R extends [infer First, ...infer Rest]
? Count['length'] extends I
? Remove<Rest, I, Out, [...Count, 0]>
: Remove<Rest, I, [...Out, First], [...Count, 0]>
: never
type X = Remove<['a', 'b', 'c'], 1>
@Phryxia
Phryxia / recursiveArray.ts
Last active March 10, 2024 04:01
TypeScript implementation of Infinite Dimension Array
type RecursiveArray<T> = (T | RecursiveArray<T>)[]
type UtilizedRecursiveArray<T> = RecursiveArray<T> & {
get(...indices: number[]): T | RecursiveArray<T>
set(value: T, ...indices: number[]): T
}
function utilize<T>(rarr: RecursiveArray<T>): UtilizedRecursiveArray<T> {
const res = rarr as any
@Phryxia
Phryxia / randomDataStructure.ts
Last active March 2, 2024 06:10
Random data structure using bless of dimensionality
interface BinaryTrieNode<T> {
value?: T
lc?: BinaryTrieNode<T>
rc?: BinaryTrieNode<T>
}
class BinaryTrie<T> {
root: BinaryTrieNode<T> = {}
constructor(public readonly depth: number) {}