@zcrkey/js-utils 0.0.4 → 0.0.6
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/README.md +69 -56
- package/dist/cjs/eventCenter.js +120 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +51 -0
- package/dist/cjs/objUtil.d.ts +12 -0
- package/dist/cjs/objUtil.js +37 -0
- package/dist/cjs/storage.js +118 -0
- package/dist/cjs/treeUtil.d.ts +48 -0
- package/dist/cjs/treeUtil.js +145 -0
- package/dist/{util.d.ts → cjs/util.d.ts} +41 -6
- package/dist/cjs/util.js +572 -0
- package/dist/esm/eventCenter.d.ts +59 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/objUtil.d.ts +12 -0
- package/dist/esm/objUtil.js +28 -0
- package/dist/esm/storage.d.ts +44 -0
- package/dist/esm/treeUtil.d.ts +48 -0
- package/dist/esm/treeUtil.js +185 -0
- package/dist/esm/util.d.ts +209 -0
- package/dist/{util.js → esm/util.js} +117 -6
- package/dist/umd/index.umd.js +1 -0
- package/package.json +15 -6
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -3
- package/dist/index.umd.js +0 -1
- /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
- /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
- /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
- /package/dist/{storage.js → esm/storage.js} +0 -0
package/README.md
CHANGED
|
@@ -1,56 +1,69 @@
|
|
|
1
|
-
# @zcrkey/js-utils
|
|
2
|
-
|
|
3
|
-
一个 javascript 实用函数库
|
|
4
|
-
|
|
5
|
-
## 安装和使用
|
|
6
|
-
|
|
7
|
-
### 通过 import 方式安装
|
|
8
|
-
|
|
9
|
-
在开始之前,你可能需要安装 [yarn](https://github.com/yarnpkg/yarn/) 或者 [pnpm](https://pnpm.io/zh/)。
|
|
10
|
-
|
|
11
|
-
从 yarn 或 npm 或 pnpm 安装并引入
|
|
12
|
-
|
|
13
|
-
```sh
|
|
14
|
-
$ yarn add @zcrkey/js-utils
|
|
15
|
-
# or
|
|
16
|
-
$ npm install @zcrkey/js-utils --save
|
|
17
|
-
# or
|
|
18
|
-
$ pnpm install @zcrkey/js-utils --save
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
修改 `src/App.js`,引入 @zcrkey/js-utils 。
|
|
23
|
-
|
|
24
|
-
```jsx
|
|
25
|
-
import React from 'react';
|
|
26
|
-
import { CrUtil } from '@zcrkey/js-utils';
|
|
27
|
-
|
|
28
|
-
const App = () => (
|
|
29
|
-
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
console.log(CrUtil.trim(' @zcrkey/js-utils '))
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
<div className="App">
|
|
35
|
-
</div>
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
export default App;
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
###
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
1
|
+
# @zcrkey/js-utils
|
|
2
|
+
|
|
3
|
+
一个 javascript 实用函数库
|
|
4
|
+
|
|
5
|
+
## 安装和使用
|
|
6
|
+
|
|
7
|
+
### 通过 import 方式安装
|
|
8
|
+
|
|
9
|
+
在开始之前,你可能需要安装 [yarn](https://github.com/yarnpkg/yarn/) 或者 [pnpm](https://pnpm.io/zh/)。
|
|
10
|
+
|
|
11
|
+
从 yarn 或 npm 或 pnpm 安装并引入
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
$ yarn add @zcrkey/js-utils
|
|
15
|
+
# or
|
|
16
|
+
$ npm install @zcrkey/js-utils --save
|
|
17
|
+
# or
|
|
18
|
+
$ pnpm install @zcrkey/js-utils --save
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
修改 `src/App.js`,引入 @zcrkey/js-utils 。
|
|
23
|
+
|
|
24
|
+
```jsx
|
|
25
|
+
import React from 'react';
|
|
26
|
+
import { CrUtil } from '@zcrkey/js-utils';
|
|
27
|
+
|
|
28
|
+
const App = () => (
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
console.log(CrUtil.trim(' @zcrkey/js-utils '))
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
<div className="App">
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export default App;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### NODE 环境下使用
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
const { CrUtil } = require('@zcrkey/js-utils');
|
|
45
|
+
|
|
46
|
+
console.log(CrUtil.trim(' @zcrkey/js-utils '))
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 通过独立版本安装
|
|
51
|
+
|
|
52
|
+
- 下载 @zcrkey/js-utils 的独立版本文件(如 index.umd.js)
|
|
53
|
+
- 在 HTML 文件的 \<head\> 或 \<body\> 部分添加 \<script\> 标签,并指向下载的 @zcrkey/js-utils 文件
|
|
54
|
+
- 在 HTML 文件中使用 @zcrkey/js-utils
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
<script src="./index.umd.js"></script>
|
|
58
|
+
<script>
|
|
59
|
+
console.log(CrUtil.trim(' @zcrkey/js-utils '))
|
|
60
|
+
</script>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 更新日志
|
|
64
|
+
|
|
65
|
+
请查看 [CHANGELOG.md](https://cnb.cool/cnbkey/zcrkey/js-utils/-/blob/master/CHANGELOG.md) 获取版本更新详情。
|
|
66
|
+
|
|
67
|
+
## LICENSE
|
|
68
|
+
|
|
69
|
+
MIT
|
|
@@ -0,0 +1,120 @@
|
|
|
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/eventCenter.ts
|
|
20
|
+
var eventCenter_exports = {};
|
|
21
|
+
__export(eventCenter_exports, {
|
|
22
|
+
default: () => CrEventCenter
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(eventCenter_exports);
|
|
25
|
+
var CrEventCenter = class {
|
|
26
|
+
/**
|
|
27
|
+
* 新增订阅
|
|
28
|
+
*
|
|
29
|
+
* @param {*} key 名称
|
|
30
|
+
* @param {*} listener 执行函数
|
|
31
|
+
* @returns 订阅信息
|
|
32
|
+
* @example
|
|
33
|
+
this.subscription = CrEventCenter.on('名称', (参数) => {});
|
|
34
|
+
*/
|
|
35
|
+
static on(key, listener) {
|
|
36
|
+
const subscriber = this.subscribers.find((item) => item.key == key);
|
|
37
|
+
if (subscriber) {
|
|
38
|
+
subscriber.listeners.push(listener);
|
|
39
|
+
} else {
|
|
40
|
+
this.subscribers.push({
|
|
41
|
+
key,
|
|
42
|
+
listeners: [listener]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
let subscription = {
|
|
46
|
+
key,
|
|
47
|
+
listener,
|
|
48
|
+
remove: () => {
|
|
49
|
+
this.off(subscription);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return subscription;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 移除订阅
|
|
56
|
+
*
|
|
57
|
+
* @param {*} subscription
|
|
58
|
+
* @param {*} type 'all'
|
|
59
|
+
* @example
|
|
60
|
+
CrEventCenter.off(this.subscription);
|
|
61
|
+
*/
|
|
62
|
+
static off(subscription, type) {
|
|
63
|
+
const index = this.subscribers.findIndex(
|
|
64
|
+
(item) => item.key == subscription.key
|
|
65
|
+
);
|
|
66
|
+
if (index > -1) {
|
|
67
|
+
if (type == "all") {
|
|
68
|
+
this.subscribers.splice(index, 1);
|
|
69
|
+
} else {
|
|
70
|
+
const subscriber = this.subscribers[index];
|
|
71
|
+
const _index = subscriber.listeners.findIndex(
|
|
72
|
+
(item) => item == subscription.listener
|
|
73
|
+
);
|
|
74
|
+
if (_index > -1) {
|
|
75
|
+
subscriber.listeners.splice(_index, 1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 派发事件
|
|
82
|
+
*
|
|
83
|
+
* @param {*} key 名称
|
|
84
|
+
* @param {*} args 其余参数
|
|
85
|
+
* @example
|
|
86
|
+
CrEventCenter.emit('名称', 参数数据);
|
|
87
|
+
*/
|
|
88
|
+
static emit(key, ...args) {
|
|
89
|
+
const subscriber = this.subscribers.find((item) => item.key == key);
|
|
90
|
+
if (subscriber && subscriber.listeners && subscriber.listeners.length > 0) {
|
|
91
|
+
subscriber.listeners.forEach((listener) => {
|
|
92
|
+
listener(...args);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 查找事件
|
|
98
|
+
*
|
|
99
|
+
* @param {*} key 名称
|
|
100
|
+
* @example
|
|
101
|
+
let subscriber = CrEventCenter.find('名称');
|
|
102
|
+
*/
|
|
103
|
+
static find(key) {
|
|
104
|
+
return this.subscribers.find((item) => item.key == key);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 清理所有事件
|
|
108
|
+
*
|
|
109
|
+
* @memberof CrEventCenter
|
|
110
|
+
* @example
|
|
111
|
+
CrEventCenter.clear();
|
|
112
|
+
*/
|
|
113
|
+
static clear() {
|
|
114
|
+
this.subscribers.length = 0;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* 储存订阅者
|
|
119
|
+
*/
|
|
120
|
+
CrEventCenter.subscribers = [];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type * from './eventCenter';
|
|
2
|
+
export { default as CrEventCenter } from './eventCenter';
|
|
3
|
+
export type * from './objUtil';
|
|
4
|
+
export { default as CrObjUtil } from './objUtil';
|
|
5
|
+
export { default as CrStorage } from './storage';
|
|
6
|
+
export type * from './treeUtil';
|
|
7
|
+
export { default as CrTreeUtil } from './treeUtil';
|
|
8
|
+
export { default as CrUtil } from './util';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var src_exports = {};
|
|
31
|
+
__export(src_exports, {
|
|
32
|
+
CrEventCenter: () => import_eventCenter.default,
|
|
33
|
+
CrObjUtil: () => import_objUtil.default,
|
|
34
|
+
CrStorage: () => import_storage.default,
|
|
35
|
+
CrTreeUtil: () => import_treeUtil.default,
|
|
36
|
+
CrUtil: () => import_util.default
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(src_exports);
|
|
39
|
+
var import_eventCenter = __toESM(require("./eventCenter"));
|
|
40
|
+
var import_objUtil = __toESM(require("./objUtil"));
|
|
41
|
+
var import_storage = __toESM(require("./storage"));
|
|
42
|
+
var import_treeUtil = __toESM(require("./treeUtil"));
|
|
43
|
+
var import_util = __toESM(require("./util"));
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
CrEventCenter,
|
|
47
|
+
CrObjUtil,
|
|
48
|
+
CrStorage,
|
|
49
|
+
CrTreeUtil,
|
|
50
|
+
CrUtil
|
|
51
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PropertyPath } from 'lodash';
|
|
2
|
+
export type { PropertyPath };
|
|
3
|
+
export default class CrObjUtil {
|
|
4
|
+
/**
|
|
5
|
+
* 根据字段路径获取对象值
|
|
6
|
+
* @param obj { 'a': [{ 'b': { 'c': 3 } }] }
|
|
7
|
+
* @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
|
|
8
|
+
* @param defaultValue 找不到时返回的默认值
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
static getValueByPath(obj: Record<string | number | symbol, any>, propertyPath: PropertyPath, defaultValue?: any): any;
|
|
12
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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/objUtil.ts
|
|
20
|
+
var objUtil_exports = {};
|
|
21
|
+
__export(objUtil_exports, {
|
|
22
|
+
default: () => CrObjUtil
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(objUtil_exports);
|
|
25
|
+
var import_lodash = require("lodash");
|
|
26
|
+
var CrObjUtil = class {
|
|
27
|
+
/**
|
|
28
|
+
* 根据字段路径获取对象值
|
|
29
|
+
* @param obj { 'a': [{ 'b': { 'c': 3 } }] }
|
|
30
|
+
* @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
|
|
31
|
+
* @param defaultValue 找不到时返回的默认值
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
static getValueByPath(obj, propertyPath, defaultValue) {
|
|
35
|
+
return (0, import_lodash.get)(obj, propertyPath, defaultValue);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
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/storage.ts
|
|
20
|
+
var storage_exports = {};
|
|
21
|
+
__export(storage_exports, {
|
|
22
|
+
default: () => CrStorage
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(storage_exports);
|
|
25
|
+
var CrStorage = class {
|
|
26
|
+
/**
|
|
27
|
+
* 设置本地存储
|
|
28
|
+
* @param key
|
|
29
|
+
* @param data
|
|
30
|
+
*/
|
|
31
|
+
static setLocalItem(key, data) {
|
|
32
|
+
try {
|
|
33
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.warn(`setLocalItem:${key}:${error}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 获取本地存储
|
|
40
|
+
* @param key
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
static getLocalItem(key) {
|
|
44
|
+
const str = localStorage.getItem(key);
|
|
45
|
+
if (str) {
|
|
46
|
+
if (str === "undefined") {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(str);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.warn(`getLocalItem:${key}:${error}`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 清除某个本地存储
|
|
61
|
+
* @param key
|
|
62
|
+
*/
|
|
63
|
+
static removeLocalItem(key) {
|
|
64
|
+
localStorage.removeItem(key);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 清除所有本地存储
|
|
68
|
+
*/
|
|
69
|
+
static clearLocal() {
|
|
70
|
+
localStorage.clear();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 设置会话存储
|
|
74
|
+
* @param key
|
|
75
|
+
* @param data
|
|
76
|
+
*/
|
|
77
|
+
static setSessionItem(key, data) {
|
|
78
|
+
try {
|
|
79
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.warn(`setSessionItem:${key}:${error}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 获取会话存储
|
|
86
|
+
* @param key
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
static getSessionItem(key) {
|
|
90
|
+
const str = sessionStorage.getItem(key);
|
|
91
|
+
if (str) {
|
|
92
|
+
if (str === "undefined") {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(str);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.warn(`getSessionItem:${key}:${error}`);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 清除某个会话存储
|
|
107
|
+
* @param key
|
|
108
|
+
*/
|
|
109
|
+
static removeSessionItem(key) {
|
|
110
|
+
sessionStorage.removeItem(key);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 清除所有会话存储
|
|
114
|
+
*/
|
|
115
|
+
static clearSession() {
|
|
116
|
+
sessionStorage.clear();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export default class CrTreeUtil {
|
|
2
|
+
/**
|
|
3
|
+
* 列表数据转树形数据
|
|
4
|
+
* @param list
|
|
5
|
+
* @param options
|
|
6
|
+
* @param options.idField 默认值 id
|
|
7
|
+
* @param options.parentIdField 默认值 parentId
|
|
8
|
+
* @param options.childrenField 默认值 children
|
|
9
|
+
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
10
|
+
* @param options.getData 处理数据
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
static listToTree<T extends Record<string, any>>(list: T[], options?: {
|
|
14
|
+
idField?: string;
|
|
15
|
+
parentIdField?: string;
|
|
16
|
+
childrenField?: string;
|
|
17
|
+
isCloneDeep?: boolean;
|
|
18
|
+
getData?: (item: T) => any;
|
|
19
|
+
}): any[];
|
|
20
|
+
/**
|
|
21
|
+
* 树形数据转列表数据
|
|
22
|
+
* @param tree
|
|
23
|
+
* @param options
|
|
24
|
+
* @param options.childrenField 默认值 children
|
|
25
|
+
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
static treeToList<T extends Record<string, any>>(tree: T[], options?: {
|
|
29
|
+
childrenField?: string;
|
|
30
|
+
isCloneDeep?: boolean;
|
|
31
|
+
}): T[];
|
|
32
|
+
/**
|
|
33
|
+
* 获取扁平化父数据(包含自身)
|
|
34
|
+
* @param listData
|
|
35
|
+
* @param value
|
|
36
|
+
* @param settings
|
|
37
|
+
* @param settings.valueField 默认值 value
|
|
38
|
+
* @param settings.idField 默认值 id
|
|
39
|
+
* @param settings.parentIdField 默认值 parentId
|
|
40
|
+
* @param settings.getData 过滤数据
|
|
41
|
+
*/
|
|
42
|
+
static getFlatParentDatas<T = any, R extends Record<string, any> = any>(listData: R[], value: number | string, settings?: {
|
|
43
|
+
valueField?: string;
|
|
44
|
+
idField?: string;
|
|
45
|
+
parentIdField?: string;
|
|
46
|
+
getData?: (item: R) => T;
|
|
47
|
+
}): T[];
|
|
48
|
+
}
|
|
@@ -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/treeUtil.ts
|
|
20
|
+
var treeUtil_exports = {};
|
|
21
|
+
__export(treeUtil_exports, {
|
|
22
|
+
default: () => CrTreeUtil
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(treeUtil_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
|
+
};
|