(\n xs: readonly a[],\n f: (v: a) => readonly b[]\n): b[] => xs.reduce((acc, x) => acc.concat(f(x)), []);\n", "import { MatchedValue, Pattern } from './types/Pattern';\nimport * as P from './patterns';\nimport { matchPattern } from './internals/helpers';\n\n/**\n * `isMatching` takes pattern and returns a **type guard** function, cheching if a value matches this pattern.\n *\n * [Read documentation for `isMatching` on GitHub](https://github.com/gvergnaud/ts-pattern#ismatching)\n *\n * @example\n * const hasName = isMatching({ name: P.string })\n *\n * declare let input: unknown\n *\n * if (hasName(input)) {\n * // `input` inferred as { name: string }\n * return input.name\n * }\n */\nexport function isMatching>(\n pattern: p\n): (value: unknown) => value is P.infer
;\n/**\n * `isMatching` takes pattern and a value and checks if the value matches this pattern.\n *\n * [Read documentation for `isMatching` on GitHub](https://github.com/gvergnaud/ts-pattern#ismatching)\n *\n * @example\n * declare let input: unknown\n *\n * if (isMatching({ name: P.string }, input)) {\n * // `input` inferred as { name: string }\n * return input.name\n * }\n */\nexport function isMatching>(\n pattern: p,\n value: unknown\n): value is P.infer;\n\nexport function isMatching>(\n ...args: [pattern: p, value?: any]\n): boolean | ((vale: any) => boolean) {\n if (args.length === 1) {\n const [pattern] = args;\n return (value: any): value is MatchedValue> =>\n matchPattern(pattern, value, () => {});\n }\n if (args.length === 2) {\n const [pattern, value] = args;\n return matchPattern(pattern, value, () => {});\n }\n\n throw new Error(\n `isMatching wasn't given the right number of arguments: expected 1 or 2, received ${args.length}.`\n );\n}\n", "import { matchPattern, getSelectionKeys, flatMap } from './internals/helpers';\nimport * as symbols from './internals/symbols';\nimport { matcher } from './internals/symbols';\nimport { isMatching } from './is-matching';\nimport { ExtractPreciseValue } from './types/ExtractPreciseValue';\nimport { Fn } from './types/helpers';\nimport { InvertPattern } from './types/InvertPattern';\nimport {\n Pattern,\n UnknownPattern,\n OptionalP,\n ArrayP,\n MapP,\n SetP,\n AndP,\n OrP,\n NotP,\n GuardP,\n SelectP,\n AnonymousSelectP,\n GuardExcludeP,\n CustomP,\n Matcher,\n StringPattern,\n AnyPattern,\n NumberPattern,\n BooleanPattern,\n BigIntPattern,\n NullishPattern,\n SymbolPattern,\n Chainable,\n BigIntChainable,\n NumberChainable,\n StringChainable,\n ArrayChainable,\n Variadic,\n} from './types/Pattern';\n\nexport { Pattern, Fn as unstable_Fn };\n\nexport { matcher };\n\n/**\n * @experimental\n * A `Matchable` is an object implementing\n * the Matcher Protocol. It must have a `[P.matcher]: P.Matcher`\n * key, which defines how this object should be matched by TS-Pattern.\n *\n * Note that this api is unstable.\n *\n * @example\n * ```ts\n * class Some implements P.unstable_Matchable {\n * [P.matcher](): P.unstable_Matcher>\n * }\n * ```\n */\nexport type unstable_Matchable<\n narrowedOrFn,\n input = unknown,\n pattern = never\n> = CustomP;\n\n/**\n * @experimental\n * A `Matcher` is an object with `match` function, which\n * defines how this object should be matched by TS-Pattern.\n *\n * Note that this api is unstable.\n *\n * @example\n * ```ts\n * class Some implements P.unstable_Matchable {\n * [P.matcher](): P.unstable_Matcher>\n * }\n * ```\n */\nexport type unstable_Matcher<\n narrowedOrFn,\n input = unknown,\n pattern = never\n> = ReturnType[matcher]>;\n\n/**\n * `P.infer` will return the type of the value\n * matched by this pattern.\n *\n * [Read the documentation for `P.infer` on GitHub](https://github.com/gvergnaud/ts-pattern#pinfer)\n *\n * @example\n * const userPattern = { name: P.string }\n * type User = P.infer\n */\nexport type infer> = InvertPattern<\n pattern,\n unknown\n>;\n\n/**\n * `P.narrow` will narrow the input type to only keep\n * the set of values that are compatible with the provided pattern type.\n *\n * [Read the documentation for `P.narrow` on GitHub](https://github.com/gvergnaud/ts-pattern#pnarrow)\n *\n * @example\n * type Input = ['a' | 'b' | 'c', 'a' | 'b' | 'c']\n * const Pattern = ['a', P.union('a', 'b')] as const\n *\n * type Narrowed = P.narrow\n * // ^? ['a', 'a' | 'b']\n */\nexport type narrow> = ExtractPreciseValue<\n input,\n InvertPattern\n>;\n\nfunction chainable>(\n pattern: pattern\n): Chainable {\n return Object.assign(pattern, {\n optional: () => optional(pattern),\n and: (p2: any) => intersection(pattern, p2),\n or: (p2: any) => union(pattern, p2),\n select: (key: any) =>\n key === undefined ? select(pattern) : select(key, pattern),\n }) as Chainable;\n}\n\nconst variadic = (pattern: pattern): Variadic =>\n Object.assign(pattern, {\n *[Symbol.iterator]() {\n yield Object.assign(pattern, {\n [symbols.isVariadic]: true,\n });\n },\n });\n\nfunction arrayChainable>(\n pattern: pattern\n): ArrayChainable {\n return Object.assign(variadic(pattern), {\n optional: () => arrayChainable(optional(pattern)),\n select: (key: any) =>\n arrayChainable(\n key === undefined ? select(pattern) : select(key, pattern)\n ),\n }) as any;\n}\n\n/**\n * `P.optional(subpattern)` takes a sub pattern and returns a pattern which matches if the\n * key is undefined or if it is defined and the sub pattern matches its value.\n *\n * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#poptional-patterns)\n *\n * @example\n * match(value)\n * .with({ greeting: P.optional('Hello') }, () => 'will match { greeting?: \"Hello\" }')\n */\nexport function optional<\n input,\n const pattern extends unknown extends input ? UnknownPattern : Pattern\n>(pattern: pattern): Chainable, 'optional'> {\n return chainable({\n [matcher]() {\n return {\n match: (value: UnknownInput | input) => {\n let selections: Record = {};\n const selector = (key: string, value: any) => {\n selections[key] = value;\n };\n if (value === undefined) {\n getSelectionKeys(pattern).forEach((key) =>\n selector(key, undefined)\n );\n return { matched: true, selections };\n }\n const matched = matchPattern(pattern, value, selector);\n return { matched, selections };\n },\n getSelectionKeys: () => getSelectionKeys(pattern),\n matcherType: 'optional',\n };\n },\n });\n}\n\ntype UnwrapArray = xs extends readonly (infer x)[] ? x : never;\n\ntype UnwrapSet = xs extends Set ? x : never;\n\ntype UnwrapMapKey = xs extends Map ? k : never;\n\ntype UnwrapMapValue = xs extends Map ? v : never;\n\ntype WithDefault = [a] extends [never] ? b : a;\n\n/**\n * `P.array(subpattern)` takes a sub pattern and returns a pattern, which matches\n * arrays if all their elements match the sub pattern.\n *\n * [Read the documentation for `P.array` on GitHub](https://github.com/gvergnaud/ts-pattern#parray-patterns)\n *\n * @example\n * match(value)\n * .with({ users: P.array({ name: P.string }) }, () => 'will match { name: string }[]')\n */\nexport function array(): ArrayChainable>;\nexport function array<\n input,\n const pattern extends Pattern, unknown>>\n>(pattern: pattern): ArrayChainable>;\nexport function array(\n ...args: [pattern?: any]\n): ArrayChainable> {\n return arrayChainable({\n [matcher]() {\n return {\n match: (value: any) => {\n if (!Array.isArray(value)) return { matched: false };\n\n if (args.length === 0) return { matched: true };\n\n const pattern = args[0];\n let selections: Record = {};\n\n if (value.length === 0) {\n getSelectionKeys(pattern).forEach((key) => {\n selections[key] = [];\n });\n return { matched: true, selections };\n }\n\n const selector = (key: string, value: unknown) => {\n selections[key] = (selections[key] || []).concat([value]);\n };\n\n const matched = value.every((v) =>\n matchPattern(pattern, v, selector)\n );\n\n return { matched, selections };\n },\n getSelectionKeys: () =>\n args.length === 0 ? [] : getSelectionKeys(args[0]),\n };\n },\n });\n}\n\n/**\n * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches\n * sets if all their elements match the sub pattern.\n *\n * [Read `P.set` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pset-patterns)\n *\n * @example\n * match(value)\n * .with({ users: P.set(P.string) }, () => 'will match Set')\n */\nexport function set(): Chainable>;\nexport function set<\n input,\n const pattern extends Pattern, unknown>>\n>(pattern: pattern): Chainable>;\nexport function set<\n input,\n const pattern extends Pattern, unknown>>\n>(...args: [pattern?: pattern]): Chainable> {\n return chainable({\n [matcher]() {\n return {\n match: (value: UnknownInput | input) => {\n if (!(value instanceof Set)) return { matched: false };\n\n let selections: Record = {};\n\n if (value.size === 0) {\n return { matched: true, selections };\n }\n\n if (args.length === 0) return { matched: true };\n\n const selector = (key: string, value: unknown) => {\n selections[key] = (selections[key] || []).concat([value]);\n };\n\n const pattern = args[0];\n\n const matched = setEvery(value, (v) =>\n matchPattern(pattern, v, selector)\n );\n\n return { matched, selections };\n },\n getSelectionKeys: () =>\n args.length === 0 ? [] : getSelectionKeys(args[0]),\n };\n },\n });\n}\n\nconst setEvery = (set: Set, predicate: (value: T) => boolean) => {\n for (const value of set) {\n if (predicate(value)) continue;\n return false;\n }\n return true;\n};\n\n/**\n * `P.set(subpattern)` takes a sub pattern and returns a pattern that matches\n * sets if all their elements match the sub pattern.\n *\n * [Read `P.set` documentation on GitHub](https://github.com/gvergnaud/ts-pattern#pset-patterns)\n *\n * @example\n * match(value)\n * .with({ users: P.set(P.string) }, () => 'will match Set')\n */\nexport function map(): Chainable>;\nexport function map<\n input,\n const pkey extends Pattern, unknown>>,\n const pvalue extends Pattern, unknown>>\n>(patternKey: pkey, patternValue: pvalue): Chainable>;\nexport function map<\n input,\n const pkey extends Pattern, unknown>>,\n const pvalue extends Pattern, unknown>>\n>(\n ...args: [patternKey?: pkey, patternValue?: pvalue]\n): Chainable> {\n return chainable({\n [matcher]() {\n return {\n match: (value: UnknownInput | input) => {\n if (!(value instanceof Map)) return { matched: false };\n\n let selections: Record = {};\n\n if (value.size === 0) {\n return { matched: true, selections };\n }\n\n const selector = (key: string, value: unknown) => {\n selections[key] = (selections[key] || []).concat([value]);\n };\n\n if (args.length === 0) return { matched: true };\n if (args.length === 1) {\n throw new Error(\n `\\`P.map\\` wasn\\'t given enough arguments. Expected (key, value), received ${args[0]?.toString()}`\n );\n }\n const [patternKey, patternValue] = args;\n\n const matched = mapEvery(value, (v, k) => {\n const keyMatch = matchPattern(patternKey, k, selector);\n const valueMatch = matchPattern(patternValue, v, selector);\n return keyMatch && valueMatch;\n });\n\n return { matched, selections };\n },\n getSelectionKeys: () =>\n args.length === 0\n ? []\n : [...getSelectionKeys(args[0]), ...getSelectionKeys(args[1])],\n };\n },\n });\n}\n\nconst mapEvery = (\n map: Map,\n predicate: (value: T, key: K) => boolean\n) => {\n for (const [key, value] of map.entries()) {\n if (predicate(value, key)) continue;\n return false;\n }\n return true;\n};\n\n/**\n * `P.intersection(...patterns)` returns a pattern which matches\n * only if **every** patterns provided in parameter match the input.\n *\n * [Read the documentation for `P.intersection` on GitHub](https://github.com/gvergnaud/ts-pattern#pintersection-patterns)\n *\n * @example\n * match(value)\n * .with(\n * {\n * user: P.intersection(\n * { firstname: P.string },\n * { lastname: P.string },\n * { age: P.when(age => age > 21) }\n * )\n * },\n * ({ user }) => 'will match { firstname: string, lastname: string, age: number } if age > 21'\n * )\n */\nexport function intersection<\n input,\n const patterns extends readonly [Pattern, ...Pattern[]]\n>(...patterns: patterns): Chainable> {\n return chainable({\n [matcher]: () => ({\n match: (value) => {\n let selections: Record = {};\n const selector = (key: string, value: any) => {\n selections[key] = value;\n };\n const matched = (patterns as readonly UnknownPattern[]).every((p) =>\n matchPattern(p, value, selector)\n );\n return { matched, selections };\n },\n getSelectionKeys: () =>\n flatMap(patterns as readonly UnknownPattern[], getSelectionKeys),\n matcherType: 'and',\n }),\n });\n}\n\n/**\n * `P.union(...patterns)` returns a pattern which matches\n * if **at least one** of the patterns provided in parameter match the input.\n *\n * [Read the documentation for `P.union` on GitHub](https://github.com/gvergnaud/ts-pattern#punion-patterns)\n *\n * @example\n * match(value)\n * .with(\n * { type: P.union('a', 'b', 'c') },\n * ({ type }) => 'will match { type: \"a\" | \"b\" | \"c\" }'\n * )\n */\nexport function union<\n input,\n const patterns extends readonly [Pattern, ...Pattern[]]\n>(...patterns: patterns): Chainable> {\n return chainable({\n [matcher]: () => ({\n match: (value: UnknownInput | input) => {\n let selections: Record = {};\n const selector = (key: string, value: any) => {\n selections[key] = value;\n };\n flatMap(\n patterns as readonly UnknownPattern[],\n getSelectionKeys\n ).forEach((key) => selector(key, undefined));\n const matched = (patterns as readonly UnknownPattern[]).some((p) =>\n matchPattern(p, value, selector)\n );\n return { matched, selections };\n },\n getSelectionKeys: () =>\n flatMap(patterns as readonly UnknownPattern[], getSelectionKeys),\n matcherType: 'or',\n }),\n });\n}\n\n/**\n * `P.not(pattern)` returns a pattern which matches if the sub pattern\n * doesn't match.\n *\n * [Read the documentation for `P.not` on GitHub](https://github.com/gvergnaud/ts-pattern#pnot-patterns)\n *\n * @example\n * match<{ a: string | number }>(value)\n * .with({ a: P.not(P.string) }, (x) => 'will match { a: number }'\n * )\n */\n\nexport function not<\n input,\n const pattern extends Pattern | UnknownPattern\n>(pattern: pattern): Chainable> {\n return chainable({\n [matcher]: () => ({\n match: (value: UnknownInput | input) => ({\n matched: !matchPattern(pattern, value, () => {}),\n }),\n getSelectionKeys: () => [],\n matcherType: 'not',\n }),\n });\n}\n\n/**\n * `P.when((value) => boolean)` returns a pattern which matches\n * if the predicate returns true for the current input.\n *\n * [Read the documentation for `P.when` on GitHub](https://github.com/gvergnaud/ts-pattern#pwhen-patterns)\n *\n * @example\n * match<{ age: number }>(value)\n * .with({ age: P.when(age => age > 21) }, (x) => 'will match if value.age > 21'\n * )\n */\nexport function when unknown>(\n predicate: predicate\n): GuardP<\n input,\n predicate extends (value: any) => value is infer narrowed ? narrowed : never\n>;\nexport function when(\n predicate: (input: input) => input is narrowed\n): GuardExcludeP;\nexport function when unknown>(\n predicate: predicate\n): GuardP<\n input,\n predicate extends (value: any) => value is infer narrowed ? narrowed : never\n> {\n return {\n [matcher]: () => ({\n match: (value: UnknownInput | input) => ({\n matched: Boolean(predicate(value as input)),\n }),\n }),\n };\n}\n\n/**\n * `P.select()` is a pattern which will always match,\n * and will inject the selected piece of input in the handler function.\n *\n * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#pselect-patterns)\n *\n * @example\n * match<{ age: number }>(value)\n * .with({ age: P.select() }, (age) => 'age: number'\n * )\n */\nexport function select(): Chainable;\nexport function select<\n input,\n const patternOrKey extends\n | string\n | (unknown extends input ? UnknownPattern : Pattern)\n>(\n patternOrKey: patternOrKey\n): patternOrKey extends string\n ? Chainable>\n : Chainable<\n SelectP,\n 'select' | 'or' | 'and'\n >;\nexport function select<\n input,\n const pattern extends unknown extends input ? UnknownPattern : Pattern,\n const k extends string\n>(\n key: k,\n pattern: pattern\n): Chainable, 'select' | 'or' | 'and'>;\nexport function select(\n ...args: [keyOrPattern?: unknown | string, pattern?: unknown]\n): Chainable, 'select' | 'or' | 'and'> {\n const key: string | undefined =\n typeof args[0] === 'string' ? args[0] : undefined;\n const pattern: unknown =\n args.length === 2\n ? args[1]\n : typeof args[0] === 'string'\n ? undefined\n : args[0];\n return chainable({\n [matcher]() {\n return {\n match: (value) => {\n let selections: Record = {\n [key ?? symbols.anonymousSelectKey]: value,\n };\n const selector = (key: string, value: any) => {\n selections[key] = value;\n };\n return {\n matched:\n pattern === undefined\n ? true\n : matchPattern(pattern, value, selector),\n selections: selections,\n };\n },\n getSelectionKeys: () =>\n [key ?? symbols.anonymousSelectKey].concat(\n pattern === undefined ? [] : getSelectionKeys(pattern)\n ),\n };\n },\n });\n}\n\nfunction isUnknown(x: unknown): x is unknown {\n return true;\n}\n\nfunction isNumber(x: T | number): x is number {\n return typeof x === 'number';\n}\n\nfunction isString(x: T | string): x is string {\n return typeof x === 'string';\n}\n\nfunction isBoolean(x: T | boolean): x is boolean {\n return typeof x === 'boolean';\n}\n\nfunction isBigInt(x: T | bigint): x is bigint {\n return typeof x === 'bigint';\n}\n\nfunction isSymbol(x: T | symbol): x is symbol {\n return typeof x === 'symbol';\n}\n\nfunction isNullish(x: T | null | undefined): x is null | undefined {\n return x === null || x === undefined;\n}\n\ntype AnyConstructor = abstract new (...args: any[]) => any;\n\nfunction isInstanceOf(classConstructor: T) {\n return (val: unknown): val is InstanceType