import { InvalidStateError } from "./errors"; import { bind, combine, identity, isWrappedFunction } from "./runtime"; import type { AnyParameters, AsyncMonad, AsyncTap, Container, Mapper, MaybePromiseLike, Pipe, Zip } from "./types"; const name = "Identity"; class Identity implements AsyncMonad, Container, AsyncTap, Pipe, Zip { static from(value: T): Identity { return new Identity(value); } get [Symbol.toStringTag](): typeof name { return name; } get name(): typeof name { return name; } private constructor(public readonly value: T) {} //#region Functor map( map: Mapper, ...parameters: P ): Identity { return this.fold(combine(bind(map, parameters), from)); } async asyncMap( map: Mapper, P>, ...parameters: P ): Promise> { return await this.map(map, ...parameters).await(); } //#endregion //#region Applicative pure(value: T): Identity { return new Identity(value); } apply( this: Identity>, argument: Identity, ...parameters: P ): Identity; apply( this: Identity, map: Identity>, ...parameters: P ): Identity; apply( this: Identity>, argument: Identity>, ...parameters: P ): Identity { return this.zip(argument).map(([current, argument]): B => { if (isWrappedFunction(current)) { return current(argument as A, ...parameters); } if (isWrappedFunction(argument)) { return argument(current as A, ...parameters); } throw new InvalidStateError( InvalidStateError.Messages.APPLY_SHOULD_BE_FUNCTION ); }); } asyncApply( this: Identity, P>>, argument: Identity, ...parameters: P ): Promise>; asyncApply( this: Identity, map: Identity, P>>, ...parameters: P ): Promise>; async asyncApply( this: Identity, P>>, argument: Identity, P>>, ...parameters: P ): Promise> { return await this.zip(argument) .map(([current, argument]): B => { if (isWrappedFunction(current)) { return current(argument as A, ...parameters); } if (isWrappedFunction(argument)) { return argument(current as A, ...parameters); } throw new InvalidStateError( InvalidStateError.Messages.APPLY_SHOULD_BE_FUNCTION ); }) .await(); } //#endregion //#region Monad chain( map: Mapper, P>, ...parameters: P ): Identity { return this.fold(bind(map, parameters)); } async asyncChain( map: Mapper>, P>, ...parameters: P ): Promise> { const result = await this.asyncMap, P>(map, ...parameters); result.join(); return result.join(); } async await(this: Identity>): Promise> { return await this.fold>>(async (value) => from(await value) ); } join(this: Identity>): Identity { return this.chain(identity); } //#endregion //#region Alternative //#endregion //#region Tap tap

( callback: Mapper, ...parameters: P ): Identity { this.map(callback, ...parameters); return this; } async asyncTap

( callback: Mapper, P>, ...parameters: P ): Promise> { await this.asyncMap(callback, ...parameters); return this; } //#endregion // Pipe pipe( pipe: Mapper, A, P>, ...parameters: P ): A { return bind(pipe, parameters)(this); } //#region Container unwrapOrElse(fallback: () => X): X | T { return this.fold((value) => value || fallback()); } unwrapOr(value: X): X | T { return this.unwrapOrElse(() => value); } unwrap(): T { return this.value; } //#endregion // Zip zip(either: Identity): Identity<[T, A]> { return this.chain((value) => either.map((right) => [value, right])); } // Identity Specific fold(mapper: Mapper): A { return mapper(this.value); } getValue(): T { return this.value; } } Object.freeze(Identity); Object.freeze(Identity.prototype); export { type Identity }; export const from = Identity.from; export const isIdentity = ( value: unknown | Identity ): value is Identity => value instanceof Identity; export function chain( map: (value: A, ...parameters: P) => MaybePromiseLike>, ...parameters: P ): (either: Identity) => Promise> { return (either) => either.asyncChain(bind(map, parameters)); } export function merge(values: [Identity]): Identity<[V1]>; export function merge( values: [Identity, Identity] ): Identity<[V1, V2]>; export function merge( values: [Identity, Identity, Identity] ): Identity<[V1, V2, V3]>; export function merge( values: [Identity, Identity, Identity, Identity] ): Identity<[V1, V2, V3, V4]>; export function merge( values: [Identity, Identity, Identity, Identity, Identity] ): Identity<[V1, V2, V3, V4, V5]>; export function merge( values: [ Identity, Identity, Identity, Identity, Identity, Identity ] ): Identity<[V1, V2, V3, V4, V5, V6]>; export function merge(values: Identity[]): Identity; export function merge(values: Identity[]): Identity { return from(values.map((value) => value.unwrap())); }