77 lines
1.7 KiB
Markdown
77 lines
1.7 KiB
Markdown
# native-monad
|
|
|
|
TypeScript monad library using native JS/TS idioms - Result, Option, and Query types.
|
|
|
|
## Install
|
|
|
|
```bash
|
|
npm install native-monad
|
|
```
|
|
|
|
## Types
|
|
|
|
### Result\<T, E\>
|
|
|
|
Success/failure type. Variants: `Ok<T>` | `Err<E>`.
|
|
|
|
```typescript
|
|
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`.
|
|
|
|
```typescript
|
|
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>`.
|
|
|
|
```typescript
|
|
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
|
|
|
|
```bash
|
|
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
|
|
```
|