Skip to content

Instantly share code, notes, and snippets.

@amdilley
amdilley / lruCache.test.ts
Created September 28, 2025 17:56
Extension of Map class to order entries from oldest to newest
import { expect, it } from "vitest";
import { LRUCache } from "./lruCache";
it("should move entry to end of map on access", () => {
const cache = new LRUCache();
cache.set("a", 1).set("b", 2).set("c", 3);
expect([...cache.entries()]).toEqual([
@amdilley
amdilley / partition.test.ts
Created May 31, 2025 19:58
Partitions an array of items according to a sequence of filters. If an item fails all filters then it is added to the last returned array.
import { expect, it } from "vitest";
import { partition } from "./partition";
it("should divide array by provided filters", () => {
expect(
partition(
[1, "2", undefined, "test", {}],
(item) => typeof item === "number",
(item) => typeof item === "string"
@amdilley
amdilley / sort.test.ts
Last active September 14, 2025 06:11
Sorts an array according to a list of comparators. If the first comparator evaluates two items as equal, the next comparator is applied and so on.
import { expect, it } from "vitest";
import { sort } from "./sort";
it("should sort based on multiple comparators", () => {
const users = [
{ name: "Alice", age: 39, active: true },
{ name: "Caroline", age: 25, active: false },
{ name: "Alice", age: 38, active: false },
{ name: "Bob", age: 39, active: true },
@amdilley
amdilley / filterMap.ts
Created March 9, 2025 17:07
A function the combines `.filter(...).map(...)` into a single array traversal.
/**
* Takes array of items and filter and\
* mapping functions to transform the\
* array in a single traversal.
* @param items array of items
* @param filter narrowing function
* @param mapper mapping function
* @returns function to filter and map items
* @example
* const isDefined = (n: number | undefined) => n !== undefined;
@amdilley
amdilley / mapAsync.ts
Last active February 15, 2025 09:58
Optimized version of Promise.all + Array.prototype.map that maps as each promise is resolved.
/**
* An optimized version of Promise.all + map.
* When needing to map results of Promise.all
* we need to wait for all promised to resolve
* and then mapping from start to end can
* begin. There is no need to wait for all
* promises to resolve. Instead we can map each
* promise's result as soon as it has resolved.
* @param promises array of promises to map
* @param mapper mapping to apply to each resolved promise
@amdilley
amdilley / poll.js
Last active September 14, 2025 14:33
Runs callback function at specified intervals until timeout is reached or signal is aborted.
type Options = {
timeout?: number;
interval?: number;
signal?: AbortSignal;
};
type PromiseExecutor<T> = (
resolve: (value: T) => void,
reject: (reason?: any) => void
) => void;
@amdilley
amdilley / waitFor.js
Last active June 19, 2022 00:52
Initializes a mutation observer instance on document.body (by default) that executes the provided callback as soon as the target selector is detected.
/**
* Initializes a mutation observer instance on
* document.body (by default) that executes the
* provided callback as soon as the target selector
* is detected.
* @param {String} selector target CSS selector
* @param {Function} callback function to execute
* @param {Object} options
* @param {HTMLElement} options.observeTarget parent node to observe
* @param {Boolean} options.passAllElements whether callback gets first or all elements
@amdilley
amdilley / generic-fn-type-mapper.ts
Created February 15, 2020 05:42
Generic function mapper from one type to another
// fn: T -> K
type MapFn<T,K> = {
(param: T): K;
}
// fn: T -> T
type IsoFn<T> = MapFn<T,T>;
// Example
interface User {
@amdilley
amdilley / async-fn-wrapper.ts
Created February 2, 2020 01:35
Wraps any function type to preserve call parameters but wrap return type in promise
// @example:
// interface ParseFn {
// (s: string): number;
// }
//
// const parseServerResponse: Async<ParseFn> = ...
type Async<T extends (...args: any) => any> =
(...args: Parameters<T>) => Promise<ReturnType<T>>;
type IsoFn<T> = {
(param: T): T;
}