Skip to content

Instantly share code, notes, and snippets.

View Phryxia's full-sized avatar

Phryxia Phryxia

  • South Korea
View GitHub Profile
@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) {}
@Phryxia
Phryxia / combinateWithoutReplacement.ts
Created January 17, 2024 09:41
TypeScript implementation of combination without replacement (i.e. Heap's Algorithm)
export function* combinateWithoutReplacement<T>(candidates: T[]) {
const indices = candidates.map((_, i) => i)
yield indices.map((i) => candidates[i])
const c = indices.map(() => 0)
let i = 1
while (i < indices.length) {
if (c[i] < i) {
if (i % 2 === 0) {
@Phryxia
Phryxia / combinateWithReplacement.ts
Last active January 17, 2024 09:29
TypeScript implementation of combination with replacement of given elements with arbitrary length.
export function* combinateWithReplacement<T>(candidates: T[], length: number) {
if (length <= 0) return
for (let i = 0; i < candidates.length ** length; ++i) {
const result = new Array<T>(length)
let j = i
for (let k = length - 1; k >= 0; --k) {
result[k] = candidates[j % candidates.length]
j = Math.floor(j / candidates.length)
}
@Phryxia
Phryxia / anchor.tsx
Last active January 17, 2024 07:25
React wrapper for vanilla anchor element considering `href` to internal pages
import { AnchorHTMLAttributes, MouseEvent as ReactMouseEvent, useCallback } from 'react'
const InternalUrlRegexp = /^\.?\//
const StartingDotRegexp = /^\./
export function Anchor({
children,
href,
onClick,
...rest
@Phryxia
Phryxia / permutation.ts
Created January 7, 2024 13:34
TypeScript implementation of permutation of given elements with specific length.
function* permutate<T>(domain: T[], length: number) {
function* recurse(stack: T[] = [], depth: number = 0): Generator<T[]> {
if (depth >= length) {
yield stack.slice()
return
}
for (const element of domain) {
stack[depth] = element
yield* recurse(stack, depth + 1)