@zcrkey/js-utils 0.0.5 → 0.0.7

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.
Files changed (55) hide show
  1. package/README.md +9 -0
  2. package/dist/cjs/color.d.ts +195 -0
  3. package/dist/cjs/color.js +353 -0
  4. package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
  5. package/dist/cjs/index.d.ts +9 -0
  6. package/dist/cjs/index.js +54 -0
  7. package/dist/cjs/obj.js +37 -0
  8. package/{src/storage.ts → dist/cjs/storage.js} +118 -101
  9. package/dist/cjs/tree.js +145 -0
  10. package/dist/{util.d.ts → cjs/util.d.ts} +3 -3
  11. package/{src/util.ts → dist/cjs/util.js} +173 -257
  12. package/dist/esm/color.d.ts +195 -0
  13. package/dist/esm/color.js +500 -0
  14. package/dist/esm/eventCenter.d.ts +59 -0
  15. package/dist/esm/index.d.ts +9 -0
  16. package/dist/esm/index.js +6 -0
  17. package/dist/esm/obj.d.ts +12 -0
  18. package/dist/{objUtil.js → esm/obj.js} +6 -6
  19. package/dist/esm/storage.d.ts +44 -0
  20. package/dist/esm/tree.d.ts +48 -0
  21. package/dist/esm/util.d.ts +209 -0
  22. package/dist/{util.js → esm/util.js} +129 -129
  23. package/dist/{index.umd.js → umd/index.umd.js} +1 -1
  24. package/package.json +15 -4
  25. package/.dumi/global.less +0 -1396
  26. package/.dumirc.ts +0 -36
  27. package/.fatherrc.ts +0 -14
  28. package/.husky/commit-msg +0 -4
  29. package/.husky/pre-commit +0 -4
  30. package/.prettierignore +0 -2
  31. package/.stylelintrc +0 -10
  32. package/dist/index.d.ts +0 -8
  33. package/dist/index.js +0 -5
  34. package/docs/api/eventCenter/index.md +0 -34
  35. package/docs/api/index.md +0 -5
  36. package/docs/api/storage/index.md +0 -9
  37. package/docs/api/storage/local.tsx +0 -91
  38. package/docs/api/storage/session.tsx +0 -85
  39. package/docs/api/treeUtil/index.md +0 -5
  40. package/docs/api/treeUtil/index.tsx +0 -266
  41. package/docs/api/util/index.md +0 -6
  42. package/docs/api/util/index.tsx +0 -405
  43. package/docs/api/util/is.tsx +0 -196
  44. package/docs/guide.md +0 -24
  45. package/src/index.ts +0 -8
  46. package/src/objUtil.ts +0 -20
  47. package/src/treeUtil.ts +0 -164
  48. package/tsconfig.json +0 -18
  49. /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
  50. /package/dist/{objUtil.d.ts → cjs/obj.d.ts} +0 -0
  51. /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
  52. /package/dist/{treeUtil.d.ts → cjs/tree.d.ts} +0 -0
  53. /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
  54. /package/dist/{storage.js → esm/storage.js} +0 -0
  55. /package/dist/{treeUtil.js → esm/tree.js} +0 -0
