{"version":3,"sources":["jsdelivr-header.js","/npm/@elmish-ts/tagged-union@2.0.2/src/internal.ts","/npm/@elmish-ts/tagged-union@2.0.2/src/tagged-union.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,+LCHO,IAAM,EAAqB,0BCqOlC,SAAgB,EACd,EACA,GAEA,GAwBoB,iBAFpB,EAtBiD,IAwBU,mBAAnB,EAAa,EAxBI,CACvD,IAAM,EAAqB,EAAK,GAC1B,EAAyB,EAAK,GAC9B,EAAU,EAAM,GAChB,EAAqC,EAAS,EAEpD,OAAI,EAAa,WAAA,GAAuC,GAAO,OAAK,IAC3D,EAAO,WAAA,EAAI,GAEX,G,CAYb,IACE,EAVM,EAAqB,EAAK,GAC1B,EAAyB,EAAK,GAGpC,OAFiB,EAAM,GAET,WAAA,EAAI,EACpB,CAUA,SAAS,EAAqC,G,IAAY,IAAA,EAAA,GAAA,EAAA,EAAA,EAAA,UAAA,OAAA,IAAA,EAAA,EAAA,GAAA,UAAA,GACxD,GAAiB,mBAAN,EAST,OAAO,EARP,IAEE,OADA,EAAC,WAAA,EAAI,IACE,C,CACP,MAAO,GAEP,OAAO,C,CAKb,C,eA9IE,G,UAAmC,EAAA,GAAA,EAAA,EAAA,EAAA,UAAA,OAAA,IAAA,EAAA,EAAA,GAAA,UAAA,GACnC,OAAA,EAAA,IACG,GAAM,EACP,EAAC,GAAM,E,CAEX,E,SAiDA,SACE,GAEA,OAAO,SAAA,GAAQ,OAAA,EAAS,EAAM,EAAM,CACtC,E","file":"/npm/@elmish-ts/tagged-union@2.0.2/dist/tagged-union.umd.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@elmish-ts/tagged-union@2.0.2/dist/tagged-union.umd.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { DeepReadonly } from 'typelevel-ts'\n\n/** @ignore */\nexport const TAG: unique symbol = '@elmish-ts/tagged-union' as any\n\n/** @ignore */\ntype _ = unknown\n\n/** @ignore */\nexport type TupleOnly<T extends unknown[]> = any[] extends T ? never : T\n\n/** @ignore */\nexport type AnyTuple =\n  | []\n  | [_]\n  | [_, _]\n  | [_, _, _]\n  | [_, _, _, _]\n  | [_, _, _, _, _]\n  | [_, _, _, _, _, _]\n  | [_, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n  | [_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _]\n","import { TaggedUnionMember, Exact } from 'typelevel-ts'\nimport { TAG, AnyTuple } from './internal'\n\n/**\n * Utility for defining variants of tagged sum types\n *\n * @param Tag a string literal type used to name/tag the variant being defined\n * @param Values (optional) a tuple of type parameters. Defaults to [] (unit)\n *\n * @definition\n * ```ts\n * export type Def<Tag extends string, Values extends AnyTuple = []>> =\n *   { readonly [TAG]: Tag } & { readonly [K in Tag]: Values }\n * ```\n *\n * @example\n * ```ts\n * type Maybe<A> =\n *   | Def<'Nothing'>\n *   | Def<'Just', [A]>\n * ```\n *\n * is literally equivalent to\n *\n * ```ts\n * type Maybe<A> =\n *   | { tag: 'Nothing', Nothing: [] }\n *   | { tag: 'Just', Just: [A] }\n *\n * ```\n */\nexport type Def<Tag extends string, Values extends AnyTuple = []> = Tagged<Tag> & Of<Tag, Values>\n\ntype Tagged<T extends string> = { readonly [TAG]: T }\ntype Of<T extends string, V extends AnyTuple> = {\n  readonly [K in T]: V\n}\n/**\n * A struct of tag-handler pairs, where the handler function\n * receives whatever values (if any) is associated with the\n * given tag.\n *\n */\nexport type CaseOfStruct<D extends Def<string, AnyTuple>, R> =\n  | ExhaustiveCaseOfStruct<D, R>\n  | PartialCaseOfStruct<D, R>\n\ntype ExhaustiveCaseOfStruct<U extends Def<string, AnyTuple>, R> = {\n  readonly [K in U[typeof TAG]]: (...val: Extract<U, { [TAG]: K }>[K]) => R\n}\n\ntype PartialCaseOfStruct<D extends Def<string, AnyTuple>, R> = FallbackMatch<R> &\n  Partial<ExhaustiveCaseOfStruct<D, R>>\n\ntype FallbackMatch<R> = { readonly _: () => R }\n\n/**\n * A struct of tag-handler pairs, where the handler function\n * receives whatever values (if any) is associated with the\n * given tag. Enforces that all cases are covered and that\n * no excess properties are present.\n *\n */\nexport type StrictCaseOfStruct<\n  D extends Def<string, any>,\n  C extends CaseOfStruct<any, any>\n> = C extends ExhaustiveCaseOfStruct<D, infer R> & FallbackMatch<infer R>\n  ? ExhaustiveCaseOfStruct<D, R>\n  : C extends ExhaustiveCaseOfStruct<D, infer R>\n  ? Exact<ExhaustiveCaseOfStruct<D, R>, C>\n  : C\n\n/**\n * Infers whatever type is returned from all case expressions\n */\nexport type CaseOfReturn<\n  D extends Def<string, AnyTuple>,\n  M extends CaseOfStruct<D, unknown>\n> = M extends CaseOfStruct<D, infer R> ? R : unknown\n\n/**\n * Constructs a tagged variant.\n * Used to define nullary* data constructors for tagged sum variants.\n *\n * * _\"nullary\" meaning \"arity zero\", or not needing any arguments_\n *\n * @example\n * ```ts\n * type Maybe<A> =\n *   | Def<'Nothing'>\n *   | Def<'Just', [A]>\n *\n * const Nothing: Maybe<never> = def('Nothing')\n * const Just = <A>(a: A): Maybe<A> def('Just', a)\n *\n * const maybeString: Maybe<string> = Nothing\n * ```\n */\nexport function def<D extends Def<string, AnyTuple>, T extends D[typeof TAG]>(\n  tag: string extends T ? never : T\n): D extends Def<string, AnyTuple> ? Def<T, []> : Def<T, []> extends D ? D : Def<T, []>\n\n/**\n * Constructs a tagged variant parameterized by some type.\n * Used to define n-arity* data constructors for tagged sum variants.\n *\n * * _\"n-arity\" meaning it requires some number of arguments to construct_\n *\n * @example\n * ```ts\n * type Maybe<A> =\n *   | Def<'Nothing'>\n *   | Def<'Just', [A]>\n *\n * const Nothing: Maybe<never> = def('Nothing')\n * const Just = <A>(a: A): Maybe<A> def('Just', a)\n *\n * const maybeString: Maybe<string> = Just(\"hello\")\n * ```\n */\nexport function def<\n  D extends Def<string, AnyTuple>,\n  T extends D[typeof TAG],\n  V extends Def<string, AnyTuple> extends D ? AnyTuple : TaggedUnionMember<D, typeof TAG, T>[T]\n>(\n  tag: string extends T ? never : T,\n  ...values: V\n): Def<string, AnyTuple> extends D ? Def<typeof tag, typeof values> : D\n\nexport function def<\n  D extends Def<string, AnyTuple>,\n  T extends D[typeof TAG],\n  V extends Def<string, AnyTuple> extends D ? AnyTuple : TaggedUnionMember<D, typeof TAG, T>[T]\n>(tag: string extends T ? never : T, ...values: V) {\n  return {\n    [TAG]: tag,\n    [tag]: values\n  }\n}\n\n/**\n * A curried, data-last (i.e. pipeable) version of [caseWhen](#caseWhen)\n *\n * Pattern matching for tagged unions. Supply a struct of tag-handler pairs\n * to handle each case of the tagged sum. Use the `_` pattern to provide a\n * fallback handler if you don't want to handle every variant of the union.\n *\n * @example\n * ```ts\n *  export type List<A> =\n *   | Def<'Nil'>\n *   | Def<'Cons', [MoreList<A>]>\n *\n * interface MoreList<A> {\n *   head: A\n *   tail: List<A>\n * }\n *\n * export const Nil: List<never> = def('Nil')\n * export const Cons = <A>(a: A) => (ls: List<A>): List<A> =>\n *   def('Cons', { head: a, tail: ls })\n *\n * const ls: List<string> = Cons('hello')(Nil)\n *\n * const num: number = pipe(\n *   ls,\n *   caseOf({\n *     Nil: () => 0,\n *     Cons: ({head, tail}) => head.length\n *   })\n * )\n *\n * const fallbackCase = pipe(\n *   ls,\n *   caseOf({\n *     Cons: ({head, tail}) => head,\n *     _: () => 'fallback'\n *   })\n * )\n *\n * expect(num).toEqual('hello'.length)\n * expect(fallbackCase).toEqual('fallback')\n * ```\n *\n * @param D a tagged variant against which to pattern match\n * @param C an object/struct defining how to handle all possible variants of the tagged sum\n */\nexport function caseOf<D extends Def<string, AnyTuple>, C extends CaseOfStruct<D, unknown>>(\n  cases: StrictCaseOfStruct<D, C>\n): (data: D) => CaseOfReturn<D, C> {\n  return data => caseWhen(data, cases)\n}\n\n/**\n * Pattern matching for tagged unions. Supply a struct of tag-handler pairs\n * to handle each case of the tagged sum. Use the `_` pattern to provide a\n * fallback handler if you don't want to handle every variant of the union.\n *\n * @example\n * ```ts\n * export type List<A> =\n *   | Def<'Nil'>\n *   | Def<'Cons', [MoreList<A>]>\n *\n * interface MoreList<A> {\n *   head: A\n *   tail: List<A>\n * }\n *\n * export const Nil: List<never> = def('Nil')\n * export const Cons = <A>(a: A) => (ls: List<A>): List<A> =>\n *   def('Cons', { head: a, tail: ls })\n *\n * const ls: List<string> = Cons('hello')(Nil)\n *\n * const num: number = caseWhen(ls, {\n *   Nil: () => 0,\n *   Cons: ({head, tail}) => head.length\n * })\n *\n * const fallbackCase = caseWhen(ls, {\n *   Cons: ({head, tail}) => head,\n *   _: () => 'fallback'\n * })\n *\n * expect(num).toEqual('hello'.length)\n * expect(fallbackCase).toEqual('fallback')\n * ```\n *\n * @param D a tagged variant against which to pattern match\n * @param C an object/struct defining how to handle all possible variants of the tagged sum\n */\nexport function caseWhen<D extends Def<string, AnyTuple>, C extends CaseOfStruct<D, unknown>>(\n  data: D,\n  cases: StrictCaseOfStruct<D, C>\n): CaseOfReturn<D, C> {\n  if (isPartialCaseOfStruct<D, CaseOfReturn<D, C>>(cases)) {\n    const tag: D[typeof TAG] = data[TAG]\n    const vals: D[D[typeof TAG]] = data[tag]\n    const handler = cases[tag]\n    const fallback: () => CaseOfReturn<D, C> = cases['_']\n\n    if (isCaseHandler<D[D[typeof TAG]], CaseOfReturn<D, C>>(handler, ...vals)) {\n      return handler(...vals)\n    } else {\n      return fallback()\n    }\n  }\n  const tag: D[typeof TAG] = data[TAG]\n  const vals: D[D[typeof TAG]] = data[tag]\n  const handler = (cases[tag] as unknown) as (...v: typeof vals) => CaseOfReturn<D, C>\n\n  return handler(...vals)\n}\n\n// ----------------------------------------------\n\nfunction isPartialCaseOfStruct<D extends Def<string, AnyTuple>, R>(\n  v: unknown\n): v is PartialCaseOfStruct<D, R> {\n  return typeof v === 'object' && typeof (v as any)['_'] === 'function'\n}\n\nfunction isCaseHandler<T extends AnyTuple, R>(f: unknown, ...v: T): f is (...val: T) => R {\n  if (typeof f === 'function') {\n    try {\n      f(...v)\n      return true\n    } catch (e) {\n      /* istanbul ignore next */\n      return false\n    }\n  } else {\n    return false\n  }\n}\n"]}