Skip to content

Instantly share code, notes, and snippets.

View mimshins's full-sized avatar
👾
Invading Codebases

Mostafa Shamsitabar mimshins

👾
Invading Codebases
View GitHub Profile
@mimshins
mimshins / DoubleEndedList.test.ts
Last active July 2, 2025 16:18
A TypeScript implementation of Redis' List data structure
import { describe, it, expect, beforeEach } from "vitest";
import { DoubleEndedList, Positions } from "./DoubleEndedList.ts";
describe("DoubleEndedList", () => {
let list: DoubleEndedList<number>;
beforeEach(() => {
list = new DoubleEndedList<number>();
});
@mimshins
mimshins / create-promise-resolvers.ts
Created February 15, 2025 07:54
An utility function to create a promise and its resolvers.
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;
@mimshins
mimshins / execution-flow-helpers.ts
Last active June 2, 2025 13:51
DOM Execution Flow Helpers
/**
* 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 => {
@mimshins
mimshins / ObjectPool.ts
Last active October 30, 2024 18:35
Simple Object Pool Implementation
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;
@mimshins
mimshins / ArrayPool.ts
Last active October 30, 2024 18:36
Simple Array Pool Implementation
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
@mimshins
mimshins / resolve-throwable.ts
Last active March 20, 2025 09:14
An utility function to help resolve throwables in a declarative way.
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};
@mimshins
mimshins / heapify.ts
Last active February 12, 2025 11:45
Heapify: creating a heap from array
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)) {
@mimshins
mimshins / nominal.ts
Last active January 4, 2025 12:16
Nominal Types
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'>;
@mimshins
mimshins / shallow-merge.ts
Created April 7, 2024 12:00
Type-safe shallow merge of two objects
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];
};
@mimshins
mimshins / deep-merge.ts
Created April 7, 2024 11:59
Type-safe deep merge of two objects
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