/** * @since 1.0.0 */ import type * as E from "effect/Either" import type * as O from "effect/Option" import type { Pipeable } from "effect/Pipeable" import * as Predicate from "effect/Predicate" import type { Unify } from "effect/Unify" import * as internal from "./internal/matcher" import type { SafeRefinement } from "./SafeRefinement" import type * as Types from "./Types" /** * @category type ids * @since 1.0.0 */ export const MatcherTypeId: unique symbol = internal.TypeId /** * @category type ids * @since 1.0.0 */ export type MatcherTypeId = typeof MatcherTypeId /** * @category model * @since 1.0.0 */ export type Matcher = | TypeMatcher | ValueMatcher /** * @category model * @since 1.0.0 */ export interface TypeMatcher extends Pipeable { readonly _tag: "TypeMatcher" readonly [MatcherTypeId]: { readonly _input: (_: Input) => unknown readonly _filters: (_: never) => Filters readonly _remaining: (_: never) => Remaining readonly _result: (_: never) => Result } readonly cases: ReadonlyArray readonly add: (_case: Case) => TypeMatcher } /** * @category model * @since 1.0.0 */ export interface ValueMatcher extends Pipeable { readonly _tag: "ValueMatcher" readonly [MatcherTypeId]: { readonly _input: (_: Input) => unknown readonly _filters: (_: never) => Filters readonly _result: (_: never) => Result } readonly provided: Provided readonly value: E.Either readonly add: (_case: Case) => ValueMatcher } /** * @category model * @since 1.0.0 */ export type Case = When | Not /** * @category model * @since 1.0.0 */ export interface When { readonly _tag: "When" readonly guard: (u: unknown) => boolean readonly evaluate: (input: unknown) => any } /** * @category model * @since 1.0.0 */ export interface Not { readonly _tag: "Not" readonly guard: (u: unknown) => boolean readonly evaluate: (input: unknown) => any } /** * @category constructors * @since 1.0.0 */ export const type: () => Matcher, I, never, never> = internal.type /** * @category constructors * @since 1.0.0 */ export const value: ( i: I, ) => Matcher, I, never, I> = internal.value /** * @category constructors * @since 1.0.0 */ export const valueTags: < const I, P extends { readonly [Tag in Types.Tags<"_tag", I> & string]: ( _: Extract, ) => any }, >(fields: P) => (input: I) => Unify> = internal.valueTags /** * @category constructors * @since 1.0.0 */ export const typeTags: () => < P extends { readonly [Tag in Types.Tags<"_tag", I> & string]: ( _: Extract, ) => any }, >(fields: P) => (input: I) => Unify> = internal.typeTags /** * @category combinators * @since 1.0.0 */ export const when: < R, const P extends Types.PatternPrimitive | Types.PatternBase, Fn extends (_: Types.WhenMatch) => unknown, >( pattern: P, f: Fn, ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>, Types.ApplyFilters>>, A | ReturnType, Pr > = internal.when as any /** * @category combinators * @since 1.0.0 */ export const whenOr: < R, const P extends ReadonlyArray< Types.PatternPrimitive | Types.PatternBase >, Fn extends (_: Types.WhenMatch) => unknown, >( ...args: [...patterns: P, f: Fn] ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>, Types.ApplyFilters>>, A | ReturnType, Pr > = internal.whenOr as any /** * @category combinators * @since 1.0.0 */ export const whenAnd: < R, const P extends ReadonlyArray< Types.PatternPrimitive | Types.PatternBase >, Fn extends (_: Types.WhenMatch>) => unknown, >( ...args: [...patterns: P, f: Fn] ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters< I, Types.AddWithout>> >, A | ReturnType, Pr > = internal.whenAnd as any /** * @category combinators * @since 1.0.0 */ export const discriminator: ( field: D, ) => & string, B>( ...pattern: [ first: P, ...values: Array

, f: (_: Extract>) => B, ] ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters>>>, B | A, Pr > = internal.discriminator /** * @category combinators * @since 1.0.0 */ export const discriminatorStartsWith: ( field: D, ) => ( pattern: P, f: (_: Extract>) => B, ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters< I, Types.AddWithout>> >, B | A, Pr > = internal.discriminatorStartsWith as any /** * @category combinators * @since 1.0.0 */ export const discriminators: ( field: D, ) => < R, P extends { readonly [Tag in Types.Tags & string]?: | ((_: Extract>) => any) | undefined }, >( fields: P, ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters>>>, A | ReturnType, Pr > = internal.discriminators /** * @category combinators * @since 1.0.0 */ export const discriminatorsExhaustive: ( field: D, ) => < R, P extends { readonly [Tag in Types.Tags & string]: ( _: Extract>, ) => any }, >( fields: P, ) => ( self: Matcher, ) => [Pr] extends [never] ? (u: I) => Unify> : Unify> = internal.discriminatorsExhaustive /** * @category combinators * @since 1.0.0 */ export const tag: & string, B>( ...pattern: [ first: P, ...values: Array

, f: (_: Extract>) => B, ] ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters>>>, B | A, Pr > = internal.tag /** * @category combinators * @since 1.0.0 */ export const tagStartsWith: ( pattern: P, f: (_: Extract>) => B, ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters< I, Types.AddWithout>> >, B | A, Pr > = internal.tagStartsWith as any /** * @category combinators * @since 1.0.0 */ export const tags: < R, P extends { readonly [Tag in Types.Tags<"_tag", R> & string]?: | ((_: Extract>) => any) | undefined }, >( fields: P, ) => ( self: Matcher, ) => Matcher< I, Types.AddWithout>>, Types.ApplyFilters< I, Types.AddWithout>> >, A | ReturnType, Pr > = internal.tags /** * @category combinators * @since 1.0.0 */ export const tagsExhaustive: < R, P extends { readonly [Tag in Types.Tags<"_tag", R> & string]: ( _: Extract>, ) => any }, >( fields: P, ) => ( self: Matcher, ) => [Pr] extends [never] ? (u: I) => Unify> : Unify> = internal.tagsExhaustive /** * @category combinators * @since 1.0.0 */ export const not: < R, const P extends Types.PatternPrimitive | Types.PatternBase, Fn extends (_: Types.NotMatch) => unknown, >( pattern: P, f: Fn, ) => ( self: Matcher, ) => Matcher< I, Types.AddOnly>, Types.ApplyFilters>>, A | ReturnType, Pr > = internal.not as any /** * @category predicates * @since 1.0.0 */ export const nonEmptyString: SafeRefinement = internal.nonEmptyString /** * @category predicates * @since 1.0.0 */ export const is: < Literals extends ReadonlyArray, >(...literals: Literals) => Predicate.Refinement = internal.is /** * @category predicates * @since 1.0.0 */ export const string: Predicate.Refinement = Predicate.isString /** * @category predicates * @since 1.0.0 */ export const number: Predicate.Refinement = Predicate.isNumber /** * @category predicates * @since 1.0.0 */ export const any: SafeRefinement = internal.any /** * @category predicates * @since 1.0.0 */ export const defined: (u: A) => u is A & {} = internal.defined /** * @category predicates * @since 1.0.0 */ export const boolean: Predicate.Refinement = Predicate.isBoolean const _undefined: Predicate.Refinement = Predicate.isUndefined export { /** * @category predicates * @since 1.0.0 */ _undefined as undefined, } const _null: Predicate.Refinement = Predicate.isNull export { /** * @category predicates * @since 1.0.0 */ _null as null, } /** * @category predicates * @since 1.0.0 */ export const bigint: Predicate.Refinement = Predicate.isBigint /** * @category predicates * @since 1.0.0 */ export const date: Predicate.Refinement = Predicate.isDate /** * @category predicates * @since 1.0.0 */ export const record: Predicate.Refinement< unknown, { [k: string]: any [k: symbol]: any } > = Predicate.isRecord /** * @category predicates * @since 1.0.0 */ export const instanceOf: any>( constructor: A, ) => SafeRefinement, never> = internal.instanceOf /** * @category predicates * @since 1.0.0 */ export const instanceOfUnsafe: any>( constructor: A, ) => SafeRefinement, InstanceType> = internal.instanceOf /** * @category conversions * @since 1.0.0 */ export const orElse: ( f: (b: RA) => B, ) => ( self: Matcher, ) => [Pr] extends [never] ? (input: I) => Unify : Unify = internal.orElse /** * @category conversions * @since 1.0.0 */ export const orElseAbsurd: ( self: Matcher, ) => [Pr] extends [never] ? (input: I) => Unify : Unify = internal.orElseAbsurd /** * @category conversions * @since 1.0.0 */ export const either: ( self: Matcher, ) => [Pr] extends [never] ? (input: I) => E.Either> : E.Either> = internal.either /** * @category conversions * @since 1.0.0 */ export const option: ( self: Matcher, ) => [Pr] extends [never] ? (input: I) => O.Option> : O.Option> = internal.option /** * @category conversions * @since 1.0.0 */ export const exhaustive: ( self: Matcher, ) => [Pr] extends [never] ? (u: I) => Unify : Unify = internal.exhaustive