js-dev-tool 1.0.19 → 1.0.21

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
package/regex.d.ts CHANGED
@@ -75,6 +75,9 @@ export declare function createRegExp<
75
75
  const P extends string,
76
76
  const F extends string = ""
77
77
  >(pattern: P, flags?: F): TypedRegExp<P, F>;
78
+ type IsEmptyRecord<T> =
79
+ [T] extends [undefined] ? true :
80
+ [keyof T] extends [never] ? true : false;
78
81
  /*!
79
82
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80
83
  // Named Capture Groups
@@ -91,7 +94,9 @@ export type ExtractGroupNames<S extends unknown> =
91
94
  ? FirstChar<Rest> extends "=" | "!"
92
95
  ? ExtractGroupNames<Rest>
93
96
  : Rest extends `${infer GroupName}>${infer After}`
94
- ? GroupName | ExtractGroupNames<After>
97
+ ? GroupName extends ""
98
+ ? "ExtractGroupNames: regex pattern error!"
99
+ : GroupName | ExtractGroupNames<After>
95
100
  : never
96
101
  : never;
97
102
  /**
@@ -100,10 +105,14 @@ export type ExtractGroupNames<S extends unknown> =
100
105
  * @template R - A RegExp type.
101
106
  * @date 2025/12/24 14:46:30 - It may be possible to extract the group name accurately (?)
102
107
  */
103
- export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: infer T }
104
- ? {
105
- [K in ExtractGroupNames<T>]?: string;
106
- } : never;
108
+ export type RegExpNamedGroups<R extends RegExp> =
109
+ R extends RegExp & { readonly source: infer S extends string }
110
+ ? ExtractGroupNames<S> extends infer K
111
+ ? [K] extends [never]
112
+ ? undefined
113
+ : { [P in K & string]: string | undefined }
114
+ : undefined
115
+ : undefined;
107
116
  /*!
108
117
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109
118
  // Capture Groups
@@ -121,7 +130,7 @@ export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: i
121
130
  export type __PreprocessEscapes<S extends string, Result extends string = ""> =
122
131
  S extends `\\\\${infer Rest}`
123
132
  ? __PreprocessEscapes<Rest, `${Result}__`>
124
- : S extends `\\${infer Next}${infer Rest}`
133
+ : S extends `\\${infer EscapedChar}${infer Rest}`
125
134
  ? __PreprocessEscapes<Rest, `${Result}_!`>
126
135
  : S extends `${infer Char}${infer Rest}`
127
136
  ? __PreprocessEscapes<Rest, `${Result}${Char}`>
@@ -150,9 +159,10 @@ export type CountCaptureGroups<
150
159
  export type RegExpExecArrayFixed<
151
160
  R extends RegExp,
152
161
  S extends RegExpSource<R> = RegExpSource<R>,
153
- GroupCount extends number = CountCaptureGroups<S>
162
+ GroupCount extends number = CountCaptureGroups<S>,
163
+ NamedGroups = RegExpNamedGroups<R>
154
164
  > = [match: string, ...BuildCaptureTuple<GroupCount>] & {
155
- groups?: RegExpNamedGroups<R>;
165
+ groups: IsEmptyRecord<NamedGroups> extends true ? undefined : NamedGroups;
156
166
  index: number;
157
167
  input: string;
158
168
  };
@@ -185,13 +195,19 @@ export type BuildCaptureTuple<
185
195
  export type ReplaceCallbackParams<
186
196
  R extends RegExp,
187
197
  S extends RegExpSource<R> = RegExpSource<R>,
188
- GroupCount extends number = CountCaptureGroups<S>
189
- > = [
198
+ GroupCount extends number = CountCaptureGroups<S>,
199
+ NamedGroups = RegExpNamedGroups<R>
200
+ > = IsEmptyRecord<NamedGroups> extends true ? [
190
201
  match: string,
191
202
  ...captures: BuildCaptureTuple<GroupCount>,
192
203
  offset: number,
193
204
  input: string,
194
- groups: RegExpNamedGroups<R>
205
+ ] : [
206
+ match: string,
207
+ ...captures: BuildCaptureTuple<GroupCount>,
208
+ offset: number,
209
+ input: string,
210
+ groups: NamedGroups
195
211
  ];
196
212
  export type ReplacerFunctionSignature<R extends unknown> =
197
213
  R extends (
@@ -200,4 +216,139 @@ export type ReplacerFunctionSignature<R extends unknown> =
200
216
  ? (...args: ReplaceCallbackParams<R/* , T */>) => string
201
217
  : (match: string, ...rest: any[]) => string
202
218
  : string;
