Functional programming patterns for TypeScript.
| Package | Description | Version |
|---|---|---|
@deessejs/core |
Functional programming patterns |
- TypeScript 5.0+
- Node.js 20+
# Install core
npm install @deessejs/core
# Or using pnpm
pnpm add @deessejs/core
# Or using yarn
yarn add @deessejs/coreFor full documentation, visit core.deessejs.com.
import { ok, err, isOk, isErr, Result } from '@deessejs/core'
// Ok - Normal Operation Result
const ok = ok({ id: 1, name: 'John' })
if (isOk(ok)) {
console.log(ok.value.name)
}
// Err - Domain Errors
const notFound = err({
name: 'NOT_FOUND',
message: 'User not found',
data: { id: 123 }
})The Maybe type represents optional values - a value that may or may not be present. It's a safer alternative to null/undefined.
| Type | Description |
|---|---|
Maybe<T> |
Union of Some<T> or None |
Some<T> |
A present value ({ ok: true, value: T }) |
None |
An absent value ({ ok: false }) |
import {
some, none,
fromNullable, isSome, isNone,
map, flatMap, getOrElse, getOrCompute,
tap, match, toNullable, toUndefined
} from '@deessejs/core'
// Creation
const present = some(42) // Some<number>
const absent = none() // None
// From nullable
const value1 = fromNullable(42) // Some<number>
const value2 = fromNullable(null) // None
const value3 = fromNullable(0) // Some<number> (0 is a valid value)
// Type guards
isSome(present) // true
isNone(absent) // true
// Transform
map(present, x => x * 2) // Some<84>
flatMap(present, x => some(x * 2)) // Some<84>
flatMap(absent, x => some(x * 2)) // None
// Extract
getOrElse(present, 0) // 42
getOrElse(absent, 0) // 0
// Side effects
tap(present, console.log) // logs 42, returns Some<42>
match(present, v => v * 2, () => 0) // 84
match(absent, v => v * 2, () => 0) // 0
// Convert back
toNullable(present) // 42
toNullable(absent) // null
toUndefined(present) // 42
toUndefined(absent) // undefined# Install dependencies
pnpm install
# Build all packages
pnpm build
# Run tests
pnpm test
# Run tests with coverage
pnpm test:coverage
# Lint
pnpm lint
# Type check
pnpm typecheckContributions are welcome! Please feel free to submit a Pull Request.
- Nesalia Inc.
If you discover any security vulnerabilities, please send an e-mail to security@nesalia.com.
MIT License - see the LICENSE file for details.