2.0.0 • Published 11 months ago
simple-functional-style-helper v2.0.0
simple-functional-style-helper
Zero-dependency functional programming toolkit for building robust JavaScript applications.
Features
Maybe Monad
Handle null-safe operations elegantly:
Maybe.just(value): Wrap a valueMaybe.nothing(): Represent absence of valuemap/flatMap: Transform values safely- Pattern matching with
justandnothingcases
Either Monad
Elegant error handling with left/right cases:
Either.right(value): Success caseEither.left(error): Error casemap/flatMap: Transform success casesmapLeft: Transform error cases- Built-in try/catch wrapper
Result Monad
Predictable error management:
Result.success(value): Success stateResult.failure(error): Failure state- Error propagation with
map/flatMap - Safe error handling with
mapError - Automatic try/catch wrapping
SafeWrapper
Secure object property access:
- Deep nested property access
- Null-safe operations
- Path existence checking
- Schema validation
- Pattern matching
ModuleLoader
Dynamic module loading with parameter passing:
- Load all modules from a directory
- Pass parameters to module initializers
- Safe module loading with error handling
- Clean functional interface
Installation
npm install simple-functional-style-helperUsage
import { Maybe, Either, Result, SafeWrapper } from 'simple-functional-style-helper'
// Maybe example
const maybeValue = Maybe.from(someNullableValue)
.map(x => x * 2)
.getOrElse(0)
// Either example
const result = Either.try(() => riskyOperation())
.map(value => process(value))
.getOrElse(defaultValue)
// Result example
const computation = Result.try(() => compute())
.map(value => transform(value))
.mapError(err => handleError(err))
// SafeWrapper example
const wrapper = SafeWrapper.of(complexObject)
const value = wrapper.get('deeply.nested.property')
.map(val => transform(val))
.getOrElse(defaultValue)API Documentation
Maybe
just(value): Create a Maybe with a valuenothing(): Create an empty Maybefrom(value): Create Maybe from nullable valuemap(fn): Transform value if presentflatMap(fn): Chain transformationsgetOrElse(default): Get value or defaultmatch({ just, nothing }): Pattern matching
Either
right(value): Create right (success) caseleft(error): Create left (error) casetry(fn): Wrap function in try/catchmap(fn): Transform right valuemapLeft(fn): Transform left valueflatMap(fn): Chain operationsfold(leftFn, rightFn): Handle both cases
Result
success(value): Create success statefailure(error): Create failure statetry(fn): Auto try/catch wrappermap(fn): Transform success valuemapError(fn): Transform errorflatMap(fn): Chain operationsgetOrThrow(): Get value or throw
SafeWrapper
of(object): Create wrapperget(path): Safe property accesshas(path): Check path existencehasAll(paths): Check multiple pathshasAny(paths): Check any path existsvalidate(schema): Validate object structure
License
GPL-3.0