Skip to main content

General Utilities and Color Helpers

Color Helpers

Built on colord (with the names, mix, and lab plugins).

ExportSignatureDescription
isValidColor(color: AnyColor) => booleanWhether a color string/object is parseable.
toHexColor(color: AnyColor) => stringConverts to "#rrggbb".
toRgbColor(color: AnyColor) => RgbaColorConverts to { r, g, b, a }.
toHslColor(color: AnyColor) => HslaColorConverts to { h, s, l, a }.
toHsvColor(color: AnyColor) => HsvaColorConverts to { h, s, v, a }.
getColorDifference(firstColor: AnyColor, secondColor: AnyColor) => numberDelta E distance between two colors (lower = more similar).
convertHslToHex(hslColor: HslColor) => stringConverts an HSL object to hex.
setColorAlpha(color: AnyColor, alphaValue: number) => stringReturns color with its alpha channel set (hex output).
mixColor(baseColor: AnyColor, blendColor: AnyColor, blendRatio: number) => stringMixes two colors (blendRatio 0 = pure base, 1 = pure blend).
convertTransparentToOpaque(color: AnyColor, alphaValue: number, backgroundColor?: string) => stringFlattens a translucent color onto a background (default background "#ffffff") into an equivalent opaque hex color.
isWhiteColor(color: AnyColor) => booleanWhether a color is exactly white.
getColorName(color: string) => stringNearest named color from the built-in colorEntries table (exact match first, then RGB+HSL distance).
getColorPalette(color: string) => Map<ColorNumber, string>Generates (and caches, up to 100 entries) a full 50950 swatch ramp matched to color's hue/saturation, anchored on the nearest built-in colorPalettes family.

Data constants

ExportTypeDescription
colorEntriesColorEntry[]The [hex, name] lookup table backing getColorName.
colorNameMapMap<string, string>colorEntries indexed by hex for exact-match lookups.
colorPalettesColorPalette[]The built-in reference palette families (Red, Orange, …) getColorPalette adapts to match an arbitrary input color.
colorPaletteMapMap<PresetColor, Map<ColorNumber, string>>colorPalettes indexed by kebab-case palette name, then by swatch number, for direct lookup without re-deriving a palette.

Function and Task Helpers

ExportSignatureDescription
AwaitableFnInvocationOptions<TResult, TContext>{ onInvoke?; onSuccess?; onError?; onFinally? }Lifecycle hooks accepted by invokeAwaitableFn.
isAsyncFunction(fn: Function) => fn is (...args: any[]) => Promise<any>Whether a function is async (or an async generator).
invokeAwaitableFn<TArgs, TResult, TContext>(fn, args: TArgs, options: AwaitableFnInvocationOptions<TResult, TContext>) => Promise<TResult>Calls fn(...args) and runs onInvoke / onSuccess / onError / onFinally hooks around it; synchronous (non-Promise) returns skip the hooks entirely.
identity<T>(value: T) => TReturns its argument unchanged.
createThrowNotImplementedFn(feature?: string) => () => neverBuilds a function that throws a "not implemented" error when called.
throwNotImplemented(feature?: string) => neverThrows a "not implemented" error immediately.
generateId() => stringReturns a 16-character cuid2 id. Fingerprinted with a device-derived value once FingerprintJS resolves (async, browser-only); falls back to a fixed fingerprint until then and in non-browser/test environments.
scheduleMicrotask(task: () => void) => voidRuns task on the microtask queue (queueMicrotask, falling back to Promise.resolve().then).

Cache

ExportSignatureDescription
lru<T = any>(max?: number, ttl?: number, resetTTL?: boolean) => LRU<T> (from tiny-lru)Factory for an O(1) LRU cache (default max: 1000, ttl: 0 = no expiration). Used internally by getColorPalette.
LRU<T = any>class LRU<T> { get, set, has, delete, clear, keys, values, entries, … } (from tiny-lru)The cache class lru() constructs; constructing it directly skips lru()'s parameter validation.

String Case and Template Helpers

ExportSignatureDescription
constantCase(value: string) => stringConverts to CONSTANT_CASE (via snakeCase(value).toUpperCase()).
stringify(value: unknown, emptyForNullish?: boolean) => stringConverts any value to a display string; null/undefined become "" by default (emptyForNullish: true), or the literal "null"/"undefined" when false. Falls back to JSON.stringify for objects, String(value) if that throws.

Common Utilities from lib.ts

This module re-exports selected utilities from radashi (a Radash fork), plus a few direct re-exports (cloneDeep from klona, decodeQueryString/encodeQueryString from qs), gathered into one place.

Type Predicates

All (value: unknown) => value is T unless noted.

