@zwa73/utils 1.0.74 → 1.0.76

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,4 @@
1
- import { ComposedClass, ComposedMixinable, JToken, Mixinable, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
1
+ import { ComposedClass, ComposedMixinable, JObject, JToken, Mixinable, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
2
2
  /**常用函数 */
3
3
  export declare namespace UtilFunc {
4
4
  /**获取当前时间戳
@@ -12,7 +12,15 @@ export declare namespace UtilFunc {
12
12
  * @param defaultVal - 默认值
13
13
  * @returns 最终值
14
14
  */
15
- function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
15
+ function initField<T extends object, K extends keyof T>(obj: T, field: K, defaultVal: T[K]): T[K];
16
+ /**初始化一个数据对象
17
+ * @param obj - 目标对象
18
+ * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
19
+ * @returns 完成初始化的对象
20
+ */
21
+ function initObject<T extends JObject>(obj: T, checkObj: {
22
+ [P in keyof T]: T[P] | (() => void | T[P]);
23
+ }): any;
16
24
  /**生成一串uuid
17
25
  * @returns uuid
18
26
  */
@@ -27,6 +27,28 @@ var UtilFunc;
27
27
  return obj[field];
28
28
  }
29
29
  UtilFunc.initField = initField;
30
+ /**初始化一个数据对象
31
+ * @param obj - 目标对象
32
+ * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
33
+ * @returns 完成初始化的对象
34
+ */
35
+ function initObject(obj, checkObj) {
36
+ const fixobj = obj;
37
+ for (const key in checkObj) {
38
+ if (obj[key] !== undefined)
39
+ continue;
40
+ const checkVal = checkObj[key];
41
+ if (typeof checkVal === "function") {
42
+ const val = checkVal();
43
+ if (val !== undefined)
44
+ fixobj[key] = val;
45
+ }
46
+ else
47
+ fixobj[key] = checkVal;
48
+ }
49
+ return fixobj;
50
+ }
51
+ UtilFunc.initObject = initObject;
30
52
  /**生成一串uuid
31
53
  * @returns uuid
32
54
  */
@@ -86,3 +86,10 @@ insd.getB(); //?
86
86
  insd.getNum(); //?
87
87
  insd.__a = new A(3);
88
88
  insd.getNum(); //?
89
+ let jot = {
90
+ c: 1
91
+ };
92
+ __1.UtilFunc.initObject(jot, {
93
+ c: () => console.log("缺少c" + insd.getNum())
94
+ });
95
+ jot; //?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.74",
3
+ "version": "1.0.76",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { ComposedClass, ComposedMixinable, FuncPropNames, JToken, Mixinable, PromiseProcFn, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
2
+ import { ComposedClass, ComposedMixinable, FuncPropNames, JObject, JToken, Mixinable, PromiseProcFn, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
4
  import { SLogger } from "@src/UtilLogger";
5
5
 
@@ -19,15 +19,36 @@ export function getTime(): number {
19
19
  * @param defaultVal - 默认值
20
20
  * @returns 最终值
21
21
  */
22
- export function initField<T>(
23
- obj: Record<string, T>,
24
- field: string,
25
- defaultVal: T
26
- ): T {
22
+ export function initField<T extends object,K extends keyof T>(
23
+ obj: T,
24
+ field: K,
25
+ defaultVal: T[K]
26
+ ): T[K] {
27
27
  if (!(field in obj)) obj[field] = defaultVal;
28
28
  return obj[field];
29
29
  }
30
30
 
31
+ /**初始化一个数据对象
32
+ * @param obj - 目标对象
33
+ * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
34
+ * @returns 完成初始化的对象
35
+ */
36
+ export function initObject<T extends JObject>
37
+ (obj:T,checkObj:{[P in keyof T]:T[P]|(()=>void|T[P])}){
38
+ const fixobj = obj as any;
39
+ for(const key in checkObj){
40
+ if(obj[key]!==undefined) continue;
41
+
42
+ const checkVal = checkObj[key];
43
+ if(typeof checkVal === "function"){
44
+ const val = checkVal();
45
+ if(val!==undefined) fixobj[key] = val;
46
+ }
47
+ else fixobj[key] = checkVal;
48
+ }
49
+ return fixobj
50
+ }
51
+
31
52
  /**生成一串uuid
32
53
  * @returns uuid
33
54
  */
@@ -91,3 +91,17 @@ insd.getNum()//?
91
91
  insd.__a = new A(3);
92
92
  insd.getNum()//?
93
93
 
94
+
95
+
96
+ type jo = {
97
+ a?:number,
98
+ b?:string,
99
+ c:number,
100
+ }
101
+ let jot = {
102
+ c:1
103
+ } as jo;
104
+ UtilFunc.initObject(jot,{
105
+ c:()=>console.log("缺少c"+insd.getNum())
106
+ })
107
+ jot//?