@zwa73/utils 1.0.79 → 1.0.80

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,15 +1,15 @@
1
- import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
2
- /**创建一个outcome */
1
+ import { ExtractOutcome, Keyable, Outcome, UnionToIntersection } from "./UtilInterfaces";
2
+ /**创建一个Outcome */
3
3
  export declare function outcome<K extends Keyable, V>(key: K, value: V): Outcome<K, V>;
4
- /**处理联合 简单值
4
+ /**处理联合值
5
5
  * @param t - 目标值
6
6
  * @param procObj - 所有可能的id组成的处理函数映射
7
7
  * @returns 任意处理函数的返回值
8
8
  */
9
- export declare function match<T extends Keyable | Outcome<Keyable, unknown>, P extends (T extends Keyable ? {
9
+ export declare function match<T extends Keyable | Outcome<Keyable, unknown>, P extends UnionToIntersection<(T extends Keyable ? {
10
10
  [K in T]: (k: K) => unknown;
11
11
  } : T extends Outcome<Keyable, unknown> ? {
12
12
  [K in T['status']]: (k: K, v: ExtractOutcome<T, K>['result']) => unknown;
13
- } : never)>(t: T, procObj: P): P extends Record<any, (...args: any) => any> ? {
13
+ } : never)>>(t: T, procObj: P): P extends Record<any, (...args: any) => any> ? {
14
14
  [K in keyof P]: ReturnType<P[K]>;
15
15
  }[keyof P] : never;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.match = exports.outcome = void 0;
4
- /**创建一个outcome */
4
+ /**创建一个Outcome */
5
5
  function outcome(key, value) {
6
6
  return { status: key, result: value };
7
7
  }
8
8
  exports.outcome = outcome;
9
- /**处理联合 简单值
9
+ /**处理联合值
10
10
  * @param t - 目标值
11
11
  * @param procObj - 所有可能的id组成的处理函数映射
12
12
  * @returns 任意处理函数的返回值
@@ -131,7 +131,7 @@ class UtilFunc {
131
131
  repeatTime *= 1000;
132
132
  //验证处理函数
133
133
  if (verifyFn === undefined)
134
- verifyFn = () => UtilSymbol_1.Completed;
134
+ verifyFn = () => UtilSymbol_1.Success;
135
135
  //进行中的请求
136
136
  const plist = [];
137
137
  //开始处理
@@ -164,7 +164,7 @@ class UtilFunc {
164
164
  //路由请求状态
165
165
  const postresult = currObj.result;
166
166
  const result = (0, QuickFunction_1.match)(postresult.stat, {
167
- [UtilSymbol_1.Completed]() {
167
+ [UtilSymbol_1.Success]() {
168
168
  UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 repeatPromise 成功`);
169
169
  //非当前
170
170
  if (postresult.index != i)
@@ -1,4 +1,4 @@
1
- import { Completed, Failed, Terminated } from "./UtilSymbol";
1
+ import { Failed, Success, Terminated } from "./UtilSymbol";
2
2
  /**可以序列化为JSON文件的对象 */
3
3
  export type JToken = JObject | JArray | JValue | IJData;
4
4
  /**在stringify输出时 undefine 会被转为 null */
@@ -32,6 +32,11 @@ export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> = R[
32
32
  * (string&String)
33
33
  */
34
34
  export type AnyString = (string & {});
35
+ /**联合类型转为交叉类型
36
+ * 将联合类型映射为联合函数
37
+ * 再取得可以传递给任意联合函数的类型
38
+ */
39
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
35
40
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
36
41
  * 以及一个名为 K[N] 类型为 T 的新属性。
37
42
  * 所有 K 中非 K[N] 的其他属性都是可选的并且不能被赋值(因为它们的类型是 never)。
@@ -56,7 +61,7 @@ export type ExclusiveJObject<B extends JObject, T extends JToken, K extends stri
56
61
  * 终止 将直接返回 null
57
62
  * 失败 将重试
58
63
  */
59
- export type PromiseStat = Completed | Failed | Terminated;
64
+ export type PromiseStat = Success | Failed | Terminated;
60
65
  /**promise验证函数 */
61
66
  export type PromiseVerifyFn<T> = (obj: T) => Promise<PromiseStat> | PromiseStat;
62
67
  /**类型中任意函数的字符串名称 */
@@ -1,6 +1,9 @@
1
1
  /**完成 */
2
2
  export declare const Completed: unique symbol;
3
3
  export type Completed = typeof Completed;
4
+ /**成功 */
5
+ export declare const Success: unique symbol;
6
+ export type Success = typeof Success;
4
7
  /**失败 */
5
8
  export declare const Failed: unique symbol;
6
9
  export type Failed = typeof Failed;
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Timeout = exports.None = exports.Terminated = exports.Failed = exports.Completed = void 0;
3
+ exports.Timeout = exports.None = exports.Terminated = exports.Failed = exports.Success = exports.Completed = void 0;
4
4
  /**完成 */
5
5
  exports.Completed = Symbol("Completed");
6
+ /**成功 */
7
+ exports.Success = Symbol("Success");
6
8
  /**失败 */
7
9
  exports.Failed = Symbol("Failed");
8
10
  /**终止 */
@@ -11,7 +11,7 @@ async function mockAsyncFunction() {
11
11
  }
12
12
  // 验证函数,只有当结果为 "Success" 时才认为成功
13
13
  function mockVerifyFunction(result) {
14
- return result === "Success" ? __1.Completed : __1.Failed;
14
+ return result === "Success" ? __1.Success : __1.Failed;
15
15
  }
16
16
  // 测试 repeatPromise 函数
17
17
  __1.UtilFunc.repeatPromise(mockAsyncFunction, mockVerifyFunction, 3, 10)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.79",
3
+ "version": "1.0.80",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,22 +1,23 @@
1
- import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
1
+ import { ExtractOutcome, Keyable, Outcome, UnionToIntersection } from "./UtilInterfaces";
2
2
 
3
- /**创建一个outcome */
3
+ /**创建一个Outcome */
4
4
  export function outcome<K extends Keyable,V> (key:K,value:V):Outcome<K,V>{
5
5
  return {status:key,result:value}
6
6
  }
7
7
 
8
- /**处理联合 简单值
8
+
9
+ /**处理联合值
9
10
  * @param t - 目标值
10
11
  * @param procObj - 所有可能的id组成的处理函数映射
11
12
  * @returns 任意处理函数的返回值
12
13
  */
13
14
  export function match<
14
15
  T extends Keyable | Outcome<Keyable, unknown>,
15
- P extends (T extends Keyable
16
+ P extends UnionToIntersection<(T extends Keyable
16
17
  ? {[K in T]:(k:K)=>unknown}
17
18
  : T extends Outcome<Keyable,unknown>
18
- ? {[K in T['status']]:(k:K,v:ExtractOutcome<T,K>['result'])=>unknown}
19
- : never)>
19
+ ? { [K in T['status']] : (k:K,v:ExtractOutcome<T,K>['result'])=>unknown }
20
+ : never)>>
20
21
  (t:T, procObj:P):
21
22
  P extends Record<any,(...args:any)=>any>
22
23
  ? {[K in keyof P]: ReturnType<P[K]>}[keyof P]
@@ -2,7 +2,7 @@ import * as crypto from "crypto";
2
2
  import { ComposedClass, ComposedMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Mixinable, Outcome, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
4
  import { SLogger } from "@src/UtilLogger";
5
- import { Completed, Failed, None, Terminated, Timeout } from "./UtilSymbol";
5
+ import { Completed, Failed, None, Success, Terminated, Timeout } from "./UtilSymbol";
6
6
  import { match, outcome } from "./QuickFunction";
7
7
  import { LogTimeAsync } from "./UtilDecorators";
8
8
 
@@ -160,7 +160,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>
160
160
 
161
161
  //验证处理函数
162
162
  if(verifyFn===undefined)
163
- verifyFn = ()=>Completed;
163
+ verifyFn = ()=>Success;
164
164
 
165
165
  //进行中的请求
166
166
  const plist:Promise<
@@ -202,7 +202,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>
202
202
  //路由请求状态
203
203
  const postresult = currObj.result;
204
204
  const result = match(postresult.stat,{
205
- [Completed](){
205
+ [Success](){
206
206
  SLogger.info(`第 ${postresult.index+1} 次 repeatPromise 成功`);
207
207
  //非当前
208
208
  if(postresult.index!=i)
@@ -1,4 +1,4 @@
1
- import { Completed, Failed, Terminated } from "./UtilSymbol";
1
+ import { Completed, Failed, Success, Terminated } from "./UtilSymbol";
2
2
 
3
3
 
4
4
  /**可以序列化为JSON文件的对象 */
@@ -40,7 +40,16 @@ export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> =
40
40
  */
41
41
  export type AnyString = (string&{});
42
42
 
43
-
43
+ /**联合类型转为交叉类型
44
+ * 将联合类型映射为联合函数
45
+ * 再取得可以传递给任意联合函数的类型
46
+ */
47
+ export type UnionToIntersection<U> =
48
+ (U extends any
49
+ ? (k: U)=>void
50
+ : never) extends ((k: infer I)=>void)
51
+ ? I
52
+ : never
44
53
 
45
54
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
46
55
  * 以及一个名为 K[N] 类型为 T 的新属性。
@@ -71,7 +80,7 @@ export type ExclusiveJObject<B extends JObject,T extends JToken,K extends string
71
80
  * 终止 将直接返回 null
72
81
  * 失败 将重试
73
82
  */
74
- export type PromiseStat = Completed|Failed|Terminated;
83
+ export type PromiseStat = Success|Failed|Terminated;
75
84
  /**promise验证函数 */
76
85
  export type PromiseVerifyFn<T> = (obj:T)=>Promise<PromiseStat>|PromiseStat;
77
86
 
package/src/UtilSymbol.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  /**完成 */
2
2
  export const Completed = Symbol("Completed");
3
3
  export type Completed = typeof Completed;
4
+ /**成功 */
5
+ export const Success = Symbol("Success");
6
+ export type Success = typeof Success;
4
7
  /**失败 */
5
8
  export const Failed = Symbol("Failed");
6
9
  export type Failed = typeof Failed;
@@ -1,4 +1,4 @@
1
- import { Completed, Failed, PromiseStat, PromiseVerifyFn, UtilFunc } from "..";
1
+ import { Completed, Failed, PromiseStat, PromiseVerifyFn, Success, UtilFunc } from "..";
2
2
 
3
3
  // 模拟的异步函数,有一定几率失败
4
4
  async function mockAsyncFunction(): Promise<string> {
@@ -11,7 +11,7 @@ async function mockAsyncFunction(): Promise<string> {
11
11
 
12
12
  // 验证函数,只有当结果为 "Success" 时才认为成功
13
13
  function mockVerifyFunction(result: string): PromiseStat {
14
- return result === "Success" ? Completed : Failed;
14
+ return result === "Success" ? Success : Failed;
15
15
  }
16
16
 
17
17
  // 测试 repeatPromise 函数