-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
303 lines (279 loc) · 10.8 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* core/match -- a pattern matching library for JavaScript and TypeScript
*
* Copyright (c) 2024 TANIGUCHI Masaya. All rights reserved.
* This code is licensed under the MIT License. See LICENSE file for more information.
*
* SPDX-License-Identifier: MIT
*/
import type { U, A } from "npm:ts-toolbelt@9.6.0";
import type { Predicate } from "jsr:@core/unknownutil@3.18.0";
import { is } from "jsr:@core/unknownutil@3.18.0";
type Key = string | number | symbol;
type Entries<Obj, Keys = U.ListOf<keyof Obj>, Acc extends ([keyof Obj, Obj[keyof Obj]])[] = []> =
Keys extends [infer Key extends keyof Obj, ...infer Rest extends (keyof Obj)[]] ? Entries<Obj, Rest, [[Key, Obj[Key]], ...Acc]> : Acc;
export class Placeholder {}
/**
* AnonymousPlaceholder class that holds a test function.
*
* @template V - Type guard function.
*/
export class AnonymousPlaceholder<V extends Predicate<unknown> = Predicate<unknown>> extends Placeholder {
/** The test function to validate the placeholder value. */
test?: V;
/**
* Represents a class constructor
* @param [test] - The test parameter (optional).
*/
constructor(test?: V) {
super();
this.test = test;
}
}
/**
* RegularPlaceholder class that holds a name and a test function.
*
* @template T - Name of the placeholder.
* @template V - Type guard function.
*/
export class RegularPlaceholder<T extends Key, V extends Predicate<unknown> = Predicate<unknown>> extends Placeholder {
/** The name of the placeholder. */
name: T;
/** The test function to validate the placeholder value. */
test?: V;
/**
* Represents a class constructor.
* @param name - The name parameter.
* @param [test] - The test parameter (optional).
*/
constructor(name: T, test?: V) {
super();
this.name = name;
this.test = test;
}
}
/**
* Represents a template string placeholder with optional regular placeholders.
* @template T - Tuple of regular placeholders.
*/
export class TemplateStringPlaceholder<T extends Placeholder[]> extends Placeholder {
/** The template strings array. */
strings: TemplateStringsArray;
/** The regular placeholders. */
placeholders: T;
/** Indicates whether the matching should be greedy or not. */
greedy: boolean;
/**
* Creates a new instance of TemplateStringPlaceholder.
* @param greedy - Indicates whether the matching should be greedy or not.
* @param strings - The template strings array.
* @param placeholders - The regular placeholders.
*/
constructor(greedy: boolean, strings: TemplateStringsArray, ...placeholders: T) {
super();
this.greedy = greedy;
this.strings = strings;
this.placeholders = placeholders;
}
}
function _placeholder<V extends Predicate<unknown> = Predicate<unknown>>(test?: V): AnonymousPlaceholder<V>;
function _placeholder<T extends Key, V extends Predicate<unknown> = Predicate<unknown>>(name: T, test?: V): RegularPlaceholder<T, V>;
function _placeholder<T extends Placeholder[]>(strings: TemplateStringsArray, ...placeholders: T): TemplateStringPlaceholder<T>;
function _placeholder<T extends Key, U extends RegularPlaceholder<Key>[], V extends Predicate<unknown> = Predicate<unknown>>(
nameOrStringsOrTest?: T | TemplateStringsArray | V,
...testOrPlaceholders: [] | [V] | U
): RegularPlaceholder<T, V> | TemplateStringPlaceholder<U> | AnonymousPlaceholder<V> {
const isAbstractPlaceholders =
(v: unknown[]): v is U => is.ArrayOf(is.InstanceOf(Placeholder))(v);
const isKey = is.UnionOf([is.String, is.Number, is.Symbol]);
const maybeTypeGuard =
(v: unknown): v is V | undefined => is.UnionOf([is.Function, is.Undefined])(v);
const isTemplateStringsArray =
(v: unknown): v is TemplateStringsArray => is.ArrayOf(is.String)(v);
if (maybeTypeGuard(nameOrStringsOrTest)) {
return new AnonymousPlaceholder(nameOrStringsOrTest);
} else if (isKey(nameOrStringsOrTest)) {
if (maybeTypeGuard(testOrPlaceholders[0])) {
return new RegularPlaceholder(nameOrStringsOrTest, testOrPlaceholders[0]);
}
} else if (isTemplateStringsArray(nameOrStringsOrTest)) {
if (isAbstractPlaceholders(testOrPlaceholders)) {
return new TemplateStringPlaceholder(false, nameOrStringsOrTest, ...testOrPlaceholders);
}
}
throw new Error('Invalid arguments');
}
function greedy<T extends Placeholder[]>(strings: TemplateStringsArray, ...placeholders: T): TemplateStringPlaceholder<T> {
return new TemplateStringPlaceholder(true, strings, ...placeholders);
}
_placeholder.greedy = greedy;
/**
* Represents a placeholder that can be used in pattern matching.
*/
export interface PlaceholderFactory {
/**
* Creates an anonymous placeholder with an optional test function.
*
* @param test - The test function to validate the placeholder value.
* @returns An anonymous placeholder with the specified test function.
*/
<V extends Predicate<unknown> = Predicate<unknown>>(test?: V): AnonymousPlaceholder<V>;
/**
* Creates a regular placeholder with an optional name and test function.
*
* @param name - The name of the placeholder.
* @param test - The test function to validate the placeholder value.
* @returns A regular placeholder with the specified name and test function.
*/
<T extends Key, V extends Predicate<unknown> = Predicate<unknown>>(name: T, test?: V): RegularPlaceholder<T, V>;
/**
* Creates a template string placeholder with multiple regular placeholders.
*
* @param strings - The template strings array.
* @param placeholders - The regular placeholders to be used in the template string.
* @returns A template string placeholder with the specified regular placeholders.
*/
<T extends Placeholder[]>(strings: TemplateStringsArray, ...placeholders: T): TemplateStringPlaceholder<T>;
/**
* Creates a template string placeholder with greedy matching.
*
* @param strings - The template strings array.
* @param placeholders - The regular placeholders to be used in the template string.
* @returns A template string placeholder with the specified regular placeholders.
*/
greedy<T extends Placeholder[]>(strings: TemplateStringsArray, ...placeholders: T): TemplateStringPlaceholder<T>;
}
/**
* A placeholder constant.
*/
export const placeholder: PlaceholderFactory = _placeholder;
/**
* Result type is a recursive type that represents the result of `match` function.
* This type is a record of keys and values that are matched.
* All the keys are declared as placeholders in `P` and the values are the types of the matched values.
*
* @template P - The pattern to match against.
*/
export type Match<P> =
P extends RegularPlaceholder<infer V, Predicate<infer U>> ?
{ [v in V]: U } :
P extends TemplateStringPlaceholder<infer T> ?
MatchTemplateString<T> :
P extends AnonymousPlaceholder ?
never :
P extends Array<infer A> ?
MatchArrayOrRecord<U.ListOf<A>> :
P extends Record<Key, infer V> ?
MatchArrayOrRecord<U.ListOf<V>> :
never;
type MatchTemplateString<P, Acc extends Record<Key, unknown> = never> =
P extends [RegularPlaceholder<infer V, Predicate<infer U>>, ...infer Others] ?
A.Equals<U, unknown> extends 1 ?
MatchTemplateString<Others, Acc | Match<RegularPlaceholder<V, Predicate<string>>>> :
MatchTemplateString<Others, Acc | Match<RegularPlaceholder<V, Predicate<U>>>> :
P extends [infer T, ...infer Others] ?
MatchTemplateString<Others, Acc | Match<T>> :
U.Merge<Acc>;
type MatchArrayOrRecord<P, Acc extends Record<Key, unknown> = never> =
P extends [infer V, ...infer Others] ?
MatchArrayOrRecord<Others, Acc | Match<V>> :
U.Merge<Acc>;
/**
* Returns a type that represents the expected type of the pattern.
* Note that this type is experimental and no gurarantee is provided.
* @template P - The pattern to match against.
* @returns The expected type of the pattern.
**/
export type Expected<P> =
P extends RegularPlaceholder<infer _V, Predicate<infer U>> ?
U :
P extends TemplateStringPlaceholder<infer _T> ?
string :
P extends AnonymousPlaceholder ?
unknown :
P extends Array<infer A> ?
ExpectedArray<U.ListOf<A>> :
P extends Record<Key, unknown> ?
ExpectedRecord<Entries<P>> :
P;
type ExpectedArray<P, Acc extends unknown[] = []> =
P extends [infer V, ...infer Rest] ?
ExpectedArray<Rest, [...Acc, Expected<V>]> :
Acc;
type ExpectedRecord<P, Acc extends Record<Key, unknown> = never> =
P extends [[infer K extends Key, infer V], ...infer Rest] ?
ExpectedRecord<Rest, { [k in K]: Expected<V> } | Acc> :
U.Merge<Acc>;
/**
* Matches a pattern against a target object.
* @param pattern - The pattern to match against.
* @param target - The target object to match.
* @returns The result of the match or undefined if there is no match.
*/
export function match<T>(pattern: T, target: unknown): Match<T> | undefined {
if (is.InstanceOf(AnonymousPlaceholder)(pattern)) {
if (!pattern.test || pattern.test(target)) {
return {} as Match<T>;
}
return undefined;
}
if (is.InstanceOf(RegularPlaceholder)(pattern)) {
if (!pattern.test || pattern.test(target)) {
return { [pattern.name]: target } as Match<T>;
}
return undefined;
}
if (is.InstanceOf(TemplateStringPlaceholder<any[]>)(pattern)) {
if (typeof target !== 'string') {
return undefined;
}
const sep = pattern.greedy ? '(.*)' : '(.*?)';
const esc = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`^${pattern.strings.map(esc).join(sep)}$`);
const m = target.match(re);
let result: Match<T> | undefined;
for (let i = 0; m && i < pattern.placeholders.length; i++) {
const subResult = match(pattern.placeholders[i], m[i+1]);
if (subResult) {
result = { ...(result ?? {} as Match<T>), ...subResult };
continue;
}
return undefined;
}
return result;
}
if (is.ArrayOf(is.Any)(pattern)) {
if (!(is.Array(target) && pattern.length <= target.length)) {
return undefined;
}
const result = {} as Match<T>;
for (let i = 0; i < pattern.length; i++) {
const subResult = match(pattern[i], target[i]);
if (subResult) {
Object.assign(result, subResult);
continue;
}
return undefined;
}
return result;
}
// pattern should be a direct instance of Object
if (pattern instanceof Object && Object.getPrototypeOf(pattern) === Object.prototype && is.Record(target)) {
const result = {} as Match<T>;
for (const [key, value] of Object.entries(pattern)) {
if (key in target) {
const subResult = match(value, target[key]);
if (subResult) {
Object.assign(result, subResult);
continue;
}
}
return undefined;
}
return result;
}
if (pattern === target) {
return {} as Match<T>;
}
return undefined;
}