@nocobase/utils 1.7.0-alpha.10 → 1.7.0-alpha.11
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/lib/assign.js +18 -5
- package/lib/common.d.ts +11 -0
- package/lib/common.js +23 -2
- package/lib/date.js +3 -2
- package/lib/dayjs.js +2 -0
- package/package.json +2 -2
package/lib/assign.js
CHANGED
|
@@ -53,8 +53,14 @@ function getKeys(target) {
|
|
|
53
53
|
}
|
|
54
54
|
__name(getKeys, "getKeys");
|
|
55
55
|
const mergeStrategies = /* @__PURE__ */ new Map();
|
|
56
|
-
mergeStrategies.set("overwrite", (
|
|
57
|
-
if (
|
|
56
|
+
mergeStrategies.set("overwrite", (x, y) => {
|
|
57
|
+
if (y === void 0) {
|
|
58
|
+
if (typeof x === "string" && x.includes(",")) {
|
|
59
|
+
return x.split(",");
|
|
60
|
+
}
|
|
61
|
+
return x;
|
|
62
|
+
}
|
|
63
|
+
if (typeof y === "string" && y.includes(",")) {
|
|
58
64
|
y = y.split(",");
|
|
59
65
|
}
|
|
60
66
|
return y;
|
|
@@ -123,15 +129,22 @@ mergeStrategies.set(
|
|
|
123
129
|
})().filter(Boolean)
|
|
124
130
|
);
|
|
125
131
|
function assign(target, source, strategies = {}) {
|
|
126
|
-
getKeys(source)
|
|
132
|
+
const sourceKeys = getKeys(source);
|
|
133
|
+
const targetKeys = getKeys(target);
|
|
134
|
+
import_lodash.default.uniq([...sourceKeys, ...targetKeys]).forEach((sourceKey) => {
|
|
127
135
|
const strategy = strategies[sourceKey];
|
|
128
|
-
let func
|
|
136
|
+
let func;
|
|
129
137
|
if (typeof strategy === "function") {
|
|
130
138
|
func = strategy;
|
|
131
139
|
} else if (typeof strategy === "string" && mergeStrategies.has(strategy)) {
|
|
132
140
|
func = mergeStrategies.get(strategy);
|
|
133
141
|
}
|
|
134
|
-
|
|
142
|
+
if (func) {
|
|
143
|
+
target[sourceKey] = func(target[sourceKey], source[sourceKey]);
|
|
144
|
+
} else if (sourceKeys.includes(sourceKey)) {
|
|
145
|
+
const func2 = mergeStrategies.get("deepMerge");
|
|
146
|
+
target[sourceKey] = func2(target[sourceKey], source[sourceKey]);
|
|
147
|
+
}
|
|
135
148
|
});
|
|
136
149
|
return target;
|
|
137
150
|
}
|
package/lib/common.d.ts
CHANGED
|
@@ -12,3 +12,14 @@ export declare const isEmpty: (value: unknown) => boolean;
|
|
|
12
12
|
export declare const isPlainObject: (value: any) => boolean;
|
|
13
13
|
export declare const hasEmptyValue: (objOrArr: object | any[]) => any;
|
|
14
14
|
export declare const nextTick: (fn: () => void) => void;
|
|
15
|
+
/**
|
|
16
|
+
* 通用树节点深度优先遍历函数
|
|
17
|
+
* @param {Object|Array} tree - 要遍历的树结构
|
|
18
|
+
* @param {Function} callback - 遍历每个节点时执行的回调函数,返回真值时停止遍历并返回当前节点
|
|
19
|
+
* @param {Object} options - 配置选项
|
|
20
|
+
* @param {string|Function} options.childrenKey - 子节点的属性名,默认为'children',也可以是一个函数
|
|
21
|
+
* @returns {any|undefined} - 找到的节点或undefined
|
|
22
|
+
*/
|
|
23
|
+
export declare function treeFind<T = any>(tree: T | T[], callback: (node: T) => boolean, options?: {
|
|
24
|
+
childrenKey?: string | ((node: T) => T[] | undefined);
|
|
25
|
+
}): T | undefined;
|
package/lib/common.js
CHANGED
|
@@ -32,7 +32,8 @@ __export(common_exports, {
|
|
|
32
32
|
isEmpty: () => isEmpty,
|
|
33
33
|
isPlainObject: () => isPlainObject,
|
|
34
34
|
isString: () => isString,
|
|
35
|
-
nextTick: () => nextTick
|
|
35
|
+
nextTick: () => nextTick,
|
|
36
|
+
treeFind: () => treeFind
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(common_exports);
|
|
38
39
|
const isString = /* @__PURE__ */ __name((value) => {
|
|
@@ -76,6 +77,25 @@ const hasEmptyValue = /* @__PURE__ */ __name((objOrArr) => {
|
|
|
76
77
|
const nextTick = /* @__PURE__ */ __name((fn) => {
|
|
77
78
|
setTimeout(fn);
|
|
78
79
|
}, "nextTick");
|
|
80
|
+
function treeFind(tree, callback, options = {}) {
|
|
81
|
+
if (!tree) return void 0;
|
|
82
|
+
const { childrenKey = "children" } = options;
|
|
83
|
+
const nodes = Array.isArray(tree) ? [...tree] : [tree];
|
|
84
|
+
for (const node of nodes) {
|
|
85
|
+
if (callback(node)) {
|
|
86
|
+
return node;
|
|
87
|
+
}
|
|
88
|
+
const children = typeof childrenKey === "function" ? childrenKey(node) : node[childrenKey];
|
|
89
|
+
if (Array.isArray(children) && children.length > 0) {
|
|
90
|
+
const found = treeFind(children, callback, options);
|
|
91
|
+
if (found !== void 0) {
|
|
92
|
+
return found;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return void 0;
|
|
97
|
+
}
|
|
98
|
+
__name(treeFind, "treeFind");
|
|
79
99
|
// Annotate the CommonJS export names for ESM import in node:
|
|
80
100
|
0 && (module.exports = {
|
|
81
101
|
hasEmptyValue,
|
|
@@ -83,5 +103,6 @@ const nextTick = /* @__PURE__ */ __name((fn) => {
|
|
|
83
103
|
isEmpty,
|
|
84
104
|
isPlainObject,
|
|
85
105
|
isString,
|
|
86
|
-
nextTick
|
|
106
|
+
nextTick,
|
|
107
|
+
treeFind
|
|
87
108
|
});
|
package/lib/date.js
CHANGED
|
@@ -88,12 +88,13 @@ const toLocal = /* @__PURE__ */ __name((value) => {
|
|
|
88
88
|
}
|
|
89
89
|
}, "toLocal");
|
|
90
90
|
const convertQuarterToFirstDay = /* @__PURE__ */ __name((quarterStr) => {
|
|
91
|
-
|
|
91
|
+
try {
|
|
92
92
|
const year = parseInt(quarterStr.slice(0, 4));
|
|
93
93
|
const quarter = parseInt(quarterStr.slice(-1));
|
|
94
94
|
return (0, import_dayjs.dayjs)().quarter(quarter).year(year);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return null;
|
|
95
97
|
}
|
|
96
|
-
return null;
|
|
97
98
|
}, "convertQuarterToFirstDay");
|
|
98
99
|
const toMoment = /* @__PURE__ */ __name((val, options) => {
|
|
99
100
|
if (!val) {
|
package/lib/dayjs.js
CHANGED
|
@@ -42,6 +42,7 @@ module.exports = __toCommonJS(dayjs_exports);
|
|
|
42
42
|
var import_dayjs = __toESM(require("dayjs"));
|
|
43
43
|
var import_advancedFormat = __toESM(require("dayjs/plugin/advancedFormat"));
|
|
44
44
|
var import_customParseFormat = __toESM(require("dayjs/plugin/customParseFormat"));
|
|
45
|
+
var import_duration = __toESM(require("dayjs/plugin/duration"));
|
|
45
46
|
var import_isBetween = __toESM(require("dayjs/plugin/isBetween"));
|
|
46
47
|
var import_isSameOrAfter = __toESM(require("dayjs/plugin/isSameOrAfter"));
|
|
47
48
|
var import_isSameOrBefore = __toESM(require("dayjs/plugin/isSameOrBefore"));
|
|
@@ -66,6 +67,7 @@ import_dayjs.default.extend(import_weekOfYear.default);
|
|
|
66
67
|
import_dayjs.default.extend(import_weekYear.default);
|
|
67
68
|
import_dayjs.default.extend(import_customParseFormat.default);
|
|
68
69
|
import_dayjs.default.extend(import_advancedFormat.default);
|
|
70
|
+
import_dayjs.default.extend(import_duration.default);
|
|
69
71
|
// Annotate the CommonJS export names for ESM import in node:
|
|
70
72
|
0 && (module.exports = {
|
|
71
73
|
dayjs
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/utils",
|
|
3
|
-
"version": "1.7.0-alpha.
|
|
3
|
+
"version": "1.7.0-alpha.11",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"multer": "^1.4.5-lts.1",
|
|
17
17
|
"object-path": "^0.11.8"
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "41830f1b20f8f5f7aae0699042a10135f1f8cf30"
|
|
20
20
|
}
|