@robonen/stdlib

A platform-independent standard library of tools, utilities, and helpers for TypeScript — arrays, async, math, data structures, and patterns, all tree-shakeable and fully typed.

Every project ends up reimplementing the same handful of helpers: dedupe an array, clamp a number, group records by a key, retry a flaky request. @robonen/stdlib collects those building blocks into one cohesive, dependency-free package. It runs the same in Node, the browser, and the edge — there are no platform globals, no polyfills, and no runtime dependencies. Import only what you use; the rest is shaken out of your bundle.

Fully typed

Generic signatures preserve element and key types end to end, so inference keeps working through groupBy, partition, zip, and friends.

Zero dependencies

No transitive dependencies and no platform assumptions. The same code runs in Node, the browser, Deno, Bun, and edge runtimes.

Tree-shakeable

Each utility is a standalone export with no shared side effects. Import a single function and ship only that function.

Batteries included

Beyond array and math helpers you get data structures (Deque, BinaryHeap, LinkedList) and patterns (StateMachine, PubSub, Command).

Install

sh
pnpm add @robonen/stdlib

Quick start

Import named utilities directly from the package root. Each one is small, predictable, and focused on a single job.

ts
import { groupBy, unique, clamp, tryIt } from '@robonen/stdlib';

// Arrays — group records by a derived key
const byType = groupBy(
  [{ type: 'a', v: 1 }, { type: 'b', v: 2 }, { type: 'a', v: 3 }],
  item => item.type,
);
// => { a: [{ type: 'a', v: 1 }, { type: 'a', v: 3 }], b: [{ type: 'b', v: 2 }] }

// Arrays — dedupe in a single pass
unique([1, 1, 2, 3, 3]); // => [1, 2, 3]

// Math — keep a value inside a range
clamp(value, 0, 100);

// Async — error handling without forking control flow
const { error, data } = await tryIt(fetch)('/api/todos/1');
if (error) {
  // handle the failure
} else {
  // use data
}

What's inside

The library is organized into focused modules. A few of the most-used building blocks:

  • ArraysgroupBy, partition, unique, zip, cluster, range.
  • AsynctryIt, retry, pool, sleep for control flow that doesn't fight you.
  • Mathclamp, lerp, remap, each with a BigInt variant.
  • Functionsdebounce, throttle, memoize, once, compose, pipe.
  • Structs & patternsDeque, PriorityQueue, StateMachine, PubSub and more.

Where to next

Browse the full API reference below, or jump straight to a popular utility:

arrays · 14

async · 4

bits · 9

collections · 2

comparators · 1

functions · 6

math · 10

objects · 2

patterns · 7

structs · 8

sync · 1

text · 4

types · 33

T
AnyFunction

Any function

T
Arrayable

A type that can be either a single value or an array of values

T
Collection

A collection definition

T
HasSpaces

Check if `S` has any spaces

V
isArray

Check if a value is an array

V
isBigInt

Check if a value is a bigint

V
isBoolean

Check if a value is a boolean

V
isDate

Check if a value is a date

V
isError

Check if a value is an error

V
isFunction

Check if a value is a function

V
isMap

Check if a value is a map

V
isNull

Check if a value is a null

V
isNumber

Check if a value is a number

V
isObject

Check if a value is an object

V
isPromise

Check if a value is a promise

V
isRegExp

Check if a value is a regexp

V
isSet

Check if a value is a set

V
isString

Check if a value is a string

V
isSymbol

Check if a value is a symbol

V
isUndefined

Check if a value is a undefined

V
isWeakMap

Check if a value is a weakmap

V
isWeakSet

Check if a value is a weakset

T
MaybePromise

Represents a value that may be a promise.

T
Path

Parse a collection path string into an array of keys

T
PathToPartialType

Like {@link PathToType}, but every object key is optional and objects stay open (accept extra keys). Useful when the produced type only describes the keys a consumer *may* provide rather than the full shape of the source data.

T
PathToType

Convert a collection path array into a Target type

T
ReadonlyArrayable

A type that can be either a single value or a readonly array of values

I
Stringable

Stringable type

V
toString

To string any value

T
Trim

Trim leading and trailing whitespace from `S`

T
UnionToIntersection

Convert a union type to an intersection type

T
VoidFunction

Void function

T
Whitespace

Whitespace characters recognized by {@link Trim}

utils · 2