js-dev-tool 1.0.16 → 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 +2 -1
- package/regex.d.ts +46 -20
- package/scripts/publish-version.json +1 -1
- package/tool-lib/cjbm.js +7 -2
- package/tool-lib/cmt-trick.js +4 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-dev-tool",
|
|
3
|
-
"version": "1.0.
|
|
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
|
@@ -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
|
-
* @
|
|
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>,
|
|
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;
|
|
@@ -37,7 +66,7 @@ declare global {
|
|
|
37
66
|
* Extracts the literal type from the source property of a RegExp.
|
|
38
67
|
* @template R - A RegExp type.
|
|
39
68
|
*/
|
|
40
|
-
export type
|
|
69
|
+
export type RegExpSource<R extends RegExp> = R extends RegExp & { readonly source: infer T } ? T : never;
|
|
41
70
|
export type TypedRegExp<const P extends string, const F extends string = ""> = RegExp & {
|
|
42
71
|
readonly source: P;
|
|
43
72
|
readonly flags: F;
|
|
@@ -71,10 +100,15 @@ export type ExtractGroupNames<S extends string> =
|
|
|
71
100
|
* @template R - A RegExp type.
|
|
72
101
|
* @date 2025/12/24 14:46:30 - It may be possible to extract the group name accurately (?)
|
|
73
102
|
*/
|
|
74
|
-
export type
|
|
103
|
+
export type RegExpNamedGroups<R extends RegExp> = R extends { readonly source: infer T }
|
|
75
104
|
? {
|
|
76
105
|
[K in ExtractGroupNames<T>]?: string;
|
|
77
106
|
} : never;
|
|
107
|
+
/*!
|
|
108
|
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
109
|
+
// Capture Groups
|
|
110
|
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
111
|
+
*/
|
|
78
112
|
/**
|
|
79
113
|
* Preprocesses escape sequences in a string pattern by replacing escaped backslashes and characters.
|
|
80
114
|
*
|
|
@@ -115,10 +149,10 @@ export type CountCaptureGroups<
|
|
|
115
149
|
*/
|
|
116
150
|
export type RegExpExecArrayFixed<
|
|
117
151
|
R extends RegExp,
|
|
118
|
-
S extends
|
|
152
|
+
S extends RegExpSource<R> = RegExpSource<R>,
|
|
119
153
|
GroupCount extends number = CountCaptureGroups<S>
|
|
120
154
|
> = [match: string, ...BuildCaptureTuple<GroupCount>] & {
|
|
121
|
-
groups?:
|
|
155
|
+
groups?: RegExpNamedGroups<R>;
|
|
122
156
|
index: number;
|
|
123
157
|
input: string;
|
|
124
158
|
};
|
|
@@ -150,25 +184,17 @@ export type BuildCaptureTuple<
|
|
|
150
184
|
*/
|
|
151
185
|
export type ReplaceCallbackParams<
|
|
152
186
|
R extends RegExp,
|
|
153
|
-
S extends
|
|
187
|
+
S extends RegExpSource<R> = RegExpSource<R>,
|
|
154
188
|
GroupCount extends number = CountCaptureGroups<S>
|
|
155
189
|
> = [
|
|
156
190
|
match: string,
|
|
157
191
|
...captures: BuildCaptureTuple<GroupCount>,
|
|
158
192
|
offset: number,
|
|
159
193
|
input: string,
|
|
160
|
-
groups:
|
|
194
|
+
groups: RegExpNamedGroups<R>
|
|
161
195
|
];
|
|
162
|
-
export type ReplacerFunctionSignature<R extends
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
}
|
|
196
|
+
export type ReplacerFunctionSignature<R extends unkown> =
|
|
197
|
+
R extends (
|
|
198
|
+
RegExp & { readonly source: infer T }
|
|
199
|
+
) ? (...args: ReplaceCallbackParams<R, T>) => string : string;
|
|
174
200
|
export as namespace XRegex;
|
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
|
-
*
|
|
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\`!
|
package/tool-lib/cmt-trick.js
CHANGED
|
@@ -15,23 +15,11 @@
|
|
|
15
15
|
* @param {string} code
|
|
16
16
|
*/
|
|
17
17
|
function cleanup(code) {
|
|
18
|
-
const
|
|
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
|
-
|
|
22
|
+
reCleanUp, ($0, $1, $2) => $1 || $2,
|
|
35
23
|
);
|
|
36
24
|
}
|
|
37
25
|
/**
|