@zcrkey/js-utils 0.0.4 → 0.0.5
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/.dumi/global.less +1396 -0
- package/.dumirc.ts +36 -0
- package/.fatherrc.ts +14 -0
- package/.husky/commit-msg +4 -0
- package/.husky/pre-commit +4 -0
- package/.prettierignore +2 -0
- package/.stylelintrc +10 -0
- package/README.md +60 -56
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -0
- package/dist/index.umd.js +1 -1
- package/dist/objUtil.d.ts +12 -0
- package/dist/objUtil.js +28 -0
- package/dist/treeUtil.d.ts +48 -0
- package/dist/treeUtil.js +185 -0
- package/dist/util.d.ts +41 -6
- package/dist/util.js +117 -6
- package/docs/api/eventCenter/index.md +34 -0
- package/docs/api/index.md +5 -0
- package/docs/api/storage/index.md +9 -0
- package/docs/api/storage/local.tsx +91 -0
- package/docs/api/storage/session.tsx +85 -0
- package/docs/api/treeUtil/index.md +5 -0
- package/docs/api/treeUtil/index.tsx +266 -0
- package/docs/api/util/index.md +6 -0
- package/docs/api/util/index.tsx +405 -0
- package/docs/api/util/is.tsx +196 -0
- package/docs/guide.md +24 -0
- package/package.json +3 -5
- package/src/eventCenter.ts +112 -0
- package/src/index.ts +8 -0
- package/src/objUtil.ts +20 -0
- package/src/storage.ts +101 -0
- package/src/treeUtil.ts +164 -0
- package/src/util.ts +656 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export type TSubscribers = {
|
|
2
|
+
key: string;
|
|
3
|
+
listeners: Array<(...args: any) => void>;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type TSubscription = {
|
|
7
|
+
key: string;
|
|
8
|
+
listener: (...args: any) => void;
|
|
9
|
+
remove: () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default class CrEventCenter {
|
|
13
|
+
/**
|
|
14
|
+
* 储存订阅者
|
|
15
|
+
*/
|
|
16
|
+
static subscribers: TSubscribers[] = [];
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 新增订阅
|
|
20
|
+
*
|
|
21
|
+
* @param {*} key 名称
|
|
22
|
+
* @param {*} listener 执行函数
|
|
23
|
+
* @returns 订阅信息
|
|
24
|
+
* @example
|
|
25
|
+
this.subscription = CrEventCenter.on('名称', (参数) => {});
|
|
26
|
+
*/
|
|
27
|
+
static on(key: string, listener: (...args: any) => void): TSubscription {
|
|
28
|
+
const subscriber = this.subscribers.find((item) => item.key == key);
|
|
29
|
+
if (subscriber) {
|
|
30
|
+
subscriber.listeners.push(listener);
|
|
31
|
+
} else {
|
|
32
|
+
this.subscribers.push({
|
|
33
|
+
key: key,
|
|
34
|
+
listeners: [listener],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
let subscription: TSubscription = {
|
|
38
|
+
key: key,
|
|
39
|
+
listener: listener,
|
|
40
|
+
remove: () => {
|
|
41
|
+
this.off(subscription);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
return subscription;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 移除订阅
|
|
49
|
+
*
|
|
50
|
+
* @param {*} subscription
|
|
51
|
+
* @param {*} type 'all'
|
|
52
|
+
* @example
|
|
53
|
+
CrEventCenter.off(this.subscription);
|
|
54
|
+
*/
|
|
55
|
+
static off(subscription: TSubscription, type?: 'all') {
|
|
56
|
+
const index = this.subscribers.findIndex(
|
|
57
|
+
(item) => item.key == subscription.key,
|
|
58
|
+
);
|
|
59
|
+
if (index > -1) {
|
|
60
|
+
if (type == 'all') {
|
|
61
|
+
this.subscribers.splice(index, 1);
|
|
62
|
+
} else {
|
|
63
|
+
const subscriber = this.subscribers[index];
|
|
64
|
+
const _index = subscriber.listeners.findIndex(
|
|
65
|
+
(item) => item == subscription.listener,
|
|
66
|
+
);
|
|
67
|
+
if (_index > -1) {
|
|
68
|
+
subscriber.listeners.splice(_index, 1);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 派发事件
|
|
76
|
+
*
|
|
77
|
+
* @param {*} key 名称
|
|
78
|
+
* @param {*} args 其余参数
|
|
79
|
+
* @example
|
|
80
|
+
CrEventCenter.emit('名称', 参数数据);
|
|
81
|
+
*/
|
|
82
|
+
static emit(key: string, ...args: any) {
|
|
83
|
+
const subscriber = this.subscribers.find((item) => item.key == key);
|
|
84
|
+
if (subscriber && subscriber.listeners && subscriber.listeners.length > 0) {
|
|
85
|
+
subscriber.listeners.forEach((listener) => {
|
|
86
|
+
listener(...args);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 查找事件
|
|
93
|
+
*
|
|
94
|
+
* @param {*} key 名称
|
|
95
|
+
* @example
|
|
96
|
+
let subscriber = CrEventCenter.find('名称');
|
|
97
|
+
*/
|
|
98
|
+
static find(key: string): TSubscribers | undefined {
|
|
99
|
+
return this.subscribers.find((item) => item.key == key);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 清理所有事件
|
|
104
|
+
*
|
|
105
|
+
* @memberof CrEventCenter
|
|
106
|
+
* @example
|
|
107
|
+
CrEventCenter.clear();
|
|
108
|
+
*/
|
|
109
|
+
static clear() {
|
|
110
|
+
this.subscribers.length = 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/index.ts
ADDED
|
@@ -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';
|
package/src/objUtil.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { get, PropertyPath } from 'lodash';
|
|
2
|
+
|
|
3
|
+
export type { PropertyPath };
|
|
4
|
+
|
|
5
|
+
export default class CrObjUtil {
|
|
6
|
+
/**
|
|
7
|
+
* 根据字段路径获取对象值
|
|
8
|
+
* @param obj { 'a': [{ 'b': { 'c': 3 } }] }
|
|
9
|
+
* @param path 'a[0].b.c' | ['a', '0', 'b', 'c']
|
|
10
|
+
* @param defaultValue 找不到时返回的默认值
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
static getValueByPath(
|
|
14
|
+
obj: Record<string | number | symbol, any>,
|
|
15
|
+
propertyPath: PropertyPath,
|
|
16
|
+
defaultValue?: any,
|
|
17
|
+
) {
|
|
18
|
+
return get(obj, propertyPath, defaultValue);
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export default class CrStorage {
|
|
2
|
+
/**
|
|
3
|
+
* 设置本地存储
|
|
4
|
+
* @param key
|
|
5
|
+
* @param data
|
|
6
|
+
*/
|
|
7
|
+
static setLocalItem(key: string, data: any) {
|
|
8
|
+
try {
|
|
9
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
10
|
+
} catch (error) {
|
|
11
|
+
console.warn(`setLocalItem:${key}:${error}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 获取本地存储
|
|
17
|
+
* @param key
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
static getLocalItem<T = any>(key: string): T | null {
|
|
21
|
+
const str = localStorage.getItem(key);
|
|
22
|
+
if (str) {
|
|
23
|
+
if (str === 'undefined') {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(str);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.warn(`getLocalItem:${key}:${error}`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 清除某个本地存储
|
|
39
|
+
* @param key
|
|
40
|
+
*/
|
|
41
|
+
static removeLocalItem(key: string) {
|
|
42
|
+
localStorage.removeItem(key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 清除所有本地存储
|
|
47
|
+
*/
|
|
48
|
+
static clearLocal() {
|
|
49
|
+
localStorage.clear();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 设置会话存储
|
|
54
|
+
* @param key
|
|
55
|
+
* @param data
|
|
56
|
+
*/
|
|
57
|
+
static setSessionItem(key: string, data: any) {
|
|
58
|
+
try {
|
|
59
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.warn(`setSessionItem:${key}:${error}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 获取会话存储
|
|
67
|
+
* @param key
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
static getSessionItem<T = any>(key: string): T | null {
|
|
71
|
+
const str = sessionStorage.getItem(key);
|
|
72
|
+
if (str) {
|
|
73
|
+
if (str === 'undefined') {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
return JSON.parse(str);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.warn(`getSessionItem:${key}:${error}`);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 清除某个会话存储
|
|
89
|
+
* @param key
|
|
90
|
+
*/
|
|
91
|
+
static removeSessionItem(key: string) {
|
|
92
|
+
sessionStorage.removeItem(key);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 清除所有会话存储
|
|
97
|
+
*/
|
|
98
|
+
static clearSession() {
|
|
99
|
+
sessionStorage.clear();
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/treeUtil.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cloneDeep as _cloneDeep,
|
|
3
|
+
find as _find,
|
|
4
|
+
get as _get,
|
|
5
|
+
keyBy as _keyBy,
|
|
6
|
+
} from 'lodash';
|
|
7
|
+
|
|
8
|
+
export default class CrTreeUtil {
|
|
9
|
+
/**
|
|
10
|
+
* 列表数据转树形数据
|
|
11
|
+
* @param list
|
|
12
|
+
* @param options
|
|
13
|
+
* @param options.idField 默认值 id
|
|
14
|
+
* @param options.parentIdField 默认值 parentId
|
|
15
|
+
* @param options.childrenField 默认值 children
|
|
16
|
+
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
17
|
+
* @param options.getData 处理数据
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
static listToTree<T extends Record<string, any>>(
|
|
21
|
+
list: T[],
|
|
22
|
+
options?: {
|
|
23
|
+
idField?: string;
|
|
24
|
+
parentIdField?: string;
|
|
25
|
+
childrenField?: string;
|
|
26
|
+
isCloneDeep?: boolean;
|
|
27
|
+
getData?: (item: T) => any;
|
|
28
|
+
},
|
|
29
|
+
): any[] {
|
|
30
|
+
const {
|
|
31
|
+
idField = 'id',
|
|
32
|
+
parentIdField = 'parentId',
|
|
33
|
+
childrenField = 'children',
|
|
34
|
+
isCloneDeep = true,
|
|
35
|
+
getData = (item: T) => item,
|
|
36
|
+
} = options || {};
|
|
37
|
+
|
|
38
|
+
const listData = isCloneDeep ? _cloneDeep(list) : list;
|
|
39
|
+
const treeData: any[] = [];
|
|
40
|
+
const nodeMap = new Map<string | number, any>();
|
|
41
|
+
|
|
42
|
+
for (const item of listData) {
|
|
43
|
+
const id = item[idField];
|
|
44
|
+
const node = {
|
|
45
|
+
...getData(item),
|
|
46
|
+
__origin_id: id,
|
|
47
|
+
__origin_pid: item[parentIdField],
|
|
48
|
+
};
|
|
49
|
+
nodeMap.set(id, node);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const node of nodeMap.values()) {
|
|
53
|
+
const parentId = node.__origin_pid;
|
|
54
|
+
if (
|
|
55
|
+
parentId === undefined ||
|
|
56
|
+
parentId === null ||
|
|
57
|
+
parentId === 0 ||
|
|
58
|
+
!nodeMap.has(parentId)
|
|
59
|
+
) {
|
|
60
|
+
treeData.push(node);
|
|
61
|
+
} else {
|
|
62
|
+
const parentNode = nodeMap.get(parentId);
|
|
63
|
+
if (!parentNode[childrenField]) {
|
|
64
|
+
parentNode[childrenField] = [];
|
|
65
|
+
}
|
|
66
|
+
parentNode[childrenField].push(node);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const clean = (nodes: any[]) => {
|
|
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
|
+
|
|
81
|
+
return treeData;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 树形数据转列表数据
|
|
86
|
+
* @param tree
|
|
87
|
+
* @param options
|
|
88
|
+
* @param options.childrenField 默认值 children
|
|
89
|
+
* @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
static treeToList<T extends Record<string, any>>(
|
|
93
|
+
tree: T[],
|
|
94
|
+
options?: {
|
|
95
|
+
childrenField?: string;
|
|
96
|
+
isCloneDeep?: boolean;
|
|
97
|
+
},
|
|
98
|
+
) {
|
|
99
|
+
const { childrenField = 'children', isCloneDeep = true } = options || {};
|
|
100
|
+
const result: T[] = [];
|
|
101
|
+
const queue: T[] = [...tree];
|
|
102
|
+
while (queue.length) {
|
|
103
|
+
const node = queue.shift()!;
|
|
104
|
+
const children = node[childrenField];
|
|
105
|
+
const { [childrenField]: _, ...rest } = node;
|
|
106
|
+
result.push(rest as T);
|
|
107
|
+
if (children && children.length) {
|
|
108
|
+
queue.push(...children);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return isCloneDeep ? _cloneDeep(result) : result;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 获取扁平化父数据(包含自身)
|
|
117
|
+
* @param listData
|
|
118
|
+
* @param value
|
|
119
|
+
* @param settings
|
|
120
|
+
* @param settings.valueField 默认值 value
|
|
121
|
+
* @param settings.idField 默认值 id
|
|
122
|
+
* @param settings.parentIdField 默认值 parentId
|
|
123
|
+
* @param settings.getData 过滤数据
|
|
124
|
+
*/
|
|
125
|
+
static getFlatParentDatas<T = any, R extends Record<string, any> = any>(
|
|
126
|
+
listData: R[],
|
|
127
|
+
value: number | string,
|
|
128
|
+
settings?: {
|
|
129
|
+
valueField?: string;
|
|
130
|
+
idField?: string;
|
|
131
|
+
parentIdField?: string;
|
|
132
|
+
getData?: (item: R) => T;
|
|
133
|
+
},
|
|
134
|
+
): T[] {
|
|
135
|
+
if (!Array.isArray(listData) || listData.length === 0 || value == null) {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
const options = Object.assign(
|
|
139
|
+
{
|
|
140
|
+
valueField: 'value',
|
|
141
|
+
idField: 'id',
|
|
142
|
+
parentIdField: 'parentId',
|
|
143
|
+
getData: (item: R): T => {
|
|
144
|
+
return item as unknown as T;
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
settings,
|
|
148
|
+
);
|
|
149
|
+
const nodeMap = _keyBy(listData, options.valueField);
|
|
150
|
+
const result: T[] = [];
|
|
151
|
+
const visited = new Set();
|
|
152
|
+
let current: R | undefined = nodeMap[value];
|
|
153
|
+
while (current && !visited.has(_get(current, options.idField))) {
|
|
154
|
+
visited.add(_get(current, options.idField));
|
|
155
|
+
result.unshift(options.getData(current));
|
|
156
|
+
const parentId: string | number | undefined | null = _get(
|
|
157
|
+
current,
|
|
158
|
+
options.parentIdField,
|
|
159
|
+
);
|
|
160
|
+
current = _find(listData, [options.valueField, parentId]);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
}
|