js-dev-tool 1.0.15 → 1.0.17

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.15",
3
+ "version": "1.0.17",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
@@ -35,6 +35,7 @@
35
35
  "!tool-lib/cjbm-*",
36
36
  "!scripts/unzip.ts",
37
37
  "!scripts/bin",
38
+ "!regex-test.ts",
38
39
  "!extras/npm",
39
40
  "!/**/*.png",
40
41
  "!tool.sh",
package/regex.d.ts CHANGED
@@ -9,23 +9,64 @@
9
9
  * @file js-dev-scripts/regex.d.ts
10
10
  */
11
11
  declare global {
12
+ interface RegExp/* extends TypedRegExp<string, string>*/ {
13
+ /**
14
+ * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
15
+ * @template {string} P
16
+ * @template {string} F
17
+ * @param {TypedRegExp<P, F>} this The regular expression object.
18
+ * @param {string} str The String object or string literal on which to perform the search.
19
+ * @returns {RegExpExecArrayFixed< this > | null} An array of results or null if no match is found.
20
+ */
21
+ exec<const P extends string, const F extends string = "">(this: TypedRegExp<P, F>, str: string): RegExpExecArrayFixed< this > | null;
22
+ }
23
+ interface String {
24
+ /**
25
+ * Replaces occurrences of a pattern in the string with a specified replacement.
26
+ * @template {RegExp | string} SV
27
+ * @param searchValue A regular expression object.
28
+ * @param replaceValue A string or a function to create the new substring.
29
+ */
30
+ replace<SV extends RegExp | string>(this: string, searchValue: SV, replaceValue: ReplacerFunctionSignature<SV>): string;
31
+ }
12
32
  interface RegExpConstructor {
13
- new <const P extends string, const F extends string = "">(pattern: string, flags?: F): RegExp & {
33
+ /**
34
+ * Creates a typed RegExp object with literal types for the pattern and flags.
35
+ * @template {string} P
36
+ * @template {string} F
37
+ * @param {P} pattern The regular expression pattern.
38
+ * @param {F} [flags] The flags for the regular expression.
39
+ * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
40
+ */
41
+ new <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
14
42
  readonly source: P;
15
43
  readonly flags: F;
16
44
  };
17
- <const P extends string, const F extends string = "">(pattern: string, flags?: F): RegExp & {
45
+ /**
46
+ * Creates a typed RegExp object with literal types for the pattern and flags.
47
+ * @template {string} P
48
+ * @template {string} F
49
+ * @param {P} pattern The regular expression pattern.
50
+ * @param {F} [flags] The flags for the regular expression.
51
+ * @returns {RegExp & { readonly source: P; readonly flags: F }} A typed RegExp object.
52
+ */
53
+ <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
18
54
  readonly source: P;
19
55
  readonly flags: F;
20
56
  };
21
57
  readonly "prototype": RegExp;
22
58
  }
23
59
  }
60
+ /*!
61
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62
+ // Basics
63
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64
+ */
24
65
  /**
25
66
  * Extracts the literal type from the source property of a RegExp.
26
67
  * @template R - A RegExp type.
27
68
  */
