js-dev-tool 1.1.3 → 1.2.0

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.
@@ -1,385 +0,0 @@
1
- /*!
2
- // Copyright jeffy-g 2025
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
8
- // http://www.apache.org/licenses/LICENSE-2.0
9
- //
10
- // Unless required by applicable law or agreed to in writing, software
11
- // distributed under the License is distributed on an "AS IS" BASIS,
12
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- // See the License for the specific language governing permissions and
14
- // limitations under the License.
15
- */
16
- /**
17
- * @file literate-regex/index.d.ts
18
- * @desc It is recommended to add `"skipLibCheck": true` to your tsconfig.
19
- * @todo publish as "literate-regex"
20
- * @todo implement runtime utility function for `/<source>/<flags>`
21
- */
22
- /*!
23
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24
- // Basics
25
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26
- */
27
- export type Brand<Tag extends string> = {
28
- [k in Tag]: true;
29
- };
30
- export type TypedRegExpBrand = Brand<"__TypedRegExp">;
31
- export type TypedRegExp<P extends string, F extends string = ""> = RegExp & {
32
- readonly source: P;
33
- readonly flags: F;
34
- } & TypedRegExpBrand;
35
- /**
36
- * Extracts the literal type from the source property of a RegExp.
37
- * @template R - A RegExp type.
38
- */
39
- export type RegExpSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
40
- export declare function createRegExp<
41
- const P extends string,
42
- const F extends string = ""
43
- >(pattern: P, flags?: F): TypedRegExp<P, F>;
44
- type IsEmptyRecord<T> =
45
- [T] extends [undefined] ? true :
46
- [keyof T] extends [never] ? true : false;
47
- /*!
48
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
- // Named Capture Groups
50
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51
- */
52
- type FirstChar<S extends string> = S extends `${infer F}${infer _}` ? F : never;
53
- /**
54
- * Recursively extracts parts that match the format (?<GroupName>...) from a string pattern,
55
- * without considering nesting, and unions them.
56
- * @template S - A string pattern.
57
- */
58
- export type ExtractGroupNames<S extends unknown> =
59
- S extends `${infer _Before}(?<${infer Rest}`
60
- ? FirstChar<Rest> extends "=" | "!"
61
- ? ExtractGroupNames<Rest>
62
- : Rest extends `${infer GroupName}>${infer After}`
63
- ? GroupName extends ""
64
- ? "ExtractGroupNames: regex pattern error!"
65
- : GroupName | ExtractGroupNames<After>
66
- : never
67
- : never;
68
- /**
69
- * Creates an object type with keys as the extracted group names and values as strings.
70
- * If no groups are found, it results in an empty object.
71
- * @template R - A RegExp type.
72
- * @date 2025/12/24 14:46:30 - It may be possible to extract the group name accurately (?)
73
- */
74
- export type RegExpNamedGroups<R extends RegExp> =
75
- R extends RegExp & { readonly source: infer S extends string }
76
- ? ExtractGroupNames<S> extends infer K
77
- ? [K] extends [never]
78
- ? undefined
79
- : { [P in K & string]: string | undefined }
80
- : undefined
81
- : undefined;
82
- /*!
83
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84
- // Capture Groups
85
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
- */
87
- /**
88
- * Preprocesses escape sequences in a string pattern by replacing escaped backslashes and characters.
89
- *
90
- * + NOTE: This type is used to preprocess the pattern string before counting capture groups.
91
- *
92
- * @template S - A string pattern.
93
- * @template Result - The resulting string after preprocessing.
94
- * @internal
95
- */
96
- export type __PreprocessEscapes<S extends string, Result extends string = ""> =
97
- S extends `\\\\${infer Rest}`
98
- ? __PreprocessEscapes<Rest, `${Result}__`>
99
- : S extends `\\${infer EscapedChar}${infer Rest}`
100
- ? __PreprocessEscapes<Rest, `${Result}_!`>
101
- : S extends `${infer Char}${infer Rest}`
102
- ? __PreprocessEscapes<Rest, `${Result}${Char}`>
103
- : Result;
104
- /**
105
- * Counts the exact number of capture groups in a string pattern.
106
- * @template S - A string pattern.
107
- * @template Counter - An array used to count the capture groups.
108
- */
109
- export type CountCaptureGroups<
110
- S extends string,
111
- Counter extends unknown[] = []
112
- > =
113
- __PreprocessEscapes<S> extends `${infer _Before}(${infer Rest}`
114
- ? Rest extends `${"?:" | "?=" | "?!" | "?<=" | "?<!"}${infer After}`
115
- ? CountCaptureGroups<After, Counter>
116
- : CountCaptureGroups<Rest, [...Counter, unknown]>
117
- : Counter["length"];
118
- /**
119
- * Represents a fixed version of RegExpExecArray that includes the matched string,
120
- * captures, and optionally named groups.
121
- * @template R - A RegExp type.
122
- * @template S - The source string of the RegExp.
123
- * @template GroupCount - The number of capture groups.
124
- */
125
- export type RegExpExecArrayFixed<
126
- R extends RegExp,
127
- S extends RegExpSource<R> = RegExpSource<R>,
128
- GroupCount extends number = CountCaptureGroups<S>,
129
- NamedGroups = RegExpNamedGroups<R>
130
- > = [match: string, ...BuildCaptureTuple<GroupCount, string | undefined>] & {
131
- groups: IsEmptyRecord<NamedGroups> extends true ? undefined : NamedGroups;
132
- index: number;
133
- input: string;
134
- };
135
- type CaptureOptTuple<N extends number, T = string | undefined> =
136
- N extends 0 ? [] :
137
- N extends 1 ? [$1: T] :
138
- N extends 2 ? [$1: T, $2: T] :
139
- N extends 3 ? [$1: T, $2: T, $3: T] :
140
- N extends 4 ? [$1: T, $2: T, $3: T, $4: T] :
141
- N extends 5 ? [$1: T, $2: T, $3: T, $4: T, $5: T] :
142
- N extends 6 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T] :
143
- N extends 7 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T] :
144
- N extends 8 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T] :
145
- N extends 9 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T] :
146
- N extends 10 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T] :
147
- N extends 11 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T] :
148
- N extends 12 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T] :
149
- N extends 13 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T] :
150
- N extends 14 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T] :
151
- N extends 15 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T, $15: T] :
152
- N extends 16 ? [$1: T, $2: T, $3: T, $4: T, $5: T, $6: T, $7: T, $8: T, $9: T, $10: T, $11: T, $12: T, $13: T, $14: T, $15: T, $16: T] :
153
- BuildCaptureTuple<N, T>;
154
- export type RegExpExecArrayFixedPretty<
155
- R extends RegExp,
156
- S extends RegExpSource<R> = RegExpSource<R>,
157
- GroupCount extends number = CountCaptureGroups<S>,
158
- NamedGroups = RegExpNamedGroups<R>
159
- > = [match: string, ...CaptureOptTuple<GroupCount>] & {
160
- groups: IsEmptyRecord<NamedGroups> extends true ? undefined : NamedGroups;
161
- index: number;
162
- input: string;
163
- };
164
- export type TupleOf<
165
- Count extends number, ArrayType,
166
- Result extends ArrayType[] = []
167
- > = Result["length"] extends Count
168
- ? Result
169
- : TupleOf<Count, ArrayType, [...Result, ArrayType]>;
170
- /**
171
- * Builds a tuple type whose length equals the number of capture groups.
172
- * @template Count - Number of capture groups.
173
- * @template Result - Accumulator (internal).
174
- */
175
- export type BuildCaptureTuple<
176
- Count extends number, ArrayType = string,
177
- > = TupleOf<Count, ArrayType>;
178
- /*!
179
- // ============================================================================
180
- // Helper Types for Replacer
181
- // ============================================================================
182
- */
183
- /**
184
- * Creates the parameter types for String.replace callback function.
185
- *
186
- * @example
187
- * type Params = ReplaceCallbackParams<typeof myRegex>;
188
- * // [match: string, ...captures: string[], offset: number, string: string, groups?: {...}]
189
- */
190
- export type ReplaceCallbackParams<
191
- R extends RegExp,
192
- S extends RegExpSource<R> = RegExpSource<R>,
193
- GroupCount extends number = CountCaptureGroups<S>,
194
- NamedGroups = RegExpNamedGroups<R>
195
- > = IsEmptyRecord<NamedGroups> extends true ? [
196
- match: string,
197
- ...captures: CaptureOptTuple<GroupCount>,
198
- offset: number,
199
- input: string,
200
- ] : [
201
- match: string,
202
- ...captures: CaptureOptTuple<GroupCount>,
203
- offset: number,
204
- input: string,
205
- groups: NamedGroups
206
- ];
207
- export type ReplacerFunctionSignature<R extends unknown> =
208
- R extends (
209
- RegExp & { readonly source: infer T }
210
- ) ? R extends TypedRegExpBrand
211
- ? (...args: ReplaceCallbackParams<R/* , T */>) => string
212
- : (match: string, ...rest: any[]) => string
213
- : string;
214
- /*! [2025/12/31 13:01:03]
215
- * ## Support PCRE-style regex source (type-level)
216
- *
217
- * @remarks
218
- * This module provides **type-level normalization** for "PCRE-ish" regex sources:
219
- * you can write a readable multi-line regex with indentation and `# ...` comments,
220
- * then derive a compact JavaScript `RegExp` source at compile time.
221
- *
222
- * Why type-level? Because it lets us:
223
- * - keep the regex readable in source code (with comments and formatting)
224
- * - ensure the normalized output is stable and verifiable (via type assertions)
225
- *
226
- * ## Supported rules
227
- * - `#` starts a comment and consumes the rest of the **current line**
228
- * - `\#` escapes `#` as a literal (comment is NOT started) and normalizes to `#`
229
- * - whitespace characters are removed during normalization
230
- * - newlines are normalized: `\r\n` and `\r` become `\n`
231
- *
232
- * ## Design note (ts(2589) survival)
233
- * Naively scanning the entire string character-by-character (multiple passes)
234
- * can hit TypeScript's instantiation depth limit (`ts(2589)`).
235
- * This implementation is **line-oriented**:
236
- * it strips each line, then concatenates results.
237
- * That tends to scale much better for long, commented regex sources.
238
- *
239
- * ## Non-goals (for now)
240
- * This is not a full PCRE parser. It intentionally focuses on comment + whitespace
241
- * normalization for readability and DX.
242
- */
243
- /**
244
- * Union of whitespace characters used by this library.
245
- *
246
- * + A set aligned with ECMAScript RegExp **`\s`** (WhiteSpace ∪ LineTerminator)
247
- *
248
- * The union is used to normalize "PCRE-style" regex sources at the type level.
249
- */
250
- export type TWhiteSpace =
251
- | "\x09"
252
- | "\x0A"
253
- | "\x0B"
254
- | "\x0C"
255
- | "\x0D"
256
- | "\x20"
257
- | "\u00A0"
258
- | "\u1680"
259
- | "\u2000"
260
- | "\u2001"
261
- | "\u2002"
262
- | "\u2003"
263
- | "\u2004"
264
- | "\u2005"
265
- | "\u2006"
266
- | "\u2007"
267
- | "\u2008"
268
- | "\u2009"
269
- | "\u200A"
270
- | "\u2028" | "\u2029"
271
- | "\u202F" | "\u205F" | "\u3000"
272
- | "\uFEFF";
273
- /**
274
- * Normalize newline sequences for line-based parsing.
275
- *
276
- * + Align **`\r\n`** and **`\r`** to **`\n`** (makes splitting easier)
277
- *
278
- * @template S - Input string literal.
279
- * @returns A new string literal with `\r\n` and `\r` normalized to `\n`.
280
- *
281
- * @example
282
- * type X = NormalizeNewlines<"a\r\nb\rc\n">; // "a\nb\nc\n"
283
- */
284
- type NormalizeNewlines<S extends string> =
285
- S extends `${infer A}\r\n${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
286
- S extends `${infer A}\r${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
287
- S;
288
- /**
289
- * Strip and normalize a single line of a PCRE-style regex source.
290
- *
291
- * @remarks
292
- * Rules:
293
- * - `\#` is treated as a literal `#` (the backslash is dropped)
294
- * - an unescaped `#` starts a line comment and truncates the rest
295
- * - all whitespace characters are removed
296
- * - other escapes like `\s`, `\w`, `\/`, `\*` are preserved as-is
297
- *
298
- * @template S - One line of source (no `\n`).
299
- * @template Acc - Accumulator for the normalized output.
300
- *
301
- * @example
302
- * type A = StripLine<" \\#\\w+ # comment ">; // "#\\w+"
303
- * type B = StripLine<" abc # comment">; // "abc"
304
- *
305
- * @internal
306
- *
307
- * @todo
308
- * - Handle `\\#` (a literal backslash + literal #) more strictly.
309
- * - Do not treat `#` as a comment starter inside character classes (`[...]`).
310
- */
311
- type StripLine<S extends string, Acc extends string = ""> =
312
- S extends `\\#${infer Rest}`
313
- ? StripLine<Rest, `${Acc}#`>
314
- : S extends `\\${infer C}${infer Rest}`
315
- ? StripLine<Rest, `${Acc}\\${C}`>
316
- : S extends `#${string}`
317
- ? Acc
318
- : S extends `${infer C}${infer Rest}`
319
- ? C extends TWhiteSpace
320
- ? StripLine<Rest, Acc>
321
- : StripLine<Rest, `${Acc}${C}`>
322
- : Acc;
323
- /**
324
- * Convert a multi-line PCRE-style regex source into a compact JS `RegExp.source`.
325
- *
326
- * @remarks
327
- * This type:
328
- * 1) normalizes newlines, 2) splits by line, 3) applies {@link StripLine},
329
- * then 4) concatenates the results.
330
- *
331
- * @template Input - A `const` string literal of the PCRE-ish source.
332
- * @template Acc - Accumulator for the result.
333
- * @template S - Internal normalized newline buffer (do not pass manually).
334
- *
335
- * @example
336
- * const RE_SOURCE = `
337
- * ^ # start
338
- * (?:\\#\\w+) # keep literal "#"
339
- * ` as const;
340
- *
341
- * type Src = PCREStyleToJsRegExpSource<typeof RE_SOURCE>;
342
- * // "^(:?##\\w+)" ... (depending on your actual input)
343
- */
344
- export type PCREStyleToJsRegExpSource<
345
- Input extends string,
346
- Acc extends string = "",
347
- S extends string = NormalizeNewlines<Input>,
348
- > =
349
- S extends `${infer Line}\n${infer Rest}`
350
- ? PCREStyleToJsRegExpSource<Input, `${Acc}${StripLine<Line>}`, Rest>
351
- : `${Acc}${StripLine<S>}`;
352
- /*!
353
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
354
- // Parse PCRE Style Regex Literal
355
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
356
- */
357
- type AllowedRegExpFlag = "d" | "g" | "i" | "m" | "s" | "u" | "v" | "y";
358
- type StripFlagsFromEnd<S extends string, Acc extends string = ""> =
359
- S extends `${infer Rest}y` ? StripFlagsFromEnd<Rest, `y${Acc}`> :
360
- S extends `${infer Rest}v` ? StripFlagsFromEnd<Rest, `v${Acc}`> :
361
- S extends `${infer Rest}u` ? StripFlagsFromEnd<Rest, `u${Acc}`> :
362
- S extends `${infer Rest}s` ? StripFlagsFromEnd<Rest, `s${Acc}`> :
363
- S extends `${infer Rest}m` ? StripFlagsFromEnd<Rest, `m${Acc}`> :
364
- S extends `${infer Rest}i` ? StripFlagsFromEnd<Rest, `i${Acc}`> :
365
- S extends `${infer Rest}g` ? StripFlagsFromEnd<Rest, `g${Acc}`> :
366
- S extends `${infer Rest}d` ? StripFlagsFromEnd<Rest, `d${Acc}`> :
367
- { body: S; flags: Acc };
368
- export type RegExpLiteralParts<L extends string> =
369
- StripFlagsFromEnd<L> extends { body: infer B extends string; flags: infer F extends string }
370
- ? B extends `/${infer AfterStart}`
371
- ? AfterStart extends `${infer Pattern}/`
372
- ? { pattern: Pattern; flags: F }
373
- : never
374
- : never
375
- : never;
376
- export type PCREStyleRegExpParts<S extends string> = RegExpLiteralParts<PCREStyleToJsRegExpSource<S>>;
377
- export type PCREStyleRegExpPattern<S extends string> = PCREStyleRegExpParts<S>["pattern"];
378
- export type PCREStyleRegExpFlags<S extends string> = PCREStyleRegExpParts<S>["flags"];
379
- export declare const normalizePCREStyleSource: <S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
380
- export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string>(src: S) => PCREStyleRegExpParts<S>;
381
- export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<
382
- PCREStyleRegExpPattern<S>,
383
- PCREStyleRegExpFlags<S>
384
- >;
385
- export as namespace XRegex;
@@ -1,34 +0,0 @@
1
- {
2
- "name": "literate-regex",
3
- "version": "0.1.0",
4
- "description": "Write readable (literate) regex sources with # comments, then normalize them to JS RegExp.source at the type level.",
5
- "license": "Apache-2.0",
6
- "sideEffects": false,
7
- "main": "./dist/cjs/index.js",
8
- "module": "./dist/esm/index.mjs",
9
- "types": "./dist/index.d.ts",
10
- "exports": {
11
- ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/esm/index.mjs",
14
- "require": "./dist/cjs/index.js"
15
- },
16
- "./global": {
17
- "types": "./dist/global.d.ts"
18
- }
19
- },
20
- "files": [
21
- "README.md",
22
- "LICENSE",
23
- "dist"
24
- ],
25
- "keywords": [
26
- "regex",
27
- "regexp",
28
- "literate",
29
- "types",
30
- "typescript",
31
- "pcre",
32
- "jsdoc"
33
- ]
34
- }