20bce963ac68ca3e87941f34a0edcabb978de383
native-monad
TypeScript monad library using native JS/TS idioms - Result, Option, and Query types.
Install
npm install native-monad
Types
Result<T, E>
Success/failure type. Variants: Ok<T> | Err<E>.
import { ok, err, Result } from 'native-monad';
const success = ok(42); // Ok<number>
const failure = err('oops'); // Err<string>
success.map(x => x * 2); // Ok(84)
failure.map(x => x * 2); // Err('oops')
success.match(
value => `got ${value}`,
error => `failed: ${error}`,
); // 'got 42'
// Collection operations
Result.all([ok(1), ok(2), ok(3)]); // Ok([1, 2, 3])
Result.all([ok(1), err('x')]); // Err('x')
Option<T>
Presence/absence type. Variants: Some<T> | None.
import { some, none, Option } from 'native-monad';
const present = some('hello'); // Some<string>
const absent = none(); // None
present.map(s => s.toUpperCase()); // Some('HELLO')
absent.map(s => s.toUpperCase()); // None
// Create from nullable values (only null/undefined are falsy)
Option.from(0); // Some(0)
Option.from(null); // None
Option.from(undefined); // None
Query<T, E>
Three-state monad combining Result and Option. Variants: OkSome<T> | OkNone | ErrNone<E>.
import { okSome, okNone, errNone } from 'native-monad';
const found = okSome(42); // success with value
const notFound = okNone(); // success without value
const failed = errNone('error'); // failure
Development
pnpm install
pnpm build # Build (dual ESM/CJS)
pnpm test # Run Jest tests
pnpm test:types # Run vitest type tests
pnpm lint # ESLint
pnpm typecheck # tsc --noEmit
Description
Languages
TypeScript
99.9%