/** * @since 2.0.0 */ import * as Equal from "../Equal.js" import * as Hash from "../Hash.js" import { format, NodeInspectSymbol, toJSON } from "../Inspectable.js" import type * as Option from "../Option.js" import { hasProperty } from "../Predicate.js" import { EffectPrototype } from "./effectable.js" const TypeId: Option.TypeId = Symbol.for("effect/Option") as Option.TypeId const CommonProto = { ...EffectPrototype, [TypeId]: { _A: (_: never) => _ }, [NodeInspectSymbol](this: Option.Option) { return this.toJSON() }, toString(this: Option.Option) { return format(this.toJSON()) } } const SomeProto = Object.assign(Object.create(CommonProto), { _tag: "Some", _op: "Some", [Equal.symbol](this: Option.Some, that: unknown): boolean { return isOption(that) && isSome(that) && Equal.equals(this.value, that.value) }, [Hash.symbol](this: Option.Some) { return Hash.cached(this, Hash.combine(Hash.hash(this._tag))(Hash.hash(this.value))) }, toJSON(this: Option.Some) { return { _id: "Option", _tag: this._tag, value: toJSON(this.value) } } }) const NoneHash = Hash.hash("None") const NoneProto = Object.assign(Object.create(CommonProto), { _tag: "None", _op: "None", [Equal.symbol](this: Option.None, that: unknown): boolean { return isOption(that) && isNone(that) }, [Hash.symbol](this: Option.None) { return NoneHash }, toJSON(this: Option.None) { return { _id: "Option", _tag: this._tag } } }) /** @internal */ export const isOption = (input: unknown): input is Option.Option => hasProperty(input, TypeId) /** @internal */ export const isNone = (fa: Option.Option): fa is Option.None => fa._tag === "None" /** @internal */ export const isSome = (fa: Option.Option): fa is Option.Some => fa._tag === "Some" /** @internal */ export const none: Option.Option = Object.create(NoneProto) /** @internal */ export const some = (value: A): Option.Option => { const a = Object.create(SomeProto) a.value = value return a }