@zwa73/utils 1.0.75 → 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.
- package/dist/UtilFunctions.d.ts +9 -1
- package/dist/UtilFunctions.js +22 -0
- package/dist/test/composeTest.js +7 -0
- package/package.json +1 -1
- package/src/UtilFunctions.ts +22 -1
- package/src/test/composeTest.ts +14 -0
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -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
|
/**获取当前时间戳
|
|
@@ -13,6 +13,14 @@ export declare namespace UtilFunc {
|
|
|
13
13
|
* @returns 最终值
|
|
14
14
|
*/
|
|
15
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
|
*/
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -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
|
*/
|
package/dist/test/composeTest.js
CHANGED
package/package.json
CHANGED
package/src/UtilFunctions.ts
CHANGED
|
@@ -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
|
|
|
@@ -28,6 +28,27 @@ export function initField<T extends object,K extends keyof T>(
|
|
|
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
|
*/
|
package/src/test/composeTest.ts
CHANGED
|
@@ -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//?
|