Core utilities
The core is what you get from the bare @xtandard/lib import: small building blocks for error handling, async
code, iterables, resource cleanup and strings. It has no dependencies and uses only standard JavaScript,
so the same code runs in Node, Bun, Deno, browsers and edge runtimes.
import { tryCatch, AsyncChannel, enumerate, defer } from "@xtandard/lib";The sub-entry points (@xtandard/lib/interval, @xtandard/lib/pagination, @xtandard/lib/filters, …) build on these
utilities. Their dependencies are optional peers, so importing the core never pulls them in.
What’s in it
Section titled “What’s in it”| Category | Exports | Page |
|---|---|---|
| Error handling | tryCatch, tryCatchSync (deprecated), assertNotNull, valueOrThrow, AssertError |
Error handling |
| Async | wait, Completer, collect, asyncOf, AsyncChannel, resolveMaybePromise, chainMaybePromise, MaybePromise |
Async |
| Iterables & generators | range, rangeLazy, enumerate, enumerateAsync, toIterator, toAsyncIterable |
Iterables & generators |
| Type guards | isPromise, isIterable, isAsyncIterable, isIterator, isGenerator, isAsyncGenerator, isDisposable, isAsyncDisposable, isSyncDisposable |
Type guards |
| Disposables | defer, deferSync, makeDisposable, DisposedSymbol, EnabledSymbol, CanceledSymbol, CancelReasonSymbol |
Disposables |
| Strings | ensureString, naturalSortCompare, jaroWinklerSimilarity |
Strings |
| Misc | cast, log, secondsToMs, minutesToMs, hoursToMs, prepareLoaderResult, resolveStreamSource, StreamSource |
Misc |
The full signatures are in the core API reference.
Zero dependencies, isomorphic
Section titled “Zero dependencies, isomorphic”The core imports nothing outside the library. It relies only on language features: Promises, iterators,
async generators, Symbol.dispose / Symbol.asyncDispose and Intl string comparison. It doesn’t touch the
DOM, node:* modules or process, so it is safe to import in shared frontend and backend code. Helpers that
need a third-party library (DataLoader, React, Temporal, …) live in their own entry points. See
Entry points.
Sync/async-adaptive APIs
Section titled “Sync/async-adaptive APIs”Several core functions are as synchronous as their input. Give them sync input and you get a plain value
back, with no await and no microtask. Give them async input and you get a Promise. The return type reflects
which one you get, so you always know whether to await.
import { tryCatch, collect, enumerate } from "@xtandard/lib";
const [config, error] = tryCatch(() => JSON.parse(raw)); // sync function → tuple, no awaitconst [user, fetchError] = await tryCatch(fetch("/api/me")); // promise → Promise<tuple>
collect(new Set([1, 2])); // number[]await collect(asyncRows()); // Promise<Row[]>
for (const [i, ch] of enumerate("abc")) {} // Generatorfor await (const [i, row] of enumerate(asyncRows())) {} // AsyncGeneratorawait still works on the sync results (awaiting a plain value is a no-op), so code that doesn’t care can
always await. The building blocks, MaybePromise, isPromise, chainMaybePromise and
resolveMaybePromise, are exported too, so you can write your own adaptive utilities. The
Sync/async-adaptive APIs guide explains the rules and gives recipes.