UNPKG

@konkon5991/pipeline-ts

Version:

A lightweight TypeScript library for Railway Oriented Programming (ROP) with comprehensive functional programming utilities.

251 lines 12.3 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * Represents a successful result. * @template T - The type of the value. */ export class Success { /** * Creates an instance of Success. * @param {T} value - The value of the successful result. */ constructor(value) { this.value = value; this.isSuccess = true; this.isFailure = false; } } /** * Represents a failed result. * @template E - The type of the error. */ export class Failure { /** * Creates an instance of Failure. * @param {E} error - The error of the failed result. */ constructor(error) { this.error = error; this.isSuccess = false; this.isFailure = true; } } /** * Checks if the given result is a Success. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {Result<T, E>} result - The result to check. * @returns {result is Success<T>} - True if the result is a Success, otherwise false. */ export const isSuccess = (result) => result.isSuccess; /** * Checks if the given result is a Failure. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {Result<T, E>} result - The result to check. * @returns {result is Failure<E>} - True if the result is a Failure, otherwise false. */ export const isFailure = (result) => result.isFailure; /** * Creates a success result. * @template T - The type of the value. * @param {T} value - The value of the success result. * @returns {Result<T, never>} - The success result. */ export const success = (value) => new Success(value); /** * Creates a failure result. * @template E - The type of the error. * @param {E} error - The error of the failure result. * @returns {Result<never, E>} - The failure result. */ export const failure = (error) => new Failure(error); /** * Wraps a value in a success result. * @template T - The type of the value. * @param {T} value - The value to wrap. * @returns {Result<T, never>} - The success result. */ export const of = (value) => success(value); /** * Matches a result to a function based on whether it is a success or a failure. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @template RS - The return type of the success handler. * @template RF - The return type of the failure handler. * @param {Object} handlers - An object containing the success and failure handlers. * @param {(value: T) => RS | Promise<RS>} handlers.onSuccess - The success handler. * @param {(error: E) => RF | Promise<RF>} handlers.onFailure - The failure handler. * @returns {(result: Result<T, E>) => Promise<RS | RF>} - A function that takes a result and returns a promise that resolves to the result of the appropriate handler. */ export const match = ({ onSuccess, onFailure, }) => (result) => __awaiter(void 0, void 0, void 0, function* () { return isSuccess(result) ? onSuccess(result.value) : onFailure(result.error); }); /** * Converts a promise to a result. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {Promise<T>} promise - The promise to convert. * @returns {Promise<Result<T, E>>} - A promise that resolves to a result. */ export const fromPromise = (promise) => __awaiter(void 0, void 0, void 0, function* () { try { const value = yield promise; return success(value); } catch (error) { return failure(error); } }); /** * Maps a function over a successful result. * @template T - The type of the value in case of success. * @template U - The type of the mapped value. * @template E - The type of the error in case of failure. * @param {(value: T) => U} fn - The mapping function. * @returns {(result: Result<T, E>) => Result<U, E>} - A function that takes a result and returns a mapped result. */ export const map = (fn) => (result) => isSuccess(result) ? success(fn(result.value)) : result; /** * Maps an async function over a successful result. * @template T - The type of the value in case of success. * @template U - The type of the mapped value. * @template E - The type of the error in case of failure. * @param {(value: T) => Promise<U>} fn - The async mapping function. * @returns {(result: Result<T, E>) => Promise<Result<U, E>>} - A function that takes a result and returns a promise of mapped result. */ export const mapAsync = (fn) => (result) => __awaiter(void 0, void 0, void 0, function* () { return isSuccess(result) ? success(yield fn(result.value)) : result; }); /** * FlatMaps a function over a successful result. * @template T - The type of the value in case of success. * @template U - The type of the mapped value. * @template E - The type of the error in case of failure. * @param {(value: T) => Result<U, E>} fn - The flat mapping function. * @returns {(result: Result<T, E>) => Result<U, E>} - A function that takes a result and returns a flat mapped result. */ export const flatMap = (fn) => (result) => isSuccess(result) ? fn(result.value) : result; /** * FlatMaps an async function over a successful result. * @template T - The type of the value in case of success. * @template U - The type of the mapped value. * @template E - The type of the error in case of failure. * @param {(value: T) => Promise<Result<U, E>>} fn - The async flat mapping function. * @returns {(result: Result<T, E>) => Promise<Result<U, E>>} - A function that takes a result and returns a promise of flat mapped result. */ export const flatMapAsync = (fn) => (result) => __awaiter(void 0, void 0, void 0, function* () { return isSuccess(result) ? fn(result.value) : result; }); /** * Folds a result into a single value. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @template U - The type of the folded value. * @param {(error: E) => U} onFailure - The failure handler. * @param {(value: T) => U} onSuccess - The success handler. * @returns {(result: Result<T, E>) => U} - A function that takes a result and returns a folded value. */ export const fold = (onFailure, onSuccess) => (result) => isSuccess(result) ? onSuccess(result.value) : onFailure(result.error); /** * Maps both success and failure cases. * @template T - The type of the value in case of success. * @template U - The type of the mapped success value. * @template E - The type of the error in case of failure. * @template F - The type of the mapped error. * @param {(value: T) => U} onSuccess - The success mapping function. * @param {(error: E) => F} onFailure - The failure mapping function. * @returns {(result: Result<T, E>) => Result<U, F>} - A function that takes a result and returns a bi-mapped result. */ export const bimap = (onSuccess, onFailure) => (result) => isSuccess(result) ? success(onSuccess(result.value)) : failure(onFailure(result.error)); /** * Executes a side effect on a successful result. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {(value: T) => void} fn - The side effect function. * @returns {(result: Result<T, E>) => Result<T, E>} - A function that takes a result and returns the same result. */ export const tap = (fn) => (result) => { if (isSuccess(result)) fn(result.value); return result; }; /** * Executes an async side effect on a successful result. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {(value: T) => Promise<void>} fn - The async side effect function. * @returns {(result: Result<T, E>) => Promise<Result<T, E>>} - A function that takes a result and returns a promise of the same result. */ export const tapAsync = (fn) => (result) => __awaiter(void 0, void 0, void 0, function* () { if (isSuccess(result)) yield fn(result.value); return result; }); /** * Maps a function over a failure result. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @template F - The type of the mapped error. * @param {(error: E) => F} fn - The error mapping function. * @returns {(result: Result<T, E>) => Result<T, F>} - A function that takes a result and returns a mapped result. */ export const mapError = (fn) => (result) => isFailure(result) ? failure(fn(result.error)) : result; /** * Recovers from a failure by converting it to a success. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {(error: E) => T} fn - The recovery function. * @returns {(result: Result<T, E>) => Result<T, never>} - A function that takes a result and returns a recovered result. */ export const recover = (fn) => (result) => isFailure(result) ? success(fn(result.error)) : result; /** * Recovers from a failure with a Result. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @template F - The type of the new error. * @param {(error: E) => Result<T, F>} fn - The recovery function. * @returns {(result: Result<T, E>) => Result<T, F>} - A function that takes a result and returns a recovered result. */ export const recoverWith = (fn) => (result) => isFailure(result) ? fn(result.error) : result; /** * Returns an alternative result if the current one is a failure. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {() => Result<T, E>} fn - Function that returns an alternative result. * @returns {(result: Result<T, E>) => Result<T, E>} - A function that takes a result and returns either the original or alternative result. */ export const orElse = (fn) => (result) => isFailure(result) ? fn() : result; /** * Returns a default value if the result is a failure. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {T} defaultValue - The default value. * @returns {(result: Result<T, E>) => T} - A function that takes a result and returns either the success value or default. */ export const getOrElse = (defaultValue) => (result) => isSuccess(result) ? result.value : defaultValue; /** * Returns a value computed from the error if the result is a failure. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {(error: E) => T} fn - Function to compute value from error. * @returns {(result: Result<T, E>) => T} - A function that takes a result and returns either the success value or computed value. */ export const getOrElseWith = (fn) => (result) => isSuccess(result) ? result.value : fn(result.error); /** * Filters a success value based on a predicate. * @template T - The type of the value in case of success. * @template E - The type of the error in case of failure. * @param {(value: T) => boolean} predicate - The predicate function. * @param {(value: T) => E} onFalse - Function to create error when predicate fails. * @returns {(result: Result<T, E>) => Result<T, E>} - A function that takes a result and returns a filtered result. */ export const filter = (predicate, onFalse) => (result) => isSuccess(result) ? predicate(result.value) ? result : failure(onFalse(result.value)) : result; //# sourceMappingURL=result.js.map