@cjser/ts-pattern 5.9.0-cjser.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1776 -0
  3. package/dist/errors.d.cts +8 -0
  4. package/dist/errors.d.ts +8 -0
  5. package/dist/index.cjs +2 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +5 -0
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/index.umd.js +2 -0
  12. package/dist/index.umd.js.map +1 -0
  13. package/dist/internals/helpers.d.cts +12 -0
  14. package/dist/internals/helpers.d.ts +12 -0
  15. package/dist/internals/symbols.d.cts +24 -0
  16. package/dist/internals/symbols.d.ts +24 -0
  17. package/dist/is-matching.d.cts +40 -0
  18. package/dist/is-matching.d.ts +40 -0
  19. package/dist/match.d.cts +19 -0
  20. package/dist/match.d.ts +19 -0
  21. package/dist/patterns.d.cts +345 -0
  22. package/dist/patterns.d.ts +345 -0
  23. package/dist/types/BuildMany.d.cts +22 -0
  24. package/dist/types/BuildMany.d.ts +22 -0
  25. package/dist/types/DeepExclude.d.cts +2 -0
  26. package/dist/types/DeepExclude.d.ts +2 -0
  27. package/dist/types/DistributeUnions.d.cts +111 -0
  28. package/dist/types/DistributeUnions.d.ts +111 -0
  29. package/dist/types/ExtractPreciseValue.d.cts +27 -0
  30. package/dist/types/ExtractPreciseValue.d.ts +27 -0
  31. package/dist/types/FindSelected.d.cts +78 -0
  32. package/dist/types/FindSelected.d.ts +78 -0
  33. package/dist/types/InvertPattern.d.cts +104 -0
  34. package/dist/types/InvertPattern.d.ts +104 -0
  35. package/dist/types/IsMatching.d.cts +13 -0
  36. package/dist/types/IsMatching.d.ts +13 -0
  37. package/dist/types/Match.d.cts +171 -0
  38. package/dist/types/Match.d.ts +171 -0
  39. package/dist/types/Pattern.d.cts +401 -0
  40. package/dist/types/Pattern.d.ts +401 -0
  41. package/dist/types/helpers.d.cts +80 -0
  42. package/dist/types/helpers.d.ts +80 -0
  43. package/dist/types/index.d.cts +1 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist-cjser/index.cjs +319 -0
  46. package/package.json +135 -0