@@ -0,0 +1,145 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/tree.ts
20
+ var tree_exports = {};
21
+ __export(tree_exports, {
22
+ default: () => CrTreeUtil
23
+ });
24
+ module.exports = __toCommonJS(tree_exports);
25
+ var import_lodash = require("lodash");
26
+ var CrTreeUtil = class {
27
+ /**
28
+ * 列表数据转树形数据
29
+ * @param list
30
+ * @param options
31
+ * @param options.idField 默认值 id
32
+ * @param options.parentIdField 默认值 parentId
33
+ * @param options.childrenField 默认值 children
34
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
35
+ * @param options.getData 处理数据
36
+ * @returns
37
+ */
38
+ static listToTree(list, options) {
39
+ const {
40
+ idField = "id",
41
+ parentIdField = "parentId",
42
+ childrenField = "children",
43
+ isCloneDeep = true,
44
+ getData = (item) => item
45
+ } = options || {};
46
+ const listData = isCloneDeep ? (0, import_lodash.cloneDeep)(list) : list;
47
+ const treeData = [];
48
+ const nodeMap = /* @__PURE__ */ new Map();
49
+ for (const item of listData) {
50
+ const id = item[idField];
51
+ const node = {
52
+ ...getData(item),
53
+ __origin_id: id,
54
+ __origin_pid: item[parentIdField]
55
+ };
56
+ nodeMap.set(id, node);
57
+ }
58
+ for (const node of nodeMap.values()) {
59
+ const parentId = node.__origin_pid;
60
+ if (parentId === void 0 || parentId === null || parentId === 0 || !nodeMap.has(parentId)) {
61
+ treeData.push(node);
62
+ } else {
63
+ const parentNode = nodeMap.get(parentId);
64
+ if (!parentNode[childrenField]) {
65
+ parentNode[childrenField] = [];
66
+ }
67
+ parentNode[childrenField].push(node);
68
+ }
69
+ }
70
+ const clean = (nodes) => {
71
+ for (const node of nodes) {
72
+ delete node.__origin_id;
73
+ delete node.__origin_pid;
74
+ if (node[childrenField]) {
75
+ clean(node[childrenField]);
76
+ }
77
+ }
78
+ };
79
+ clean(treeData);
80
+ return treeData;
81
+ }
82
+ /**
83
+ * 树形数据转列表数据
84
+ * @param tree
85
+ * @param options
86
+ * @param options.childrenField 默认值 children
87
+ * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
88
+ * @returns
89
+ */
90
+ static treeToList(tree, options) {
91
+ const { childrenField = "children", isCloneDeep = true } = options || {};
92
+ const result = [];
93
+ const queue = [...tree];
94
+ while (queue.length) {
95
+ const node = queue.shift();
96
+ const children = node[childrenField];
97
+ const { [childrenField]: _, ...rest } = node;
98
+ result.push(rest);
99
+ if (children && children.length) {
100
+ queue.push(...children);
101
+ }
102
+ }
103
+ return isCloneDeep ? (0, import_lodash.cloneDeep)(result) : result;
104
+ }
105
+ /**
106
+ * 获取扁平化父数据(包含自身)
107
+ * @param listData
108
+ * @param value
109
+ * @param settings
110
+ * @param settings.valueField 默认值 value
111
+ * @param settings.idField 默认值 id
112
+ * @param settings.parentIdField 默认值 parentId
113
+ * @param settings.getData 过滤数据
114
+ */
115
+ static getFlatParentDatas(listData, value, settings) {
116
+ if (!Array.isArray(listData) || listData.length === 0 || value == null) {
117
+ return [];
118
+ }
119
+ const options = Object.assign(
120
+ {
121
+ valueField: "value",
122
+ idField: "id",
123
+ parentIdField: "parentId",
124
+ getData: (item) => {
125
+ return item;
126
+ }
127
+ },
128
+ settings
129
+ );
130
+ const nodeMap = (0, import_lodash.keyBy)(listData, options.valueField);
131
+ const result = [];
132
+ const visited = /* @__PURE__ */ new Set();
133
+ let current = nodeMap[value];
134
+ while (current && !visited.has((0, import_lodash.get)(current, options.idField))) {
135
+ visited.add((0, import_lodash.get)(current, options.idField));
136
+ result.unshift(options.getData(current));
137
+ const parentId = (0, import_lodash.get)(
138
+ current,
139
+ options.parentIdField
140
+ );
141
+ current = (0, import_lodash.find)(listData, [options.valueField, parentId]);
142
+ }
143
+ return result;
144
+ }
145
+ };
@@ -91,7 +91,7 @@ export default class CrUtil {
91
91
  * @param settings.idField 默认值 id
92
92
  * @param settings.pidField 默认值 parentId
93
93
  * @param settings.childrenField 默认值 children
94
- * @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
94
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.listToTree} 替代
95
95
  * @returns
96
96
  */
97
97
  static listToTreeData(listData: any[], settings?: {
@@ -108,7 +108,7 @@ export default class CrUtil {
108
108
  * @param settings.idField 默认值 id
109
109
  * @param settings.pidField 默认值 parentId
110
110
  * @param settings.childrenField 默认值 children
111
- * @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
111
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.treeToList} 替代
112
112
  * @returns
113
113
  */
114
114
  static treeDataToListData(treeData: any[], pid?: string | number, settings?: {
@@ -125,7 +125,7 @@ export default class CrUtil {
125
125
  * @param settings.valueField 默认值 value
126
126
  * @param settings.idField 默认值 id
127
127
  * @param settings.pidField 默认值 parentId
128
- * @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
128
+ * @deprecated 即将移除,使用 {@link CrTreeUtil.getFlatParentDatas} 替代
129
129
  * @returns
130
130
  */
131
131
  static getParentNodes<T, R extends Record<string, any>>(listData: R[], value: number | string, settings?: {