js-dev-tool 1.0.16 → 1.0.18

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.16",
3
+ "version": "1.0.18",
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
@@ -12,15 +12,44 @@ declare global {
12
12
  interface RegExp/* extends TypedRegExp<string, string>*/ {
13
13
  /**
14
14
  * Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
15
- * @param string The String object or string literal on which to perform the 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.
16
20
  */
17
- exec<const P extends string, const F extends string = "">(this: TypedRegExp<P, F>, string: string): RegExpExecArrayFixed< this > | null;
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;
18
31
  }
19
32
  interface RegExpConstructor {
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
+ */
20
41
  new <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
21
42
  readonly source: P;
22
43
  readonly flags: F;
23
44
  };
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
+ */
24
53
  <const P extends string, const F extends string = "">(pattern: P, flags?: F): RegExp & {
25
54
  readonly source: P;
26
55
  readonly flags: F;
@@ -33,15 +62,19 @@ declare global {
33
62
  // Basics
34
63
  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35
64
  */
65
+ export type Brand<Tag extends string> = {
66
+ [k in Tag]: true;
67
+ };
68
+ export type TypedRegExpBrand = Brand<"__TypedRegExp">;
69
+ export type TypedRegExp<const P extends string, const F extends string = ""> = RegExp & {
70
+ readonly source: P;
71
+ readonly flags: F;
72
+ } & TypedRegExpBrand;
36
73
  /**
37
74
  * Extracts the literal type from the source property of a RegExp.
38
75
  * @template R - A RegExp type.
39
76
  */
40
- export type ReSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
41
- export type TypedRegExp<const P extends string, const F extends string = ""> = RegExp & {
42
- readonly source: P;
43
- readonly flags: F;
44
- };
77
+ export type RegExpSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
45
78
  export declare function createRegExp<
46
79
  const P extends string,
47
80
  const F extends string = ""
@@ -71,10 +104,15 @@ export type ExtractGroupNames<S extends string> =
71
104
  * @template R - A RegExp type.
72
105
  * @date 2025/12/24 14:46:30 - It may be possible to extract the group name accurately (?)
73
106
  */
74
- export type ReGroups<R extends RegExp> = R extends (/*RegExp &*/ { readonly source: infer T })
107
+ export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: infer T }
75
108
  ? {
76
109
  [K in ExtractGroupNames<T>]?: string;
77
110
  } : never;
111
+ /*!
112
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113
+ // Capture Groups
114
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115
+ */
78
116
  /**
79
117
  * Preprocesses escape sequences in a string pattern by replacing escaped backslashes and characters.
80
118
  *
@@ -115,10 +153,10 @@ export type CountCaptureGroups<
115
153
  */
116
154
  export type RegExpExecArrayFixed<
117
155
  R extends RegExp,
118
- S extends ReSource<R> = ReSource<R>,
156
+ S extends RegExpSource<R> = RegExpSource<R>,
119
157
  GroupCount extends number = CountCaptureGroups<S>
120
158
  > = [match: string, ...BuildCaptureTuple<GroupCount>] & {
121
- groups?: ReGroups<R>;
159
+ groups?: RegExpNamedGroups<R>;
122
160
  index: number;
123
161
  input: string;
124
162
  };
@@ -150,25 +188,20 @@ export type BuildCaptureTuple<
150
188
  */
151
189
  export type ReplaceCallbackParams<
152
190
  R extends RegExp,
153
- S extends ReSource<R> = ReSource<R>,
191
+ S extends RegExpSource<R> = RegExpSource<R>,
154
192
  GroupCount extends number = CountCaptureGroups<S>
155
193
  > = [
156
194
  match: string,
157
195
  ...captures: BuildCaptureTuple<GroupCount>,
158
196
  offset: number,
159
197
  input: string,
160
- groups: ReGroups<R>
198
+ groups: RegExpNamedGroups<R>
161
199
  ];
162
- export type ReplacerFunctionSignature<R extends RegExp> = (...args: ReplaceCallbackParams<R>) => string;
163
- const re1 = createRegExp("(test)(group)()()()()()()()(22)", "g");
164
- type TRe1Replacer = ReplacerFunctionSignature<typeof re1>;
165
- type ExecResult1 = RegExpExecArrayFixed<typeof re1>;
166
- const re2 = createRegExp("(?<name1>test)(?<name2>group)", "g");
167
- type TRe2Replacer = ReplacerFunctionSignature<typeof re2>;
168
- type ExecResult2 = RegExpExecArrayFixed<typeof re2>;
169
- const result = re2.exec("test group") as RegExpExecArrayFixed<typeof re2>;
170
- if (result) {
171
- console.log(result[1]);
172
- console.log(result.groups?.name1);
173
- }
200
+ export type ReplacerFunctionSignature<R extends unkown> =
201
+ R extends (
202
+ RegExp & { readonly source: infer T }
203
+ ) ? R extends TypedRegExpBrand
204
+ ? (...args: ReplaceCallbackParams<R, T>) => string
205
+ : (match: string, ...rest: any[]) => string
206
+ : string;
174
207
  export as namespace XRegex;
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.0.16"
2
+ "version": "1.0.18"
3
3
  }
package/tool-lib/cjbm.js CHANGED
@@ -13,10 +13,15 @@
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
- return new RegExp(pattern, flags);
24
+ return /** @type {XRegex.TypedRegExp<P, F>} */(new RegExp(pattern, flags));
20
25
  }
21
26
  const reImportExportDetection = createRegExp(
22
27
  `(?<!\\s*\\/\\/+.*?)(?:(?<!@)import|export)\\s*(?:(?<qcGlobal>["'])(?!https?:)(?=[.\\/]+)\
@@ -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
  /**