@@ -0,0 +1,401 @@
1
+ import type * as symbols from '../internals/symbols.js';
2
+ import { MergeUnion, Primitives, WithDefault } from './helpers.js';
3
+ import { None, Some, SelectionType } from './FindSelected.js';
4
+ import { matcher } from '../patterns.js';
5
+ import { ExtractPreciseValue } from './ExtractPreciseValue.js';
6
+ export type MatcherType = 'not' | 'optional' | 'or' | 'and' | 'array' | 'record' | 'map' | 'set' | 'select' | 'default' | 'custom';
7
+ export type MatcherProtocol<input, narrowed, matcherType extends MatcherType, selections extends SelectionType, excluded> = {
8
+ match: <I>(value: I | input) => MatchResult;
9
+ getSelectionKeys?: () => string[];
10
+ matcherType?: matcherType;
11
+ };
12
+ export type MatchResult = {
13
+ matched: boolean;
14
+ selections?: Record<string, any>;
15
+ };
16
+ /**
17
+ * A `Matcher` is an object implementing the match
18
+ * protocol. It must define a `symbols.matcher` property
19
+ * which returns an object with a `match()` method, taking
20
+ * the input value and returning whether the pattern matches
21
+ * or not, along with optional selections.
22
+ */
23
+ export interface Matcher<input, narrowed, matcherType extends MatcherType = 'default', selections extends SelectionType = None, excluded = narrowed> {
24
+ [matcher](): MatcherProtocol<input, narrowed, matcherType, selections, excluded>;
25
+ [symbols.isVariadic]?: boolean;
26
+ }
27
+ type PatternMatcher<input> = Matcher<input, unknown, any, any>;
28
+ export type MatchedValue<a, invpattern> = WithDefault<ExtractPreciseValue<a, invpattern>, a>;
29
+ export type AnyMatcher = Matcher<any, any, any, any, any>;
30
+ type UnknownMatcher = PatternMatcher<unknown>;
31
+ export type CustomP<input, pattern, narrowedOrFn> = Matcher<input, pattern, 'custom', None, narrowedOrFn>;
32
+ export type ArrayP<input, p> = Matcher<input, p, 'array'>;
33
+ export type RecordP<input, pkey, pvalue> = Matcher<input, [
34
+ pkey,
35
+ pvalue
36
+ ], 'record'>;
37
+ export type OptionalP<input, p> = Matcher<input, p, 'optional'>;
38
+ export type MapP<input, pkey, pvalue> = Matcher<input, [pkey, pvalue], 'map'>;
39
+ export type SetP<input, p> = Matcher<input, p, 'set'>;
40
+ export type AndP<input, ps> = Matcher<input, ps, 'and'>;
41
+ export type OrP<input, ps> = Matcher<input, ps, 'or'>;
42
+ export type NotP<input, p> = Matcher<input, p, 'not'>;
43
+ export type GuardP<input, narrowed> = Matcher<input, narrowed>;
44
+ export type GuardExcludeP<input, narrowed, excluded> = Matcher<input, narrowed, 'default', None, excluded>;
45
+ export type SelectP<key extends string, input = unknown, p = Matcher<unknown, unknown>> = Matcher<input, p, 'select', Some<key>>;
46
+ export type AnonymousSelectP = SelectP<symbols.anonymousSelectKey>;
47
+ export interface Override<a> {
48
+ [symbols.override]: a;
49
+ }
50
+ export type UnknownProperties = {
51
+ readonly [k: PropertyKey]: unknown;
52
+ };
53
+ export type UnknownValuePattern = readonly [] | readonly [unknown, ...unknown[]] | readonly [...unknown[], unknown] | UnknownProperties | Primitives | UnknownMatcher;
54
+ /**
55
+ * `Pattern<a>` is the generic type for patterns matching a value of type `a`. A pattern can be any (nested) javascript value.
56
+ *
57
+ * They can also be wildcards, like `P._`, `P.string`, `P.number`,
58
+ * or other matchers, like `P.when(predicate)`, `P.not(pattern)`, etc.
59
+ *
60
+ * [Read the documentation for `P.Pattern` on GitHub](https://github.com/gvergnaud/ts-pattern#patterns)
61
+ *
62
+ * @example
63
+ * const pattern: P.Pattern<User> = { name: P.string }
64
+ */
65
+ export type Pattern<a = unknown> = unknown extends a ? UnknownValuePattern : KnownPattern<a>;
66
+ type KnownPattern<a> = KnownPatternInternal<a>;
67
+ type KnownPatternInternal<a, objs = Exclude<a, Primitives | Map<any, any> | Set<any> | readonly any[]>, arrays = Extract<a, readonly any[]>, primitives = Extract<a, Primitives>> = primitives | PatternMatcher<a> | ([objs] extends [never] ? never : ObjectPattern<Readonly<MergeUnion<objs>>>) | ([arrays] extends [never] ? never : ArrayPattern<arrays>);
68
+ type ObjectPattern<a> = {
69
+ readonly [k in keyof a]?: Pattern<a[k]>;
70
+ } | never;
71
+ type ArrayPattern<a> = a extends readonly (infer i)[] ? a extends readonly [any, ...any] ? {
72
+ readonly [index in keyof a]: Pattern<a[index]>;
73
+ } : readonly [] | readonly [Pattern<i>, ...Pattern<i>[]] | readonly [...Pattern<i>[], Pattern<i>] : never;
74
+ export type UnknownPattern = Chainable<GuardP<unknown, unknown>, never>;
75
+ export type StringPattern = StringChainable<GuardP<unknown, string>, never>;
76
+ export type NumberPattern = NumberChainable<GuardP<unknown, number>, never>;
77
+ export type BooleanPattern = Chainable<GuardP<unknown, boolean>, never>;
78
+ export type BigIntPattern = BigIntChainable<GuardP<unknown, bigint>, never>;
79
+ export type SymbolPattern = Chainable<GuardP<unknown, symbol>, never>;
80
+ export type NullishPattern = Chainable<GuardP<unknown, null | undefined>, never>;
81
+ export type NonNullablePattern = Chainable<GuardP<unknown, {}>, never>;
82
+ type MergeGuards<input, guard1, guard2> = [guard1, guard2] extends [
83
+ GuardExcludeP<any, infer narrowed1, infer excluded1>,
84
+ GuardExcludeP<any, infer narrowed2, infer excluded2>
85
+ ] ? GuardExcludeP<input, narrowed1 & narrowed2, excluded1 & excluded2> : never;
86
+ export type Chainable<p, omitted extends string = never> = p & Omit<{
87
+ /**
88
+ * `.optional()` returns a pattern which matches if the
89
+ * key is undefined or if it is defined and the previous pattern matches its value.
90
+ *
91
+ * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#poptional-patterns)
92
+ *
93
+ * @example
94
+ * match(value)
95
+ * .with({ greeting: P.string.optional() }, () => 'will match { greeting?: string}')
96
+ */
97
+ optional<input>(): Chainable<OptionalP<input, p>, omitted | 'optional'>;
98
+ /**
99
+ * `pattern.and(pattern)` returns a pattern that matches
100
+ * if the previous pattern and the next one match the input.
101
+ *
102
+ * [Read the documentation for `P.intersection` on GitHub](https://github.com/gvergnaud/ts-pattern#pintersection-patterns)
103
+ *
104
+ * @example
105
+ * match(value)
106
+ * .with(
107
+ * P.string.and(P.when(isUsername)),
108
+ * (username) => '...'
109
+ * )
110
+ */
111
+ and<input, const p2 extends Pattern<input>>(pattern: p2): Chainable<AndP<input, [p, p2]>, omitted>;
112
+ /**
113
+ * `pattern.or(pattern)` returns a pattern that matches
114
+ * if **either** the previous pattern or the next one match the input.
115
+ *
116
+ * [Read the documentation for `P.union` on GitHub](https://github.com/gvergnaud/ts-pattern#punion-patterns)
117
+ *
118
+ * @example
119
+ * match(value)
120
+ * .with(
121
+ * { value: P.string.or(P.number) },
122
+ * ({ value }) => 'value: number | string'
123
+ * )
124
+ */
125
+ or<input, const p2 extends Pattern<input>>(pattern: p2): Chainable<OrP<input, [p, p2]>, omitted>;
126
+ /**
127
+ * `P.select()` will inject this property into the handler function's arguments.
128
+ *
129
+ * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#pselect-patterns)
130
+ *
131
+ * @example
132
+ * match<{ age: number }>(value)
133
+ * .with({ age: P.string.select() }, (age) => 'age: number')
134
+ */
135
+ select<input>(): Chainable<SelectP<symbols.anonymousSelectKey, input, p>, omitted | 'select' | 'or' | 'and'>;
136
+ select<input, k extends string>(key: k): Chainable<SelectP<k, input, p>, omitted | 'select' | 'or' | 'and'>;
137
+ }, omitted>;
138
+ export type StringChainable<p extends Matcher<any, any, any, any, any>, omitted extends string = never> = Chainable<p, omitted> & Omit<{
139
+ /**
140
+ * `P.string.startsWith(start)` is a pattern, matching **strings** starting with `start`.
141
+ *
142
+ * [Read the documentation for `P.string.startsWith` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringstartsWith)
143
+ *
144
+ * @example
145
+ * match(value)
146
+ * .with(P.string.startsWith('A'), () => 'value starts with an A')
147
+ */
148
+ startsWith<input, const start extends string>(start: start): StringChainable<MergeGuards<input, p, GuardP<unknown, `${start}${string}`>>, omitted | 'startsWith'>;
149
+ /**
150
+ * `P.string.endsWith(end)` is a pattern, matching **strings** ending with `end`.
151
+ *
152
+ * [Read the documentation for `P.string.endsWith` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringendsWith)
153
+ *
154
+ * @example
155
+ * match(value)
156
+ * .with(P.string.endsWith('!'), () => 'value ends with an !')
157
+ */
158
+ endsWith<input, const end extends string>(end: end): StringChainable<MergeGuards<input, p, GuardP<unknown, `${string}${end}`>>, omitted | 'endsWith'>;
159
+ /**
160
+ * `P.string.minLength(min)` is a pattern, matching **strings** with at least `min` characters.
161
+ *
162
+ * [Read the documentation for `P.string.minLength` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringminLength)
163
+ *
164
+ * @example
165
+ * match(value)
166
+ * .with(P.string.minLength(10), () => 'string with more length <= 10')
167
+ */
168
+ minLength<input, const min extends number>(min: min): StringChainable<MergeGuards<input, p, GuardExcludeP<unknown, string, never>>, omitted | 'minLength'>;
169
+ /**
170
+ * `P.string.length(len)` is a pattern, matching **strings** with exactly `len` characters.
171
+ *
172
+ * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength)
173
+ *
174
+ * @example
175
+ * match(value)
176
+ * .with(P.string.length(10), () => 'strings with length === 10')
177
+ */
178
+ length<input, const len extends number>(len: len): StringChainable<MergeGuards<input, p, GuardExcludeP<unknown, string, never>>, omitted | 'length' | 'minLength' | 'maxLength'>;
179
+ /**
180
+ * `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters.
181
+ *
182
+ * [Read the documentation for `P.string.maxLength` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringmaxLength)
183
+ *
184
+ * @example
185
+ * match(value)
186
+ * .with(P.string.maxLength(10), () => 'string with more length >= 10')
187
+ */
188
+ maxLength<input, const max extends number>(max: max): StringChainable<MergeGuards<input, p, GuardExcludeP<unknown, string, never>>, omitted | 'maxLength'>;
189
+ /**
190
+ * `P.string.includes(substr)` is a pattern, matching **strings** containing `substr`.
191
+ *
192
+ * [Read the documentation for `P.string.includes` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringincludes)
193
+ *
194
+ * @example
195
+ * match(value)
196
+ * .with(P.string.includes('http'), () => 'value contains http')
197
+ */
198
+ includes<input, const substr extends string>(substr: substr): StringChainable<MergeGuards<input, p, GuardExcludeP<unknown, string, never>>, omitted>;
199
+ /**
200
+ * `P.string.regex(expr)` is a pattern, matching **strings** that `expr` regular expression.
201
+ *
202
+ * [Read the documentation for `P.string.regex` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringregex)
203
+ *
204
+ * @example
205
+ * match(value)
206
+ * .with(P.string.regex(/^https?:\/\//), () => 'url')
207
+ */
208
+ regex<input, const expr extends string | RegExp>(expr: expr): StringChainable<MergeGuards<input, p, GuardExcludeP<unknown, string, never>>, omitted>;
209
+ }, omitted>;
210
+ export type NumberChainable<p, omitted extends string = never> = Chainable<p, omitted> & Omit<{
211
+ /**
212
+ * `P.number.between(min, max)` matches **number** between `min` and `max`,
213
+ * equal to min or equal to max.
214
+ *
215
+ * [Read the documentation for `P.number.between` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberbetween)
216
+ *
217
+ * @example
218
+ * match(value)
219
+ * .with(P.number.between(0, 10), () => '0 <= numbers <= 10')
220
+ */
221
+ between<input, const min extends number, const max extends number>(min: min, max: max): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted>;
222
+ /**
223
+ * `P.number.lt(max)` matches **number** smaller than `max`.
224
+ *
225
+ * [Read the documentation for `P.number.lt` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberlt)
226
+ *
227
+ * @example
228
+ * match(value)
229
+ * .with(P.number.lt(10), () => 'numbers < 10')
230
+ */
231
+ lt<input, const max extends number>(max: max): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted>;
232
+ /**
233
+ * `P.number.gt(min)` matches **number** greater than `min`.
234
+ *
235
+ * [Read the documentation for `P.number.gt` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbergt)
236
+ *
237
+ * @example
238
+ * match(value)
239
+ * .with(P.number.gt(10), () => 'numbers > 10')
240
+ */
241
+ gt<input, const min extends number>(min: min): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted>;
242
+ /**
243
+ * `P.number.lte(max)` matches **number** smaller than or equal to `max`.
244
+ *
245
+ * [Read the documentation for `P.number.lte` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberlte)
246
+ *
247
+ * @example
248
+ * match(value)
249
+ * .with(P.number.lte(10), () => 'numbers <= 10')
250
+ */
251
+ lte<input, const max extends number>(max: max): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted>;
252
+ /**
253
+ * `P.number.gte(min)` matches **number** greater than or equal to `min`.
254
+ *
255
+ * [Read the documentation for `P.number.gte` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbergte)
256
+ *
257
+ * @example
258
+ * match(value)
259
+ * .with(P.number.gte(10), () => 'numbers >= 10')
260
+ */
261
+ gte<input, const min extends number>(min: min): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted>;
262
+ /**
263
+ * `P.number.int` matches **integer** numbers.
264
+ *
265
+ * [Read the documentation for `P.number.int` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberint)
266
+ *
267
+ * @example
268
+ * match(value)
269
+ * .with(P.number.int, () => 'an integer')
270
+ */
271
+ int<input>(): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted | 'int'>;
272
+ /**
273
+ * `P.number.finite` matches **finite numbers**.
274
+ *
275
+ * [Read the documentation for `P.number.finite` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberfinite)
276
+ *
277
+ * @example
278
+ * match(value)
279
+ * .with(P.number.finite, () => 'not Infinity')
280
+ */
281
+ finite<input>(): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted | 'finite'>;
282
+ /**
283
+ * `P.number.positive` matches **positive** numbers.
284
+ *
285
+ * [Read the documentation for `P.number.positive` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberpositive)
286
+ *
287
+ * @example
288
+ * match(value)
289
+ * .with(P.number.positive, () => 'number > 0')
290
+ */
291
+ positive<input>(): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted | 'positive' | 'negative'>;
292
+ /**
293
+ * `P.number.negative` matches **negative** numbers.
294
+ *
295
+ * [Read the documentation for `P.number.negative` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbernegative)
296
+ *
297
+ * @example
298
+ * match(value)
299
+ * .with(P.number.negative, () => 'number < 0')
300
+ */
301
+ negative<input>(): NumberChainable<MergeGuards<input, p, GuardExcludeP<unknown, number, never>>, omitted | 'positive' | 'negative' | 'negative'>;
302
+ }, omitted>;
303
+ export type BigIntChainable<p, omitted extends string = never> = Chainable<p, omitted> & Omit<{
304
+ /**
305
+ * `P.bigint.between(min, max)` matches **bigint** between `min` and `max`,
306
+ * equal to min or equal to max.
307
+ *
308
+ * [Read the documentation for `P.bigint.between` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberbetween)
309
+ *
310
+ * @example
311
+ * match(value)
312
+ * .with(P.bigint.between(0, 10), () => '0 <= numbers <= 10')
313
+ */
314
+ between<input, const min extends bigint, const max extends bigint>(min: min, max: max): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted>;
315
+ /**
316
+ * `P.bigint.lt(max)` matches **bigint** smaller than `max`.
317
+ *
318
+ * [Read the documentation for `P.bigint.lt` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberlt)
319
+ *
320
+ * @example
321
+ * match(value)
322
+ * .with(P.bigint.lt(10), () => 'numbers < 10')
323
+ */
324
+ lt<input, const max extends bigint>(max: max): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted>;
325
+ /**
326
+ * `P.bigint.gt(min)` matches **bigint** greater than `min`.
327
+ *
328
+ * [Read the documentation for `P.bigint.gt` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbergt)
329
+ *
330
+ * @example
331
+ * match(value)
332
+ * .with(P.bigint.gt(10), () => 'numbers > 10')
333
+ */
334
+ gt<input, const min extends bigint>(min: min): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted>;
335
+ /**
336
+ * `P.bigint.lte(max)` matches **bigint** smaller than or equal to `max`.
337
+ *
338
+ * [Read the documentation for `P.bigint.lte` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberlte)
339
+ *
340
+ * @example
341
+ * match(value)
342
+ * .with(P.bigint.lte(10), () => 'bigints <= 10')
343
+ */
344
+ lte<input, const max extends bigint>(max: max): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted>;
345
+ /**
346
+ * `P.bigint.gte(min)` matches **bigint** greater than or equal to `min`.
347
+ *
348
+ * [Read the documentation for `P.bigint.gte` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbergte)
349
+ *
350
+ * @example
351
+ * match(value)
352
+ * .with(P.bigint.gte(10), () => 'bigints >= 10')
353
+ */
354
+ gte<input, const min extends bigint>(min: min): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted>;
355
+ /**
356
+ * `P.bigint.positive` matches **positive** bigints.
357
+ *
358
+ * [Read the documentation for `P.bigint.positive` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumberpositive)
359
+ *
360
+ * @example
361
+ * match(value)
362
+ * .with(P.bigint.positive, () => 'bigint > 0')
363
+ */
364
+ positive<input>(): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted | 'positive' | 'negative'>;
365
+ /**
366
+ * `P.bigint.negative` matches **negative** bigints.
367
+ *
368
+ * [Read the documentation for `P.bigint.negative` on GitHub](https://github.com/gvergnaud/ts-pattern#pnumbernegative)
369
+ *
370
+ * @example
371
+ * match(value)
372
+ * .with(P.bigint.negative, () => 'bigint < 0')
373
+ */
374
+ negative<input>(): BigIntChainable<MergeGuards<input, p, GuardExcludeP<unknown, bigint, never>>, omitted | 'positive' | 'negative' | 'negative'>;
375
+ }, omitted>;
376
+ export type Variadic<pattern> = pattern & Iterable<pattern>;
377
+ export type ArrayChainable<pattern, omitted extends string = never> = Variadic<pattern> & Omit<{
378
+ /**
379
+ * `.optional()` returns a pattern which matches if the
380
+ * key is undefined or if it is defined and the previous pattern matches its value.
381
+ *
382
+ * [Read the documentation for `P.optional` on GitHub](https://github.com/gvergnaud/ts-pattern#poptional-patterns)
383
+ *
384
+ * @example
385
+ * match(value)
386
+ * .with({ greeting: P.string.optional() }, () => 'will match { greeting?: string}')
387
+ */
388
+ optional<input>(): ArrayChainable<OptionalP<input, pattern>, omitted | 'optional'>;
389
+ /**
390
+ * `P.select()` will inject this property into the handler function's arguments.
391
+ *
392
+ * [Read the documentation for `P.select` on GitHub](https://github.com/gvergnaud/ts-pattern#pselect-patterns)
393
+ *
394
+ * @example
395
+ * match<{ age: number }>(value)
396
+ * .with({ age: P.string.select() }, (age) => 'age: number')
397
+ */
398
+ select<input>(): ArrayChainable<SelectP<symbols.anonymousSelectKey, input, pattern>, omitted | 'select'>;
399
+ select<input, k extends string>(key: k): ArrayChainable<SelectP<k, input, pattern>, omitted | 'select'>;
400
+ }, omitted>;
401
+ export {};
@@ -0,0 +1,80 @@
1
+ export type ValueOf<a> = a extends readonly any[] ? a[number] : a[keyof a];
2
+ export type Values<a extends object> = UnionToTuple<ValueOf<a>>;
3
+ /**
4
+ * ### LeastUpperBound
5
+ * An interesting one. A type taking two imbricated sets and returning the
6
+ * smallest one.
7
+ * We need that because sometimes the pattern's inferred type holds more
8
+ * information than the value on which we are matching (if the value is any
9
+ * or unknown for instance).
10
+ */
11
+ export type LeastUpperBound<a, b> = b extends a ? b : a extends b ? a : never;
12
+ export type Contains<a, b> = a extends any ? 'exclude' extends {
13
+ [k in keyof a]-?: Equal<a[k], b> extends true ? 'exclude' : 'include';
14
+ }[keyof a] ? true : false : never;
15
+ export type UnionToIntersection<union> = (union extends any ? (k: union) => void : never) extends (k: infer intersection) => void ? intersection : never;
16
+ export type IsUnion<a> = [a] extends [UnionToIntersection<a>] ? false : true;
17
+ export type UnionToTuple<union, output extends any[] = []> = UnionToIntersection<union extends any ? (t: union) => union : never> extends (_: any) => infer elem ? UnionToTuple<Exclude<union, elem>, [elem, ...output]> : output;
18
+ export type Flatten<xs extends readonly any[], output extends any[] = []> = xs extends readonly [infer head, ...infer tail] ? Flatten<tail, [...output, ...Extract<head, readonly any[]>]> : output;
19
+ export type Equal<a, b> = (<T>() => T extends a ? 1 : 2) extends <T>() => T extends b ? 1 : 2 ? true : false;
20
+ export type Expect<a extends true> = a;
21
+ export type IsAny<a> = 0 extends 1 & a ? true : false;
22
+ export type IsNever<T> = [T] extends [never] ? true : false;
23
+ export type Length<it extends readonly any[]> = it['length'];
24
+ export type Iterator<n extends number, it extends any[] = []> = it['length'] extends n ? it : Iterator<n, [any, ...it]>;
25
+ export type Next<it extends any[]> = [any, ...it];
26
+ export type Prev<it extends any[]> = it extends readonly [any, ...infer tail] ? tail : [];
27
+ export type Take<xs extends readonly any[], it extends any[], output extends any[] = []> = Length<it> extends 0 ? output : xs extends readonly [infer head, ...infer tail] ? Take<tail, Prev<it>, [...output, head]> : output;
28
+ export type Drop<xs extends readonly any[], n extends any[]> = Length<n> extends 0 ? xs : xs extends readonly [any, ...infer tail] ? Drop<tail, Prev<n>> : [];
29
+ export type UpdateAt<tail extends readonly any[], n extends any[], value, inits extends readonly any[] = []> = Length<n> extends 0 ? tail extends readonly [any, ...infer tail] ? [...inits, value, ...tail] : inits : tail extends readonly [infer head, ...infer tail] ? UpdateAt<tail, Prev<n>, value, [...inits, head]> : inits;
30
+ export type BuiltInObjects = Function | Date | RegExp | Generator | {
31
+ readonly [Symbol.toStringTag]: string;
32
+ } | any[];
33
+ export type IsPlainObject<o, excludeUnion = BuiltInObjects> = o extends object ? o extends string | excludeUnion ? false : true : false;
34
+ export type Compute<a extends any> = a extends BuiltInObjects ? a : {
35
+ [k in keyof a]: a[k];
36
+ };
37
+ export type IntersectObjects<a> = (a extends any ? keyof a : never) extends infer allKeys ? {
38
+ [k in Extract<allKeys, PropertyKey>]: a extends any ? k extends keyof a ? a[k] : never : never;
39
+ } : never;
40
+ export type WithDefault<a, def> = [a] extends [never] ? def : a;
41
+ export type IsLiteral<a> = [a] extends [null | undefined] ? true : [a] extends [string] ? string extends a ? false : true : [a] extends [number] ? number extends a ? false : true : [a] extends [boolean] ? boolean extends a ? false : true : [a] extends [symbol] ? symbol extends a ? false : true : [a] extends [bigint] ? bigint extends a ? false : true : false;
42
+ export type Primitives = number | boolean | string | undefined | null | symbol | bigint;
43
+ export type NonLiteralPrimitive = Exclude<Primitives, undefined | null>;
44
+ export type TupleKeys = '0' | '1' | '2' | '3' | '4';
45
+ export type Union<a, b> = [b] extends [a] ? a : [a] extends [b] ? b : a | b;
46
+ /**
47
+ * GuardValue returns the value guarded by a type guard function.
48
+ */
49
+ export type GuardValue<fn> = fn extends (value: any) => value is infer b ? b : fn extends (value: infer a) => unknown ? a : never;
50
+ export type GuardFunction<input, narrowed> = ((value: input) => value is Extract<narrowed, input>) | ((value: input) => boolean);
51
+ export type Some<bools extends boolean[]> = true extends bools[number] ? true : false;
52
+ export type All<bools extends boolean[]> = bools[number] extends true ? true : false;
53
+ export type Extends<a, b> = [a] extends [b] ? true : false;
54
+ export type Not<a extends boolean> = a extends true ? false : true;
55
+ type AllKeys<a> = a extends any ? keyof a : never;
56
+ export type MergeUnion<a> = {
57
+ [k in AllKeys<a>]: a extends any ? k extends keyof a ? a[k] : never : never;
58
+ } | never;
59
+ export type IsFixedSizeTuple<a extends readonly any[]> = IsLiteral<Length<a>>;
60
+ export type IsTuple<a> = a extends readonly [] | readonly [any, ...any] | readonly [...any, any] ? true : false;
61
+ export type IsStrictArray<a extends readonly any[]> = Not<IsTuple<a>>;
62
+ export type IsReadonlyArray<a> = a extends readonly any[] ? a extends any[] ? false : true : false;
63
+ export type MaybeAddReadonly<a, shouldAdd extends boolean> = shouldAdd extends true ? Readonly<a> : a;
64
+ export type MapKey<T> = T extends Map<infer K, any> ? K : never;
65
+ export type MapValue<T> = T extends Map<any, infer V> ? V : never;
66
+ export type SetValue<T> = T extends Set<infer V> ? V : never;
67
+ export type RecordKey<T> = T extends Record<infer K, any> ? K : never;
68
+ export type RecordValue<T> = T extends Record<any, infer V> ? V : never;
69
+ export type ReadonlyArrayValue<T> = T extends ReadonlyArray<infer V> ? V : never;
70
+ export type ExtractPlainObject<T> = T extends any ? IsPlainObject<T> extends true ? T : never : never;
71
+ export type GetKey<O, K> = O extends any ? K extends keyof O ? O[K] : never : never;
72
+ export interface Fn {
73
+ input: unknown;
74
+ output: unknown;
75
+ }
76
+ export type Call<fn extends Fn, input> = (fn & {
77
+ input: input;
78
+ })['output'];
79
+ export type IsOptionalKeysOf<obj, key extends keyof obj> = {} extends Pick<obj, key> ? true : false;
80
+ export {};
@@ -0,0 +1,80 @@
1
+ export type ValueOf<a> = a extends readonly any[] ? a[number] : a[keyof a];
2
+ export type Values<a extends object> = UnionToTuple<ValueOf<a>>;
3
+ /**
4
+ * ### LeastUpperBound
5
+ * An interesting one. A type taking two imbricated sets and returning the
6
+ * smallest one.
7
+ * We need that because sometimes the pattern's inferred type holds more
8
+ * information than the value on which we are matching (if the value is any
9
+ * or unknown for instance).
10
+ */
11
+ export type LeastUpperBound<a, b> = b extends a ? b : a extends b ? a : never;
12
+ export type Contains<a, b> = a extends any ? 'exclude' extends {
13
+ [k in keyof a]-?: Equal<a[k], b> extends true ? 'exclude' : 'include';
14
+ }[keyof a] ? true : false : never;
15
+ export type UnionToIntersection<union> = (union extends any ? (k: union) => void : never) extends (k: infer intersection) => void ? intersection : never;
16
+ export type IsUnion<a> = [a] extends [UnionToIntersection<a>] ? false : true;
17
+ export type UnionToTuple<union, output extends any[] = []> = UnionToIntersection<union extends any ? (t: union) => union : never> extends (_: any) => infer elem ? UnionToTuple<Exclude<union, elem>, [elem, ...output]> : output;
18
+ export type Flatten<xs extends readonly any[], output extends any[] = []> = xs extends readonly [infer head, ...infer tail] ? Flatten<tail, [...output, ...Extract<head, readonly any[]>]> : output;
19
+ export type Equal<a, b> = (<T>() => T extends a ? 1 : 2) extends <T>() => T extends b ? 1 : 2 ? true : false;
20
+ export type Expect<a extends true> = a;
21
+ export type IsAny<a> = 0 extends 1 & a ? true : false;
22
+ export type IsNever<T> = [T] extends [never] ? true : false;
23
+ export type Length<it extends readonly any[]> = it['length'];
24
+ export type Iterator<n extends number, it extends any[] = []> = it['length'] extends n ? it : Iterator<n, [any, ...it]>;
25
+ export type Next<it extends any[]> = [any, ...it];
26
+ export type Prev<it extends any[]> = it extends readonly [any, ...infer tail] ? tail : [];
27
+ export type Take<xs extends readonly any[], it extends any[], output extends any[] = []> = Length<it> extends 0 ? output : xs extends readonly [infer head, ...infer tail] ? Take<tail, Prev<it>, [...output, head]> : output;
28
+ export type Drop<xs extends readonly any[], n extends any[]> = Length<n> extends 0 ? xs : xs extends readonly [any, ...infer tail] ? Drop<tail, Prev<n>> : [];
29
+ export type UpdateAt<tail extends readonly any[], n extends any[], value, inits extends readonly any[] = []> = Length<n> extends 0 ? tail extends readonly [any, ...infer tail] ? [...inits, value, ...tail] : inits : tail extends readonly [infer head, ...infer tail] ? UpdateAt<tail, Prev<n>, value, [...inits, head]> : inits;
30
+ export type BuiltInObjects = Function | Date | RegExp | Generator | {
31
+ readonly [Symbol.toStringTag]: string;
32
+ } | any[];
33
+ export type IsPlainObject<o, excludeUnion = BuiltInObjects> = o extends object ? o extends string | excludeUnion ? false : true : false;
34
+ export type Compute<a extends any> = a extends BuiltInObjects ? a : {
35
+ [k in keyof a]: a[k];
36
+ };
37
+ export type IntersectObjects<a> = (a extends any ? keyof a : never) extends infer allKeys ? {
38
+ [k in Extract<allKeys, PropertyKey>]: a extends any ? k extends keyof a ? a[k] : never : never;
39
+ } : never;
40
+ export type WithDefault<a, def> = [a] extends [never] ? def : a;
41
+ export type IsLiteral<a> = [a] extends [null | undefined] ? true : [a] extends [string] ? string extends a ? false : true : [a] extends [number] ? number extends a ? false : true : [a] extends [boolean] ? boolean extends a ? false : true : [a] extends [symbol] ? symbol extends a ? false : true : [a] extends [bigint] ? bigint extends a ? false : true : false;
42
+ export type Primitives = number | boolean | string | undefined | null | symbol | bigint;
43
+ export type NonLiteralPrimitive = Exclude<Primitives, undefined | null>;
44
+ export type TupleKeys = '0' | '1' | '2' | '3' | '4';
45
+ export type Union<a, b> = [b] extends [a] ? a : [a] extends [b] ? b : a | b;
46
+ /**
47
+ * GuardValue returns the value guarded by a type guard function.
48
+ */
49
+ export type GuardValue<fn> = fn extends (value: any) => value is infer b ? b : fn extends (value: infer a) => unknown ? a : never;
50
+ export type GuardFunction<input, narrowed> = ((value: input) => value is Extract<narrowed, input>) | ((value: input) => boolean);
51
+ export type Some<bools extends boolean[]> = true extends bools[number] ? true : false;
52
+ export type All<bools extends boolean[]> = bools[number] extends true ? true : false;
53
+ export type Extends<a, b> = [a] extends [b] ? true : false;
54
+ export type Not<a extends boolean> = a extends true ? false : true;
55
+ type AllKeys<a> = a extends any ? keyof a : never;
56
+ export type MergeUnion<a> = {
57
+ [k in AllKeys<a>]: a extends any ? k extends keyof a ? a[k] : never : never;
58
+ } | never;
59
+ export type IsFixedSizeTuple<a extends readonly any[]> = IsLiteral<Length<a>>;
60
+ export type IsTuple<a> = a extends readonly [] | readonly [any, ...any] | readonly [...any, any] ? true : false;
61
+ export type IsStrictArray<a extends readonly any[]> = Not<IsTuple<a>>;
62
+ export type IsReadonlyArray<a> = a extends readonly any[] ? a extends any[] ? false : true : false;
63
+ export type MaybeAddReadonly<a, shouldAdd extends boolean> = shouldAdd extends true ? Readonly<a> : a;
64
+ export type MapKey<T> = T extends Map<infer K, any> ? K : never;
65
+ export type MapValue<T> = T extends Map<any, infer V> ? V : never;
66
+ export type SetValue<T> = T extends Set<infer V> ? V : never;
67
+ export type RecordKey<T> = T extends Record<infer K, any> ? K : never;
68
+ export type RecordValue<T> = T extends Record<any, infer V> ? V : never;
69
+ export type ReadonlyArrayValue<T> = T extends ReadonlyArray<infer V> ? V : never;
70
+ export type ExtractPlainObject<T> = T extends any ? IsPlainObject<T> extends true ? T : never : never;
71
+ export type GetKey<O, K> = O extends any ? K extends keyof O ? O[K] : never : never;
72
+ export interface Fn {
73
+ input: unknown;
74
+ output: unknown;
75
+ }
76
+ export type Call<fn extends Fn, input> = (fn & {
77
+ input: input;
78
+ })['output'];
79
+ export type IsOptionalKeysOf<obj, key extends keyof obj> = {} extends Pick<obj, key> ? true : false;
80
+ export {};
@@ -0,0 +1 @@
1
+ export * from './Pattern.cjs';
@@ -0,0 +1 @@
1
+ export * from './Pattern.js';