@zwa73/utils 1.0.165 → 1.0.166

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.
@@ -1,4 +1,5 @@
1
- import { Literal } from "./UtilInterfaces";
1
+ import { Keyable, Literal } from "./UtilInterfaces";
2
+ import Handlebars from 'handlebars';
2
3
  /**函数管道器 */
3
4
  export declare class Piper<Arg, Result = Arg> {
4
5
  /**管道函数列表 */
@@ -69,4 +70,20 @@ export declare class Stream<T> implements Iterable<T> {
69
70
  [Symbol.iterator](): Iterator<T>;
70
71
  get length(): number;
71
72
  }
73
+ /**文本模板渲染器
74
+ * @template T - 上下文对象的类型,默认为记录类型
75
+ */
76
+ export declare class Hbs<T extends Record<Keyable, any> = Record<Keyable, any>> {
77
+ hbs: typeof Handlebars;
78
+ context: T;
79
+ /**创建一个新的 Hbs 实例
80
+ * @param context - 上下文对象,用于在渲染模板时提供数据
81
+ */
82
+ constructor(context?: T);
83
+ /**渲染模板
84
+ * @param template - 要渲染的 Handlebars 模板字符串
85
+ * @returns 渲染后的字符串
86
+ */
87
+ render(template: string): string;
88
+ }
72
89
  export {};
package/dist/UtilClass.js CHANGED
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Stream = exports.Piper = void 0;
6
+ exports.Hbs = exports.Stream = exports.Piper = void 0;
4
7
  const QuickFunction_1 = require("./QuickFunction");
8
+ const handlebars_1 = __importDefault(require("handlebars"));
9
+ const UtilLogger_1 = require("./UtilLogger");
5
10
  /**函数管道器 */
6
11
  class Piper {
7
12
  /**管道函数列表 */
@@ -188,3 +193,44 @@ class Stream {
188
193
  }
189
194
  }
190
195
  exports.Stream = Stream;
196
+ /**文本模板渲染器
197
+ * @template T - 上下文对象的类型,默认为记录类型
198
+ */
199
+ class Hbs {
200
+ hbs = handlebars_1.default.create();
201
+ context;
202
+ /**创建一个新的 Hbs 实例
203
+ * @param context - 上下文对象,用于在渲染模板时提供数据
204
+ */
205
+ constructor(context = {}) {
206
+ this.context = context;
207
+ this.hbs.registerHelper('def', (name, options) => {
208
+ const value = options.fn(this.context).trim();
209
+ this.context[name] = value;
210
+ return '';
211
+ });
212
+ this.hbs.registerHelper('defli', (name, options) => {
213
+ const rawvalue = options.fn();
214
+ const values = rawvalue
215
+ .split('[[br]]')
216
+ .map(value => value.trim())
217
+ .map(value => this.hbs.compile(value)(this.context));
218
+ this.context[name] = values;
219
+ return '';
220
+ });
221
+ }
222
+ /**渲染模板
223
+ * @param template - 要渲染的 Handlebars 模板字符串
224
+ * @returns 渲染后的字符串
225
+ */
226
+ render(template) {
227
+ try {
228
+ return this.hbs.compile(template)(this.context);
229
+ }
230
+ catch (e) {
231
+ UtilLogger_1.SLogger.error(e);
232
+ return '';
233
+ }
234
+ }
235
+ }
236
+ exports.Hbs = Hbs;
@@ -1,4 +1,4 @@
1
- import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, Await, AnyRecord, AllExtends } from "./UtilInterfaces";
1
+ import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, AnyRecord, AllExtends } from "./UtilInterfaces";
2
2
  import { LogLevel } from "./UtilLogger";