219
+ /*! [2025/12/31 13:01:03]
220
+ * ## Support PCRE-style regex source (type-level)
221
+ *
222
+ * @remarks
223
+ * This module provides **type-level normalization** for "PCRE-ish" regex sources:
224
+ * you can write a readable multi-line regex with indentation and `# ...` comments,
225
+ * then derive a compact JavaScript `RegExp` source at compile time.
226
+ *
227
+ * Why type-level? Because it lets us:
228
+ * - keep the regex readable in source code (with comments and formatting)
229
+ * - ensure the normalized output is stable and verifiable (via type assertions)
230
+ *
231
+ * ## Supported rules
232
+ * - `#` starts a comment and consumes the rest of the **current line**
233
+ * - `\#` escapes `#` as a literal (comment is NOT started) and normalizes to `#`
234
+ * - whitespace characters are removed during normalization
235
+ * - newlines are normalized: `\r\n` and `\r` become `\n`
236
+ *
237
+ * ## Design note (ts(2589) survival)
238
+ * Naively scanning the entire string character-by-character (multiple passes)
239
+ * can hit TypeScript's instantiation depth limit (`ts(2589)`).
240
+ * This implementation is **line-oriented**:
241
+ * it strips each line, then concatenates results.
242
+ * That tends to scale much better for long, commented regex sources.
243
+ *
244
+ * ## Non-goals (for now)
245
+ * This is not a full PCRE parser. It intentionally focuses on comment + whitespace
246
+ * normalization for readability and DX.
247
+ */
248
+ export type Whitespace =
249
+ | "\u{9}"
250
+ | "\u{A}"
251
+ | "\u{B}"
252
+ | "\u{C}"
253
+ | "\u{D}"
254
+ | "\u{20}"
255
+ | "\u{85}"
256
+ | "\u{A0}"
257
+ | "\u{1680}"
258
+ | "\u{2000}"
259
+ | "\u{2001}"
260
+ | "\u{2002}"
261
+ | "\u{2003}"
262
+ | "\u{2004}"
263
+ | "\u{2005}"
264
+ | "\u{2006}"
265
+ | "\u{2007}"
266
+ | "\u{2008}"
267
+ | "\u{2009}"
268
+ | "\u{200A}"
269
+ | "\u{2028}"
270
+ | "\u{2029}"
271
+ | "\u{202F}"
272
+ | "\u{205F}"
273
+ | "\u{3000}"
274
+ | "\u{FEFF}";
275
+ /**
276
+ * Normalize newline sequences for line-based parsing.
277
+ *
278
+ * + Align **`\r\n`** and **`\r`** to **`\n`** (makes splitting easier)
279
+ *
280
+ * @template S - Input string literal.
281
+ * @returns A new string literal with `\r\n` and `\r` normalized to `\n`.
282
+ *
283
+ * @example
284
+ * type X = NormalizeNewlines<"a\r\nb\rc\n">; // "a\nb\nc\n"
285
+ */
286
+ type NormalizeNewlines<S extends string> =
287
+ S extends `${infer A}\r\n${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
288
+ S extends `${infer A}\r${infer B}` ? NormalizeNewlines<`${A}\n${B}`> :
289
+ S;
290
+ /**
291
+ * Strip and normalize a single line of a PCRE-style regex source.
292
+ *
293
+ * @remarks
294
+ * Rules:
295
+ * - `\#` is treated as a literal `#` (the backslash is dropped)
296
+ * - an unescaped `#` starts a line comment and truncates the rest
297
+ * - all whitespace characters are removed
298
+ * - other escapes like `\s`, `\w`, `\/`, `\*` are preserved as-is
299
+ *
300
+ * @template S - One line of source (no `\n`).
301
+ * @template Acc - Accumulator for the normalized output.
302
+ *
303
+ * @example
304
+ * type A = StripLine<" \\#\\w+ # comment ">; // "#\\w+"
305
+ * type B = StripLine<" abc # comment">; // "abc"
306
+ *
307
+ * @internal
308
+ *
309
+ * @todo
310
+ * - Handle `\\#` (a literal backslash + literal #) more strictly.
311
+ * - Do not treat `#` as a comment starter inside character classes (`[...]`).
312
+ */
313
+ type StripLine<S extends string, Acc extends string = ""> =
314
+ S extends `\\#${infer Rest}`
315
+ ? StripLine<Rest, `${Acc}#`>
316
+ : S extends `\\${infer C}${infer Rest}`
317
+ ? StripLine<Rest, `${Acc}\\${C}`>
318
+ : S extends `#${string}`
319
+ ? Acc
320
+ : S extends `${infer C}${infer Rest}`
321
+ ? C extends Whitespace
322
+ ? StripLine<Rest, Acc>
323
+ : StripLine<Rest, `${Acc}${C}`>
324
+ : Acc;
325
+ /**
326
+ * Convert a multi-line PCRE-style regex source into a compact JS `RegExp.source`.
327
+ *
328
+ * @remarks
329
+ * This type:
330
+ * 1) normalizes newlines, 2) splits by line, 3) applies {@link StripLine},
331
+ * then 4) concatenates the results.
332
+ *
333
+ * @template Input - A `const` string literal of the PCRE-ish source.
334
+ * @template Acc - Accumulator for the result.
335
+ * @template S - Internal normalized newline buffer (do not pass manually).
336
+ *
337
+ * @example
338
+ * const RE_SOURCE = `
339
+ * ^ # start
340
+ * (?:\\#\\w+) # keep literal "#"
341
+ * ` as const;
342
+ *
343
+ * type Src = PCREStyleToJsRegExpSource<typeof RE_SOURCE>;
344
+ * // "^(:?##\\w+)" ... (depending on your actual input)
345
+ */
346
+ export type PCREStyleToJsRegExpSource<
347
+ Input extends string,
348
+ Acc extends string = "",
349
+ S extends string = NormalizeNewlines<Input>,
350
+ > =
351
+ S extends `${infer Line}\n${infer Rest}`
352
+ ? PCREStyleToJsRegExpSource<Input, `${Acc}${StripLine<Line>}`, Rest>
353
+ : `${Acc}${StripLine<S>}`;
203
354
  export as namespace XRegex;
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.0.19"
2
+ "version": "1.0.21"
3
3
  }
package/tool-lib/cjbm.js CHANGED
@@ -41,8 +41,12 @@ function getReplacer(ext) {
41
41
  */
42
42
  /** @type {TImportExportDetectRegexReplacer} */
43
43
  return (
44
- match, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _offset, _input, groups
44
+ ...args
45
45
  ) => {
46
+ const [
47
+ match,,,,,,,,,,,,,
48
+ groups
49
+ ] = args;
46
50
  if (/\s*?\/\*/.test(match)) return match;
47
51
  const {
48
52
  clause,