@zwa73/utils 1.0.107 → 1.0.109

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.
@@ -17,11 +17,15 @@ type EnsurePathExistsOpt = Partial<{
17
17
  type FileSearchGlobOpt = Partial<{
18
18
  /**忽略的文件 默认 undefined */
19
19
  ingore: string | string[];
20
+ /**输出的路径风格 默认跟随系统 */
21
+ normalize: "posix" | "win32";
20
22
  }>;
21
23
  /**regex搜索选项 */
22
24
  type FileSearchRegexOpt = Partial<{
23
25
  /**搜索子目录 默认 true */
24
26
  relative: boolean;
27
+ /**输出的路径风格 默认跟随系统 */
28
+ normalize: "posix" | "win32";
25
29
  }>;
26
30
  /**文件工具 */
27
31
  export declare namespace UtilFT {
@@ -98,21 +102,23 @@ export declare namespace UtilFT {
98
102
  */
99
103
  function writeJSONFile(filePath: string, token: JToken): Promise<void>;
100
104
  /**搜索路径符合正则表达式的文件
101
- * @param folder - 文件夹路径
105
+ * @param dir - 起始目录
102
106
  * @param traitRegex - 正则表达式
103
107
  * @param opt - 可选参数
104
108
  * @param opt.relative - 搜索子目录
109
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
105
110
  * @returns 文件名路径数组
106
111
  */
107
- function fileSearchRegex(folder: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
112
+ function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
108
113
  /**搜索符合Glob匹配的文件
109
- * @param folder - 文件夹路径
114
+ * @param dir - 起始目录
110
115
  * @param globPattern - glob匹配
111
116
  * @param opt - 可选参数
112
- * @param opt.ignore - 忽略的文件
117
+ * @param opt.ignore - 忽略的文件
118
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
113
119
  * @returns 文件绝对路径数组
114
120
  */
115
- function fileSearchGlob(folder: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
121
+ function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
116
122
  /**
117
123
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
118
124
  */
@@ -194,19 +194,21 @@ var UtilFT;
194
194
  }
195
195
  UtilFT.writeJSONFile = writeJSONFile;
196
196
  /**搜索路径符合正则表达式的文件
197
- * @param folder - 文件夹路径
197
+ * @param dir - 起始目录
198
198
  * @param traitRegex - 正则表达式
199
199
  * @param opt - 可选参数
200
200
  * @param opt.relative - 搜索子目录
201
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
201
202
  * @returns 文件名路径数组
202
203
  */
203
- function fileSearchRegex(folder, traitRegex, opt) {
204
+ function fileSearchRegex(dir, traitRegex, opt) {
204
205
  const relative = opt?.relative ?? true;
205
206
  const outArray = [];
206
- const subFiles = fs.readdirSync(folder, { withFileTypes: true });
207
+ const subFiles = fs.readdirSync(dir, { withFileTypes: true });
208
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
207
209
  const regex = new RegExp(traitRegex);
208
210
  for (const subFile of subFiles) {
209
- const subFilePath = path.join(folder, subFile.name);
211
+ const subFilePath = path.join(dir, subFile.name);
210
212
  //判断是否是文件夹,递归调用
211
213
  if (subFile.isDirectory()) {
212
214
  if (relative)
@@ -216,21 +218,31 @@ var UtilFT;
216
218
  if (regex.test(subFilePath))
217
219
  outArray.push(subFilePath);
218
220
  }
219
- return outArray;
221
+ return outArray.map((filePath) => {
222
+ if (opt?.normalize === undefined)
223
+ return filePath;
224
+ return path[opt.normalize].normalize(filePath);
225
+ });
226
+ ;
220
227
  }
221
228
  UtilFT.fileSearchRegex = fileSearchRegex;
222
229
  /**搜索符合Glob匹配的文件
223
- * @param folder - 文件夹路径
230
+ * @param dir - 起始目录
224
231
  * @param globPattern - glob匹配
225
232
  * @param opt - 可选参数
226
- * @param opt.ignore - 忽略的文件
233
+ * @param opt.ignore - 忽略的文件
234
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
227
235
  * @returns 文件绝对路径数组
228
236
  */
229
- function fileSearchGlob(folder, globPattern, opt) {
237
+ function fileSearchGlob(dir, globPattern, opt) {
230
238
  const fixedPath = typeof globPattern === "string"
231
- ? path.join(folder, path.posix.normalize(globPattern))
232
- : globPattern.map((p) => path.join(folder, path.posix.normalize(p)));
233
- return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true });
239
+ ? path.join(dir, path.posix.normalize(globPattern))
240
+ : globPattern.map((p) => path.join(dir, path.posix.normalize(p)));
241
+ return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true }).map((filePath) => {
242
+ if (opt?.normalize === undefined)
243
+ return filePath;
244
+ return path[opt.normalize].normalize(filePath);
245
+ });
234
246
  }
235
247
  UtilFT.fileSearchGlob = fileSearchGlob;
236
248
  /**
@@ -1,4 +1,4 @@
1
- import { AnyFunc, ComposedClass, ComposedMixinable, ComposedCtorMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseVerifyFn, RefMixinable, UnionToIntersection } from "./UtilInterfaces";
1
+ import { AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseVerifyFn, RefMixinable, UnionToIntersection } from "./UtilInterfaces";
2
2
  import { LogLevel } from "./UtilLogger";
3
3
  import { FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
4
  type SuccessOut<T> = Outcome<Success, T>;
@@ -83,11 +83,11 @@ export declare class UtilFunc {
83
83
  */
84
84
  static composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
85
85
  /**根据 MIXIN_FIELDS 自动混入
86
- * @deprecated 请使用 composeRefMixinable
86
+ * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
87
87
  */
88
88
  static composeMixinable<Base extends object, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
89
89
  /**根据 MIXIN_FIELDS 自动混入 */
90
- static composeRefMixinable<Base extends object, Mixins extends RefMixinable<any, unknown>[]>(base: Base, ...mixins: Mixins): ComposedCtorMixinable<Base, Mixins>;
90
+ static composeRefMixinable<Base extends object, Mixins extends RefMixinable<any, unknown>[]>(base: Base, ...mixins: Mixins): ComposedRefMixinable<Base, Mixins>;
91
91
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
92
92
  * @template T - 对象的类型
93
93
  * @param obj - 要处理的对象
@@ -255,17 +255,21 @@ class UtilFunc {
255
255
  for (const fd of fields) {
256
256
  if (compObj[fd] !== undefined)
257
257
  UtilLogger_1.SLogger.warn(`field: ${fd} 已存在于基础类中, 混入可能导致类型问题, 如需覆盖, 请使用 ${key}=val`);
258
- //Object.defineProperty(compObj, fd, {
259
- // get: ()=>compObj[key][fd],
260
- // set: (value)=>{compObj[key][fd] = value},
261
- // enumerable:true ,
262
- // //writable: true ,
263
- // configurable: true
264
- //});
258
+ //func需绑定this无法直接设置属性
265
259
  if (typeof mixin[fd] === 'function') {
266
260
  compObj[fd] = (...args) => compObj[key][fd](...args);
261
+ //#region other
262
+ //const memo = (...args: any[]) => compObj[key][fd](...args);
263
+ //Object.defineProperty(compObj, fd, {
264
+ // get:()=>memo,
265
+ // set:()=>SLogger.warn(new Error(`试图修改一个由 composeClassPart 组合的函数字段:${String(fd)}, 修改已被忽略`)),
266
+ // enumerable:true ,
267
+ // //writable: true ,
268
+ // configurable: true
269
+ //});
267
270
  //(compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
268
271
  //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
272
+ //#endregion
269
273
  }
270
274
  else {
271
275
  Object.defineProperty(compObj, fd, {
@@ -280,7 +284,7 @@ class UtilFunc {
280
284
  return compObj;
281
285
  }
282
286
  /**根据 MIXIN_FIELDS 自动混入
283
- * @deprecated 请使用 composeRefMixinable
287
+ * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
284
288
  */
285
289
  static composeMixinable(base, ...mixins) {
286
290
  let out = base;
@@ -117,16 +117,17 @@ export type ReflectionAble<Ctor, Constraint = {}> = {
117
117
  /**原型构造器 */
118
118
  readonly CTOR: Ctor & Constraint;
119
119
  };
120
- /**构造函数是可混入的类
121
- * @template Ctor - 构造器类型
120
+ /**可反射的 且 构造函数是可混入的类
121
+ * @template Ctor - 构造器类型
122
122
  * @template Instance - 实例类型
123
123
  * @template Constraint - 构造器约束
124
124
  */
125
125
  export type RefMixinable<Ctor, Instance, Constraint = {}> = ReflectionAble<Ctor, Mixinable<Instance> & Constraint>;
126
- /**自动组合可混入的类 */
126
+ /**自动组合可混入的类
127
+ */
127
128
  export type ComposedMixinable<B, Ms extends unknown[]> = Ms extends [infer M, ...infer Rest] ? M extends Mixinable<M> ? ComposedMixinable<ComposedClass<B, M, M['MIXIN_KEY'], M['MIXIN_FIELDS'][number]>, Rest> : "一个混入类没有实现 Mixinable<self>" & Error : B;
128
- /**自动组合构造类型是可混入的类 */
129
- export type ComposedCtorMixinable<B, Ms extends unknown[]> = Ms extends [infer M, ...infer Rest] ? M extends RefMixinable<unknown, M> ? ComposedCtorMixinable<ComposedClass<B, M, M['CTOR']['MIXIN_KEY'], M['CTOR']['MIXIN_FIELDS'][number]>, Rest> : "一个混入类没有实现 RefMixinable<typeof self, self>" & Error : B;
129
+ /**自动组合 可反射的 且 构造函数是可混入的 类 */
130
+ export type ComposedRefMixinable<B, Ms extends unknown[]> = Ms extends [infer M, ...infer Rest] ? M extends RefMixinable<unknown, M> ? ComposedRefMixinable<ComposedClass<B, M, M['CTOR']['MIXIN_KEY'], M['CTOR']['MIXIN_FIELDS'][number]>, Rest> : "一个混入类没有实现 RefMixinable<typeof self, self>" & Error : B;
130
131
  /** extends封装
131
132
  * @template B - 基础类型
132
133
  * @template T - 判断的目标类型
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.107",
3
+ "version": "1.0.109",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,12 +25,16 @@ type EnsurePathExistsOpt = Partial<{
25
25
  type FileSearchGlobOpt = Partial<{
26
26
  /**忽略的文件 默认 undefined */
27
27
  ingore:string|string[];
28
+ /**输出的路径风格 默认跟随系统 */
29
+ normalize:"posix"|"win32";
28
30
  }>
29
31
 
30
32
  /**regex搜索选项 */
31
33
  type FileSearchRegexOpt = Partial<{
32
34
  /**搜索子目录 默认 true */
33
35
  relative:boolean;
36
+ /**输出的路径风格 默认跟随系统 */
37
+ normalize:"posix"|"win32";
34
38
  }>
35
39
 
36
40
  /**文件工具 */
@@ -228,19 +232,21 @@ export async function writeJSONFile(
228
232
  }
229
233
 
230
234
  /**搜索路径符合正则表达式的文件
231
- * @param folder - 文件夹路径
235
+ * @param dir - 起始目录
232
236
  * @param traitRegex - 正则表达式
233
237
  * @param opt - 可选参数
234
238
  * @param opt.relative - 搜索子目录
239
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
235
240
  * @returns 文件名路径数组
236
241
  */
237
- export function fileSearchRegex(folder: string, traitRegex: string, opt?:FileSearchRegexOpt) {
242
+ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
238
243
  const relative = opt?.relative ?? true;
239
244
  const outArray: string[] = [];
240
- const subFiles = fs.readdirSync(folder,{withFileTypes:true});
245
+ const subFiles = fs.readdirSync(dir,{withFileTypes:true});
246
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
241
247
  const regex = new RegExp(traitRegex);
242
248
  for (const subFile of subFiles) {
243
- const subFilePath = path.join(folder, subFile.name);
249
+ const subFilePath = path.join(dir, subFile.name);
244
250
  //判断是否是文件夹,递归调用
245
251
  if (subFile.isDirectory()) {
246
252
  if(relative) outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
@@ -248,20 +254,27 @@ export function fileSearchRegex(folder: string, traitRegex: string, opt?:FileSea
248
254
  }
249
255
  if (regex.test(subFilePath)) outArray.push(subFilePath);
250
256
  }
251
- return outArray;
257
+ return outArray.map((filePath)=>{
258
+ if(opt?.normalize===undefined) return filePath;
259
+ return path[opt.normalize].normalize(filePath);
260
+ });;
252
261
  }
253
262
  /**搜索符合Glob匹配的文件
254
- * @param folder - 文件夹路径
263
+ * @param dir - 起始目录
255
264
  * @param globPattern - glob匹配
256
265
  * @param opt - 可选参数
257
- * @param opt.ignore - 忽略的文件
266
+ * @param opt.ignore - 忽略的文件
267
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
258
268
  * @returns 文件绝对路径数组
259
269
  */
260
- export function fileSearchGlob(folder: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
270
+ export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
261
271
  const fixedPath = typeof globPattern === "string"
262
- ? path.join(folder,path.posix.normalize(globPattern))
263
- : globPattern.map((p)=>path.join(folder,path.posix.normalize(p)));
264
- return globSync(fixedPath,{ignore:opt?.ingore,absolute:true});
272
+ ? path.join(dir,path.posix.normalize(globPattern))
273
+ : globPattern.map((p)=>path.join(dir,path.posix.normalize(p)));
274
+ return globSync(fixedPath,{ignore:opt?.ingore,absolute:true}).map((filePath)=>{
275
+ if(opt?.normalize===undefined) return filePath;
276
+ return path[opt.normalize].normalize(filePath);
277
+ });
265
278
  }
266
279
  /**
267
280
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { AnyFunc, ComposedClass, ComposedMixinable, ComposedCtorMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseStat, PromiseVerifyFn, RefMixinable, UnionToIntersection } from "@src/UtilInterfaces";
2
+ import { AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseStat, PromiseVerifyFn, RefMixinable, UnionToIntersection } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
4
  import { LogLevel, SLogger } from "@src/UtilLogger";
5
5
  import { Completed, Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Terminated, Timeout } from "./UtilSymbol";
@@ -280,17 +280,21 @@ static composeClassPart
280
280
  for(const fd of fields){
281
281
  if(compObj[fd]!==undefined)
282
282
  SLogger.warn(`field: ${fd as string} 已存在于基础类中, 混入可能导致类型问题, 如需覆盖, 请使用 ${key}=val`);
283
- //Object.defineProperty(compObj, fd, {
284
- // get: ()=>compObj[key][fd],
285
- // set: (value)=>{compObj[key][fd] = value},
286
- // enumerable:true ,
287
- // //writable: true ,
288
- // configurable: true
289
- //});
283
+ //func需绑定this无法直接设置属性
290
284
  if(typeof mixin[fd] === 'function') {
291
285
  compObj[fd] = (...args: any[]) => compObj[key][fd](...args);
286
+ //#region other
287
+ //const memo = (...args: any[]) => compObj[key][fd](...args);
288
+ //Object.defineProperty(compObj, fd, {
289
+ // get:()=>memo,
290
+ // set:()=>SLogger.warn(new Error(`试图修改一个由 composeClassPart 组合的函数字段:${String(fd)}, 修改已被忽略`)),
291
+ // enumerable:true ,
292
+ // //writable: true ,
293
+ // configurable: true
294
+ //});
292
295
  //(compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
293
296
  //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
297
+ //#endregion
294
298
  } else {
295
299
  Object.defineProperty(compObj, fd, {
296
300
  get: ()=>compObj[key][fd],
@@ -304,8 +308,8 @@ static composeClassPart
304
308
  return compObj;
305
309
  }
306
310
 
307
- /**根据 MIXIN_FIELDS 自动混入
308
- * @deprecated 请使用 composeRefMixinable
311
+ /**根据 MIXIN_FIELDS 自动混入
312
+ * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
309
313
  */
310
314
  static composeMixinable
311
315
  <Base extends object, Mixins extends Mixinable<any>[]>
@@ -331,7 +335,7 @@ static composeMixinable
331
335
  static composeRefMixinable
332
336
  <Base extends object, Mixins extends RefMixinable<any,unknown>[]>
333
337
  (base:Base,...mixins:Mixins):
334
- ComposedCtorMixinable<Base,Mixins>{
338
+ ComposedRefMixinable<Base,Mixins>{
335
339
  let out = base;
336
340
  const fieldsSet = new Set<string>();
337
341
  for(const mixin of mixins){
@@ -152,8 +152,8 @@ export type ReflectionAble<Ctor,Constraint={}> = {
152
152
  /**原型构造器 */
153
153
  readonly CTOR:Ctor&Constraint
154
154
  };
155
- /**构造函数是可混入的类
156
- * @template Ctor - 构造器类型
155
+ /**可反射的 且 构造函数是可混入的类
156
+ * @template Ctor - 构造器类型
157
157
  * @template Instance - 实例类型
158
158
  * @template Constraint - 构造器约束
159
159
  */
@@ -161,7 +161,8 @@ export type RefMixinable<Ctor,Instance,Constraint={}> =
161
161
  ReflectionAble<Ctor,Mixinable<Instance>&Constraint>;
162
162
 
163
163
 
164
- /**自动组合可混入的类 */
164
+ /**自动组合可混入的类
165
+ */
165
166
  export type ComposedMixinable<B, Ms extends unknown[]> =
166
167
  Ms extends [infer M, ...infer Rest]
167
168
  ? M extends Mixinable<M>
@@ -169,11 +170,11 @@ export type ComposedMixinable<B, Ms extends unknown[]> =
169
170
  : "一个混入类没有实现 Mixinable<self>" & Error
170
171
  : B
171
172
 
172
- /**自动组合构造类型是可混入的类 */
173
- export type ComposedCtorMixinable<B, Ms extends unknown[]> =
173
+ /**自动组合 可反射的 且 构造函数是可混入的 类 */
174
+ export type ComposedRefMixinable<B, Ms extends unknown[]> =
174
175
  Ms extends [infer M, ...infer Rest]
175
176
  ? M extends RefMixinable<unknown,M>
176
- ? ComposedCtorMixinable<ComposedClass<B,M,
177
+ ? ComposedRefMixinable<ComposedClass<B,M,
177
178
  M['CTOR']['MIXIN_KEY'],
178
179
  M['CTOR']['MIXIN_FIELDS'][number]>,
179
180
  Rest>
@@ -196,8 +197,7 @@ export type ExtendThen<B,T,Y,N = never> = B extends T ? Y : N;
196
197
  */
197
198
  type UnionInclude<B,T,Y,N = never> =
198
199
  B extends Exclude<B,T>
199
- ? N
200
- : Y
200
+ ? N : Y
201
201
  /**排除可选字段 */
202
202
  export type RequiredOnly<T> = {
203
203
  [K in keyof T as