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 { describe, it, expect, beforeEach } from "vitest"; | |
import { DoubleEndedList, Positions } from "./DoubleEndedList.ts"; | |
describe("DoubleEndedList", () => { | |
let list: DoubleEndedList<number>; | |
beforeEach(() => { | |
list = new DoubleEndedList<number>(); | |
}); |
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
const createPromiseResolvers = <T = void>() => { | |
const noop = () => void 0; | |
let _resolve: (value: T | PromiseLike<T>) => void = noop; | |
let _reject: (reason?: unknown) => void = noop; | |
const createPromise = () => | |
new Promise<T>((res, rej) => { | |
_resolve = res; | |
_reject = rej; |
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
/** | |
* Runs a callback as soon as possible after next repaint. | |
* | |
* This can be helpful for operations that need to wait until the | |
* next repaint is complete before performing a follow-up task. | |
* When you need a small delay after the next repaint, perhaps for | |
* batching DOM updates or deferring non-critical tasks right after a | |
* frame is rendered. | |
*/ | |
export const runAfterRepaint = (fn: () => void): void => { |
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 ObjectPool<T> { | |
private _pool: T[] = []; | |
private readonly _createObject: () => T; | |
private readonly _resetObject: (obj: T) => void; | |
private readonly _maxPoolSize: number; | |
constructor(creator: () => T, reset: (obj: T) => void, maxPoolSize: number = 10) { | |
this._createObject = creator; | |
this._resetObject = reset; |
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 ArrayPool<T> { | |
private _pool: T[][] = []; | |
private readonly _maxPoolSize: number; | |
constructor(maxPoolSize: number = 10) { | |
this._maxPoolSize = maxPoolSize; | |
} | |
// Get an array from the pool, or create a new one if the pool is empty |
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 Success<T> = { | |
data: T; | |
error: null; | |
}; | |
type Failure<E> = { | |
data: null; | |
error: E; | |
}; |
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
const hasCorrectOrder = (parent: number, child: number, mode: "min" | "max"): boolean => { | |
if (mode === "max") return parent >= child; | |
return parent <= child; | |
}; | |
const heapifyUp = (heap: number[], mode: "min" | "max", startIdx?: number): void => { | |
let idx = startIdx ?? heap.length - 1; | |
let parentIndex = Math.floor((idx - 1) / 2); | |
while (idx >= 0 && parentIndex >= 0 && !hasCorrectOrder(heap[parentIndex], heap[idx], mode)) { |
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
declare const __NOMINAL_TYPE__ = unique symbol; | |
export type Nominal<T, ID> = T & { | |
readonly [__NOMINAL_TYPE__]: ID; | |
} | |
// Example usage: | |
type PostID = Nominal<string, 'PostID'>; | |
type UserID = Nominal<string, 'UserID'>; |
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 AnyObject = Record<keyof any, any>; | |
const isPlainObject = (x: unknown): x is AnyObject => { | |
return (x && typeof x === "object" && x.constructor === Object) as boolean; | |
}; | |
type Merged<T, S> = { [K in keyof S]: S[K] } & { | |
[K in keyof T]: K extends keyof S ? S[K] : T[K]; | |
}; |
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 AnyObject = Record<keyof any, any>; | |
const isPlainObject = (x: unknown): x is AnyObject => { | |
return (x && typeof x === "object" && x.constructor === Object) as boolean; | |
}; | |
type Merged<T, S> = { [P in keyof S]: S[P] } & { | |
[K in keyof T]: K extends keyof S | |
? S[K] extends AnyObject | |
? T[K] extends AnyObject |
NewerOlder