28
- export type ReSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
69
+ export type RegExpSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
29
70
  export type TypedRegExp<const P extends string, const F extends string = ""> = RegExp & {
30
71
  readonly source: P;
31
72
  readonly flags: F;
@@ -34,6 +75,11 @@ export declare function createRegExp<
34
75
  const P extends string,
35
76
  const F extends string = ""
36
77
  >(pattern: P, flags?: F): TypedRegExp<P, F>;
78
+ /*!
79
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80
+ // Named Capture Groups
81
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82
+ */
37
83
  type FirstChar<S extends string> = S extends `${infer F}${infer _}` ? F : never;
38
84
  /**
39
85
  * Recursively extracts parts that match the format (?<GroupName>...) from a string pattern,
@@ -52,10 +98,17 @@ export type ExtractGroupNames<S extends string> =
52
98
  * Creates an object type with keys as the extracted group names and values as strings.
53
99
  * If no groups are found, it results in an empty object.
54
100
  * @template R - A RegExp type.
101
+ * @date 2025/12/24 14:46:30 - It may be possible to extract the group name accurately (?)
55
102
  */
56
- export type ReGroups<R extends RegExp> = {
57
- [K in ExtractGroupNames<ReSource<R>>]?: string;
58
- };
103
+ export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: infer T }
104
+ ? {
105
+ [K in ExtractGroupNames<T>]?: string;
106
+ } : never;
107
+ /*!
108
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109
+ // Capture Groups
110
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
+ */
59
112
  /**
60
113
  * Preprocesses escape sequences in a string pattern by replacing escaped backslashes and characters.
61
114
  *
@@ -86,7 +139,7 @@ export type CountCaptureGroups<
86
139
  ? Rest extends `${"?:" | "?=" | "?!" | "?<=" | "?<!"}${infer After}`
87
140
  ? CountCaptureGroups<After, Counter>
88
141
  : CountCaptureGroups<Rest, [...Counter, unknown]>
89
- : Counter['length'];
142
+ : Counter["length"];
90
143
  /**
91
144
  * Represents a fixed version of RegExpExecArray that includes the matched string,
92
145
  * captures, and optionally named groups.
@@ -96,21 +149,32 @@ export type CountCaptureGroups<
96
149
  */
97
150
  export type RegExpExecArrayFixed<
98
151
  R extends RegExp,
99
- S extends ReSource<R> = ReSource<R>,
152
+ S extends RegExpSource<R> = RegExpSource<R>,
100
153
  GroupCount extends number = CountCaptureGroups<S>
101
- > = [match: string, ...ExtractCaptures<GroupCount>] & {
102
- groups?: ReGroups<R>;
154
+ > = [match: string, ...BuildCaptureTuple<GroupCount>] & {
155
+ groups?: RegExpNamedGroups<R>;
103
156
  index: number;
104
157
  input: string;
105
158
  };
159
+ export type TupleOf<
160
+ Count extends number, ArrayType,
161
+ Result extends ArrayType[] = []
162
+ > = Result["length"] extends Count
163
+ ? Result
164
+ : TupleOf<Count, ArrayType, [...Result, ArrayType]>;
106
165
  /**
107
- * Generates an array type with a length corresponding to the number of capture groups.
108
- * @template Count - The number of capture groups.
109
- * @template Result - The resulting array type.
166
+ * Builds a tuple type whose length equals the number of capture groups.
167
+ * @template Count - Number of capture groups.
168
+ * @template Result - Accumulator (internal).
110
169
  */
111
- export type ExtractCaptures<Count extends number, Result extends unknown[] = []> =
112
- Result['length'] extends Count
113
- ? Result : ExtractCaptures<Count, [...Result, string]>;
170
+ export type BuildCaptureTuple<
171
+ Count extends number, ArrayType = string,
172
+ > = TupleOf<Count, ArrayType>;
173
+ /*!
174
+ // ============================================================================
175
+ // Helper Types for Replacer
176
+ // ============================================================================
177
+ */
114
178
  /**
115
179
  * Creates the parameter types for String.replace callback function.
116
180
  *
@@ -120,25 +184,17 @@ export type ExtractCaptures<Count extends number, Result extends unknown[] = []>
120
184
  */
121
185
  export type ReplaceCallbackParams<
122
186
  R extends RegExp,
123
- S extends ReSource<R> = ReSource<R>,
187
+ S extends RegExpSource<R> = RegExpSource<R>,
124
188
  GroupCount extends number = CountCaptureGroups<S>
125
189
  > = [
126
190
  match: string,
127
- ...captures: ExtractCaptures<GroupCount>,
191
+ ...captures: BuildCaptureTuple<GroupCount>,
128
192
  offset: number,
129
193
  input: string,
130
- groups: ReGroups<R>
194
+ groups: RegExpNamedGroups<R>
131
195
  ];
132
- export type ReplacerFunctionSignature<R extends RegExp> = (...args: ReplaceCallbackParams<R>) => string;
133
- const re1 = createRegExp("(test)(group)()()()()()()()(22)", "g");
134
- type TRe1Replacer = ReplacerFunctionSignature<typeof re1>;
135
- type ExecResult1 = RegExpExecArrayFixed<typeof re1>;
136
- const re2 = createRegExp("(?<name1>test)(?<name2>group)", "g");
137
- type TRe2Replacer = ReplacerFunctionSignature<typeof re2>;
138
- type ExecResult2 = RegExpExecArrayFixed<typeof re2>;
139
- const result = re2.exec("test group") as RegExpExecArrayFixed<typeof re2>;
140
- if (result) {
141
- console.log(result[1]);
142
- console.log(result.groups?.name1);
143
- }
196
+ export type ReplacerFunctionSignature<R extends unkown> =
197
+ R extends (
198
+ RegExp & { readonly source: infer T }
199
+ ) ? (...args: ReplaceCallbackParams<R, T>) => string : string;
144
200
  export as namespace XRegex;
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.0.15"
2
+ "version": "1.0.17"
3
3
  }