3
3
  import { Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
4
  declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
@@ -222,10 +222,6 @@ export declare class UtilFunc {
222
222
  * @returns 是否安全
223
223
  */
224
224
  static isSafeNumber(num: unknown): boolean;
225
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
226
- static bindTo<K extends Keyable, V, B extends AnyRecord = {}>(key: Literal<K>, value: V, base?: B): keyof B extends K ? "Base中已有对应键" & Error : {
227
- [P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;
228
- };
229
225
  /**移除多行字符串中每行开始的最小空格数。
230
226
  *
231
227
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -269,7 +265,7 @@ export declare class UtilFunc {
269
265
  * @param func - 待转换函数
270
266
  * @returns 转换完成的函数
271
267
  */
272
- static taskEitherize<T extends (...args: any) => Promise<any>>(func: T): (...args: Parameters<T>) => Promise<Outcome<typeof Failed, Error> | Outcome<typeof Success, Await<ReturnType<T>>>>;
268
+ static taskEitherize<T extends (...args: any) => Promise<any>>(func: T): (...args: Parameters<T>) => Promise<Outcome<typeof Failed, Error> | Outcome<typeof Success, Awaited<ReturnType<T>>>>;
273
269
  /**动态导入模块的函数。这个函数是为了在TypeScript的模块系统配置为CommonJS时,防止动态import被转译为require而设计的。
274
270
  * 使用这个函数,你可以在TypeScript中动态地导入模块,而不需要担心import()被转译为require()。
275
271
  * 请注意,这个函数使用了eval(),可能会带来安全风险。
@@ -543,13 +543,6 @@ class UtilFunc {
543
543
  }
544
544
  return false;
545
545
  }
546
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
547
- static bindTo(key, value, base) {
548
- let out = base;
549
- out = out ?? {};
550
- out[key] = value;
551
- return out;
552
- }
553
546
  /**移除多行字符串中每行开始的最小空格数。
554
547
  *
555
548
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -101,7 +101,9 @@ export type ExclusiveJObject<B extends JObject, T extends JToken, K extends stri
101
101
  export type ReqStat = Success | Failed | Terminated;
102
102
  /**请求状态验证函数 */
103
103
  export type ReqVerifyFn<T> = (obj: T) => Promise<ReqStat> | ReqStat;
104
- /**获取Promise的结果类型 如同await那样 */
104
+ /**获取Promise的结果类型 如同await那样
105
+ * @deprecated 请使用官方的 Awaited<T> 类型
106
+ */
105
107
  export type Await<T> = T extends Promise<infer U> ? U : T;
106
108
  /**类型中任意函数的字符串名称 */
107
109
  export type FuncPropNames<T> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.165",
3
+ "version": "1.0.166",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "@deepkit/type": "^1.0.1-alpha.153",
20
20
  "fluent-ffmpeg": "2.1.2",
21
21
  "glob": "^10.4.1",
22
+ "handlebars": "^4.7.8",
22
23
  "html-entities": "^2.3.3",
23
24
  "http-proxy-agent": "^5.0.0",
24
25
  "https-proxy-agent": "^5.0.1",
package/src/UtilClass.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { matchProc } from "./QuickFunction";
2
- import { Literal } from "./UtilInterfaces";
2
+ import { Keyable, Literal } from "./UtilInterfaces";
3
+ import Handlebars from 'handlebars';
4
+ import { SLogger } from "./UtilLogger";
3
5
 
4
6
  /**函数管道器 */
5
7
  export class Piper<Arg, Result = Arg> {
@@ -194,4 +196,49 @@ export class Stream<T> implements Iterable<T>{
194
196
  get length(): number {
195
197
  return this._list.length;
196
198
  }
197
- }
199
+ }
200
+
201
+ /**文本模板渲染器
202
+ * @template T - 上下文对象的类型,默认为记录类型
203
+ */
204
+ export class Hbs<T extends Record<Keyable,any> = Record<Keyable,any>>{
205
+ hbs = Handlebars.create();
206
+ context:T;
207
+
208
+ /**创建一个新的 Hbs 实例
209
+ * @param context - 上下文对象,用于在渲染模板时提供数据
210
+ */
211
+ constructor(context:T={} as T) {
212
+ this.context = context;
213
+
214
+ this.hbs.registerHelper('def', (name, options)=> {
215
+ const value = options.fn(this.context).trim();
216
+ (this.context as any)[name] = value;
217
+ return '';
218
+ });
219
+
220
+ this.hbs.registerHelper('defli', (name,options)=> {
221
+ const rawvalue = options.fn() as string;
222
+
223
+ const values = rawvalue
224
+ .split('[[br]]')
225
+ .map(value => value.trim())
226
+ .map(value => this.hbs.compile(value)(this.context));
227
+ (this.context as any)[name] = values;
228
+ return '';
229
+ });
230
+ }
231
+
232
+ /**渲染模板
233
+ * @param template - 要渲染的 Handlebars 模板字符串
234
+ * @returns 渲染后的字符串
235
+ */
236
+ render(template:string):string{
237
+ try{
238
+ return this.hbs.compile(template)(this.context);
239
+ } catch(e){
240
+ SLogger.error(e);
241
+ return '';
242
+ }
243
+ }
244
+ }
@@ -1,5 +1,5 @@
1
1
  import { UtilFunc } from "./UtilFunctions";
2
- import { Await, NeedInit } from "./UtilInterfaces";
2
+ import { NeedInit } from "./UtilInterfaces";
3
3
  import { SLogger } from "./UtilLogger";
4
4
 
5
5
  type TDTg<T> =
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqStat, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, Await, AnyRecord, AllExtends } from "@src/UtilInterfaces";
2
+ import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqStat, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, AnyRecord, AllExtends } 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";
@@ -651,16 +651,6 @@ static isSafeNumber(num: unknown): boolean {
651
651
  return false;
652
652
  }
653
653
 
654
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
655
- static bindTo<K extends Keyable, V, B extends AnyRecord = {}>
656
- (key: Literal<K>,value: V, base?:B):keyof B extends K
657
- ? "Base中已有对应键"&Error
658
- : {[P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;}{
659
- let out = base as any;
660
- out = out??{};
661
- out[key]=value;
662
- return out;
663
- }
664
654
  /**移除多行字符串中每行开始的最小空格数。
665
655
  *
666
656
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -754,7 +744,7 @@ static eitherize<T extends AnyFunc>(func:T) {
754
744
  static taskEitherize<T extends (...args:any)=>Promise<any>>(func:T) {
755
745
  return async (...args: Parameters<T>) => {
756
746
  try {
757
- const result:Await<ReturnType<T>> = await func(...args as any);
747
+ const result:Awaited<ReturnType<T>> = await func(...args as any);
758
748
  return UtilFunc.outcome(Success,result);
759
749
  } catch (error) {
760
750
  if(error instanceof Error)
@@ -149,9 +149,12 @@ export type ReqStat = Success|Failed|Terminated;
149
149
  /**请求状态验证函数 */
150
150
  export type ReqVerifyFn<T> = (obj:T)=>Promise<ReqStat>|ReqStat;
151
151
 
152
- /**获取Promise的结果类型 如同await那样 */
152
+ /**获取Promise的结果类型 如同await那样
153
+ * @deprecated 请使用官方的 Awaited<T> 类型
154
+ */
153
155
  export type Await<T> = T extends Promise<infer U> ? U : T;
154
156
 
157
+
155
158
  /**类型中任意函数的字符串名称 */
156
159
  export type FuncPropNames<T> = {
157
160
  [K in keyof T]: T[K] extends AnyFunc ? K : never;