@nocobase/utils 1.7.0-beta.24 → 1.7.0-beta.26
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/common.d.ts +15 -6
- package/lib/common.js +29 -0
- package/package.json +2 -2
package/lib/common.d.ts
CHANGED
|
@@ -13,13 +13,22 @@ 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
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
* @param {Object|Array} tree -
|
|
18
|
-
* @param {Function} callback -
|
|
19
|
-
* @param {Object} options -
|
|
20
|
-
* @param {string|Function} options.childrenKey -
|
|
21
|
-
* @returns {any|undefined} -
|
|
16
|
+
* Generic tree node depth-first traversal function
|
|
17
|
+
* @param {Object|Array} tree - The tree structure to traverse
|
|
18
|
+
* @param {Function} callback - The callback function executed for each node, stops traversing and returns the current node when a truthy value is returned
|
|
19
|
+
* @param {Object} options - Configuration options
|
|
20
|
+
* @param {string|Function} options.childrenKey - The property name of child nodes, defaults to 'children', can also be a function
|
|
21
|
+
* @returns {any|undefined} - The found node or undefined
|
|
22
22
|
*/
|
|
23
23
|
export declare function treeFind<T = any>(tree: T | T[], callback: (node: T) => boolean, options?: {
|
|
24
24
|
childrenKey?: string | ((node: T) => T[] | undefined);
|
|
25
25
|
}): T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Sort a tree structure
|
|
28
|
+
* @param {Array} tree - Tree structure array
|
|
29
|
+
* @param {string|Function} sortBy - Sort field or sort function
|
|
30
|
+
* @param {string} childrenKey - The key name of child nodes, defaults to 'children'
|
|
31
|
+
* @param {boolean} isAsc - Whether to sort in ascending order, defaults to true
|
|
32
|
+
* @returns {Array} - The sorted tree structure
|
|
33
|
+
*/
|
|
34
|
+
export declare function sortTree(tree: any[], sortBy: string | Function, childrenKey?: string, isAsc?: boolean): any;
|
package/lib/common.js
CHANGED
|
@@ -7,9 +7,11 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
var __create = Object.create;
|
|
10
11
|
var __defProp = Object.defineProperty;
|
|
11
12
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
13
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
13
15
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
16
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
17
|
var __export = (target, all) => {
|
|
@@ -24,6 +26,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
24
26
|
}
|
|
25
27
|
return to;
|
|
26
28
|
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
27
37
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
38
|
var common_exports = {};
|
|
29
39
|
__export(common_exports, {
|
|
@@ -33,9 +43,11 @@ __export(common_exports, {
|
|
|
33
43
|
isPlainObject: () => isPlainObject,
|
|
34
44
|
isString: () => isString,
|
|
35
45
|
nextTick: () => nextTick,
|
|
46
|
+
sortTree: () => sortTree,
|
|
36
47
|
treeFind: () => treeFind
|
|
37
48
|
});
|
|
38
49
|
module.exports = __toCommonJS(common_exports);
|
|
50
|
+
var import_lodash = __toESM(require("lodash"));
|
|
39
51
|
const isString = /* @__PURE__ */ __name((value) => {
|
|
40
52
|
return typeof value === "string";
|
|
41
53
|
}, "isString");
|
|
@@ -96,6 +108,22 @@ function treeFind(tree, callback, options = {}) {
|
|
|
96
108
|
return void 0;
|
|
97
109
|
}
|
|
98
110
|
__name(treeFind, "treeFind");
|
|
111
|
+
function sortTree(tree, sortBy, childrenKey = "children", isAsc = true) {
|
|
112
|
+
if (!tree || !Array.isArray(tree) || tree.length === 0) {
|
|
113
|
+
return tree;
|
|
114
|
+
}
|
|
115
|
+
const sortedTree = import_lodash.default.orderBy(tree, sortBy, isAsc ? "asc" : "desc");
|
|
116
|
+
return sortedTree.map((node) => {
|
|
117
|
+
if (node[childrenKey] && node[childrenKey].length > 0) {
|
|
118
|
+
return {
|
|
119
|
+
...node,
|
|
120
|
+
[childrenKey]: sortTree(node[childrenKey], sortBy, childrenKey, isAsc)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return node;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
__name(sortTree, "sortTree");
|
|
99
127
|
// Annotate the CommonJS export names for ESM import in node:
|
|
100
128
|
0 && (module.exports = {
|
|
101
129
|
hasEmptyValue,
|
|
@@ -104,5 +132,6 @@ __name(treeFind, "treeFind");
|
|
|
104
132
|
isPlainObject,
|
|
105
133
|
isString,
|
|
106
134
|
nextTick,
|
|
135
|
+
sortTree,
|
|
107
136
|
treeFind
|
|
108
137
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/utils",
|
|
3
|
-
"version": "1.7.0-beta.
|
|
3
|
+
"version": "1.7.0-beta.26",
|
|
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.2",
|
|
17
17
|
"object-path": "^0.11.8"
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "01d3e77c1f4a853d1f592ac2d70916010e9c510a"
|
|
20
20
|
}
|