package/tool-lib/cjbm.js CHANGED
@@ -13,7 +13,12 @@
13
13
  const fs = require("fs");
14
14
  const path = require("path");
15
15
  /**
16
- * @type {typeof XRegex.createRegExp}
16
+ * Creates a typed RegExp object with literal types for the pattern and flags.
17
+ * @template {string} P
18
+ * @template {string} F
19
+ * @param {P} pattern The regular expression pattern.
20
+ * @param {F} [flags] The flags for the regular expression.
21
+ * @returns {XRegex.TypedRegExp<P, F>} A typed RegExp object.
17
22
  */
18
23
  function createRegExp(pattern, flags) {
19
24
  return new RegExp(pattern, flags);
@@ -127,7 +132,7 @@ module.exports = () => {
127
132
  root - Recursively searches for files.
128
133
  This option is useful, but if there are directories that need to be avoided, use the 'basePath' option
129
134
  basePath - can be "<path>,<path>,..." (array type arg)
130
- ext - specifies the module extension for import clauses. default is "js"
135
+ ext - specifies the module extension for import/export clauses. default is "js"
131
136
  test - Any file extension can be specified. (The default is /\\.js$/)
132
137
  targets - specify this if you want to apply it only to a specific file.
133
138
  the file specified here should be directly under \`basePath\`!
@@ -15,23 +15,11 @@
15
15
  * @param {string} code
16
16
  */
17
17
  function cleanup(code) {
18
- const pcreStyle = String.raw`
19
- \s*\/
20
- (?:
21
- \s*\/+\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)
22
- ([\s\S]+?)
23
- (?:\s+)\/\*\/
24
- [\s\S]+
25
- |
26
- \s*\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)
27
- [\s\S]+
28
- \/\*\/
29
- ([\s\S]+?)
30
- )
31
- \s*\/{2,}\*\/
32
- `;
18
+ const reCleanUp = new RegExp(
19
+ "\s*\/(?:\s*\/+\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)([\s\S]+?)(?:\s+)\/\*\/[\s\S]+|\s*\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)[\s\S]+\/\*\/([\s\S]+?))\s*\/{2,}\*\/", "g"
20
+ );
33
21
  return code.replace(
34
- new RegExp(pcreStyle.replace(/\s*\(\?#.*\)\s*$|#\s.*$|\s+/gm, ""), "g"), ($0, $1, $2) => $1 || $2,
22
+ reCleanUp, ($0, $1, $2) => $1 || $2,
35
23
  );
36
24
  }
37
25
  /**