ExportDescription
isArrayArray.
isBigIntbigint.
isBooleanboolean.
isDateDate.
isEmpty(value: unknown) => boolean — empty array/string/object/Map/Set/nullish.
isErrorError.
isFloatA non-integer number.
isFunctionFunction.
isIntA number that is an integer (Number.isInteger with a narrower type).
isIntStringA string that round-trips through an integer, e.g. "0".
isMapMap.
isNullishnull | undefined.
isNumbernumber.
isObjectAny non-primitive object.
isPlainObjectA plain {}-style object (excludes class instances, arrays, Map/Set, …).
isPrimitive(value: unknown) => boolean — any JS primitive.
isPromisePromiseLike<unknown>.
isRegExpRegExp.
isSetSet.
isStringstring.
isSymbolsymbol.
isUndefinedundefined.
isWeakMapWeakMap.
isWeakSetWeakSet.

Object Helpers

ExportSignatureDescription
assign<TInitial, TOverride>(initial: TInitial, override: TOverride) => Assign<TInitial, TOverride>Recursively merges override onto initial (plain objects merge deeply; other values are replaced).
get<TDefault = unknown>(value: any, path: string, defaultValue?: TDefault) => TDefaultReads a dot-path from a nested object, e.g. get(obj, "a.b.0.c").
omit<T, TKeys extends keyof T>(obj: T, keys: readonly TKeys[]) => Omit<T, TKeys>Returns a shallow copy without the given keys.
pick<T extends object, F>(obj: T, filter: F) => Pick<T, …>Returns a shallow copy with only the given keys (or a key-filter predicate).
set<T extends object, K>(initial: T, path: string, value: K) => TReturns a copy of initial with the dot-path path set to value.
cloneDeep<T>(value: T) => T (from klona)Deep-clones a value (objects, arrays, Date, RegExp, Map, Set).

Collection Helpers

ExportSignatureDescription
first<TArray, TDefault = undefined>(array: TArray, defaultValue?: TDefault) => …First element, or defaultValue when empty.
last<TArray, TDefault = undefined>(array: TArray, defaultValue?: TDefault) => …Last element, or defaultValue when empty.
max(array: readonly number[]) => number | null, or <T>(array: readonly T[], getter: (item: T) => number) => TLargest number, or the item with the largest getter() value.
min(array: readonly number[]) => number | null, or <T>(array: readonly T[], getter: (item: T) => number) => TSmallest number, or the item with the smallest getter() value.
sum(array: readonly number[]) => number, or <T>(array: readonly T[], fn: (item: T) => number) => numberSum of the numbers, or of fn(item) per item.
unique<T, K = T>(array: readonly T[], toKey?: (item: T) => K) => T[]De-duplicated array, by value or by a derived key.
cluster<T, Size extends number = 2>(array: readonly T[], size?: Size) => T[][]Chunks array into groups of size (default 2).

Naming Helpers

ExportSignatureDescription
camelCase(str: string) => stringConverts to camelCase.
kebabCase(str: string) => stringConverts to kebab-case.
pascalCase(str: string) => stringConverts to PascalCase.
snakeCase(str: string, options?) => stringConverts to snake_case.
capitalize(str: string) => stringUppercases the first character.
trim(str: string | null | undefined, charsToTrim?: string) => stringTrims whitespace, or the given character set.
template(str: string, data: Record<string, any>, regex?: RegExp) => stringInterpolates {{key}}-style placeholders from data (custom placeholder regex optional).
similarity(str1: string, str2: string) => numberString-similarity score between two strings.

Rate-Limiting and Memoization

ExportSignatureDescription
debounce<TArgs extends any[]>({ delay, leading }, func: (...args: TArgs) => any) => DebounceFunction<TArgs>Debounced wrapper with .cancel() / .flush() / .isPending().
throttle<TArgs extends any[]>({ interval, trailing }, func: (...args: TArgs) => any) => ThrottledFunction<TArgs>Throttled wrapper.
memoize<TArgs, TResult>(func: (...args: TArgs) => TResult, options?: MemoOptions<TArgs>) => (...args: TArgs) => TResultCaches func's result by argument list.
once<Args, Return, This>(fn: (this: This, ...args: Args) => Return) => (this: This, ...args: Args) => ReturnRuns fn at most once; later calls return the first call's result.
noop() => undefinedDoes nothing.

Numeric Conversion

ExportSignatureDescription
toFloat(value: unknown) => number, or <T>(value: unknown, defaultValue: T) => number | TParses a float, or returns defaultValue when unparsable.
toInt(value: unknown) => number, or <T>(value: unknown, defaultValue: T) => number | TParses an integer, or returns defaultValue when unparsable.

Query-String Helpers

ExportSignatureDescription
decodeQueryString(str: string) => object (qs.parse)Parses a query string into a nested object.
encodeQueryString(obj: object) => string (qs.stringify)Serializes an object into a query string.

Constants and Placeholders

ExportSignatureDescription
always<T>(value: T) => () => TBuilds a function that always returns value.
alwaysTrue() => truealways(true).
alwaysFalse() => falsealways(false).