/** * @since 1.0.0 */ import type * as Either from "@effect/data/Either" import * as Equal from "@effect/data/Equal" import { dual } from "@effect/data/Function" import * as Hash from "@effect/data/Hash" import { NodeInspectSymbol, toJSON, toString } from "@effect/data/Inspectable" import { EffectTypeId, effectVariance } from "@effect/data/internal/Effect" import * as option from "@effect/data/internal/Option" import type { Option } from "@effect/data/Option" import { pipeArguments } from "@effect/data/Pipeable" /** * @internal */ export const TypeId: Either.TypeId = Symbol.for("@effect/data/Either") as Either.TypeId const CommonProto = { [EffectTypeId]: effectVariance, [TypeId]: { _A: (_: never) => _ }, [NodeInspectSymbol](this: Either.Either) { return this.toJSON() }, pipe() { return pipeArguments(this, arguments) }, toString(this: Either.Left) { return toString(this.toJSON()) } } const RightProto = Object.assign(Object.create(CommonProto), { _tag: "Right", [Equal.symbol](this: Either.Right, that: unknown): boolean { return isEither(that) && isRight(that) && Equal.equals(that.right, this.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", [Equal.symbol](this: Either.Left, that: unknown): boolean { return isEither(that) && isLeft(that) && Equal.equals(that.left, this.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 => typeof input === "object" && input != null && TypeId in input /** @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: E): Either.Either => { const a = Object.create(LeftProto) a.left = left return a } /** @internal */ export const right = (right: A): 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 = dual( 2, (self: Option, onNone: () => E): Either.Either => option.isNone(self) ? left(onNone()) : right(self.value) )