/** * @since 2.0.0 */ import type * as Either from "../Either.js" import * as Equal from "../Equal.js" import { dual } from "../Function.js" import * as Hash from "../Hash.js" import { format, NodeInspectSymbol, toJSON } from "../Inspectable.js" import type { Option } from "../Option.js" import { hasProperty } from "../Predicate.js" import { EffectPrototype } from "./effectable.js" import * as option from "./option.js" /** * @internal */ export const TypeId: Either.TypeId = Symbol.for("effect/Either") as Either.TypeId const CommonProto = { ...EffectPrototype, [TypeId]: { _R: (_: never) => _ }, [NodeInspectSymbol](this: Either.Either) { return this.toJSON() }, toString(this: Either.Left) { return format(this.toJSON()) } } const RightProto = Object.assign(Object.create(CommonProto), { _tag: "Right", _op: "Right", [Equal.symbol](this: Either.Right, that: unknown): boolean { return isEither(that) && isRight(that) && Equal.equals(this.right, that.right) }, [Hash.symbol](this: Either.Right) { return Hash.combine(Hash.hash(this._tag))(Hash.hash(this.right)) }, toJSON(this: Either.Right) { return { _id: "Either", _tag: this._tag, right: toJSON(this.right) } } }) const LeftProto = Object.assign(Object.create(CommonProto), { _tag: "Left", _op: "Left", [Equal.symbol](this: Either.Left, that: unknown): boolean { return isEither(that) && isLeft(that) && Equal.equals(this.left, that.left) }, [Hash.symbol](this: Either.Left) { return Hash.combine(Hash.hash(this._tag))(Hash.hash(this.left)) }, toJSON(this: Either.Left) { return { _id: "Either", _tag: this._tag, left: toJSON(this.left) } } }) /** @internal */ export const isEither = (input: unknown): input is Either.Either => hasProperty(input, TypeId) /** @internal */ export const isLeft = (ma: Either.Either): ma is Either.Left => ma._tag === "Left" /** @internal */ export const isRight = (ma: Either.Either): ma is Either.Right => ma._tag === "Right" /** @internal */ export const left = (left: L): Either.Either => { const a = Object.create(LeftProto) a.left = left return a } /** @internal */ export const right = (right: R): Either.Either => { const a = Object.create(RightProto) a.right = right return a } /** @internal */ export const getLeft = ( self: Either.Either ): Option => (isRight(self) ? option.none : option.some(self.left)) /** @internal */ export const getRight = ( self: Either.Either ): Option => (isLeft(self) ? option.none : option.some(self.right)) /** @internal */ export const fromOption: { (onNone: () => L): (self: Option) => Either.Either (self: Option, onNone: () => L): Either.Either } = dual( 2, (self: Option, onNone: () => L): Either.Either => option.isNone(self) ? left(onNone()) : right(self.value) )