@zcrkey/js-utils 0.0.3 → 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.
@@ -0,0 +1,196 @@
1
+ /**
2
+ * title: isXxx 判断
3
+ */
4
+
5
+ import { CrUtil } from '@zcrkey/js-utils';
6
+ import React, { useEffect, useState } from 'react';
7
+
8
+ const number = 1;
9
+ const emptyString = '';
10
+ const string = 'zcr';
11
+ const date = new Date();
12
+ const sin = Math.sin;
13
+ const _null = null;
14
+ const _undefined = undefined;
15
+ const boolean = true;
16
+ const emptyArray: never[] = [];
17
+ const emptyObject = {};
18
+ const object = {
19
+ number: 1,
20
+ string: 'zcr',
21
+ obj: {},
22
+ arr: [],
23
+ };
24
+ const array = [1, 'zcr', {}, []];
25
+
26
+ export default () => {
27
+ const [result, setResult] = useState<any>({});
28
+
29
+ useEffect(() => {
30
+ let value: any = [];
31
+ let other: { name: string } | { name: string }[] = value;
32
+ if (CrUtil.isObject(other)) {
33
+ other.name; // other 的类型为 { name: string }
34
+ }
35
+ if (CrUtil.isArray(other)) {
36
+ other[0]?.name; // other 的类型为 { name: string }[]
37
+ }
38
+ });
39
+
40
+ const isArray = () => {
41
+ let res: any[] = [];
42
+ if (CrUtil.isArray(emptyArray)) {
43
+ res.push(`${JSON.stringify(emptyArray)}:是数组`);
44
+ }
45
+ if (CrUtil.isArray(array)) {
46
+ res.push(`${JSON.stringify(array)}:是数组`);
47
+ }
48
+ if (!CrUtil.isArray(emptyString)) {
49
+ res.push(`${JSON.stringify(emptyString)}: 不是数组`);
50
+ }
51
+ if (!CrUtil.isArray(number)) {
52
+ res.push(`${JSON.stringify(number)}: 不是数组`);
53
+ }
54
+ if (!CrUtil.isArray(_null)) {
55
+ res.push(`${JSON.stringify(_null)}: 不是数组`);
56
+ }
57
+ if (!CrUtil.isArray(_undefined)) {
58
+ res.push(`${JSON.stringify(_undefined)}: 不是数组`);
59
+ }
60
+ setResult({
61
+ ...result,
62
+ isArray: res,
63
+ });
64
+ };
65
+
66
+ const isObject = () => {
67
+ let res: any[] = [];
68
+ if (CrUtil.isObject(emptyObject)) {
69
+ res.push(`${JSON.stringify(emptyObject)}:是对象`);
70
+ }
71
+ if (CrUtil.isObject(object)) {
72
+ res.push(`${JSON.stringify(object)}:是对象`);
73
+ }
74
+ if (CrUtil.isObject(date)) {
75
+ res.push(`new Date(): 是对象`);
76
+ }
77
+ if (!CrUtil.isObject(array)) {
78
+ res.push(`${JSON.stringify(array)}: 不是对象`);
79
+ }
80
+ if (!CrUtil.isObject(_null)) {
81
+ res.push(`${JSON.stringify(_null)}: 不是对象`);
82
+ }
83
+ if (!CrUtil.isObject(_undefined)) {
84
+ res.push(`${JSON.stringify(_undefined)}: 不是对象`);
85
+ }
86
+ if (!CrUtil.isObject(sin)) {
87
+ res.push(`Math.sin: 不是对象`);
88
+ }
89
+ setResult({
90
+ ...result,
91
+ isObject: res,
92
+ });
93
+ };
94
+
95
+ const isEmptyObject = () => {
96
+ let res: any[] = [];
97
+ if (CrUtil.isEmptyObject(emptyObject)) {
98
+ res.push(`${JSON.stringify(emptyObject)}:是空对象`);
99
+ }
100
+ if (!CrUtil.isEmptyObject(object)) {
101
+ res.push(`${JSON.stringify(object)}: 不是空对象`);
102
+ }
103
+
104
+ setResult({
105
+ ...result,
106
+ isEmptyObject: res,
107
+ });
108
+ };
109
+
110
+ return (
111
+ <>
112
+ <div className="flex flex-direction">
113
+ <div>判断是否为数组</div>
114
+ <div className="text-lg">CrUtil.isArray</div>
115
+ <div>
116
+ <button
117
+ type="button"
118
+ className="cr-btn line-blue radius"
119
+ onClick={isArray}
120
+ >
121
+ 调用
122
+ </button>
123
+ <div>
124
+ {result.isArray &&
125
+ result.isArray.map((str: string) => {
126
+ return <div key={str}>{str}</div>;
127
+ })}
128
+ </div>
129
+ </div>
130
+ </div>
131
+
132
+ <div className="divider"></div>
133
+
134
+ <div className="flex flex-direction">
135
+ <div>判断是否为对象</div>
136
+ <div className="text-lg">CrUtil.isObject</div>
137
+ <div>
138
+ <button
139
+ type="button"
140
+ className="cr-btn line-blue radius"
141
+ onClick={isObject}
142
+ >
143
+ 调用
144
+ </button>
145
+ <div>
146
+ {result.isObject &&
147
+ result.isObject.map((str: string) => {
148
+ return <div key={str}>{str}</div>;
149
+ })}
150
+ </div>
151
+ </div>
152
+ </div>
153
+
154
+ <div className="divider"></div>
155
+
156
+ <div className="flex flex-direction">
157
+ <div>判断是否为空对象</div>
158
+ <div className="text-lg">CrUtil.isEmptyObject</div>
159
+ <div>
160
+ <button
161
+ type="button"
162
+ className="cr-btn line-blue radius"
163
+ onClick={isEmptyObject}
164
+ >
165
+ 调用
166
+ </button>
167
+ <div>
168
+ {result.isEmptyObject &&
169
+ result.isEmptyObject.map((str: string) => {
170
+ return <div key={str}>{str}</div>;
171
+ })}
172
+ </div>
173
+ </div>
174
+ </div>
175
+
176
+ <div className="divider"></div>
177
+
178
+ <div className="flex flex-direction margin-bottom">
179
+ <div>判断是否对象的属性是否全部为空</div>
180
+ <div className="text-lg margin-bottom">
181
+ CrUtil.isObjectPropertiesAllEmpty
182
+ </div>
183
+ <div>判断是否为日期</div>
184
+ <div className="text-lg margin-bottom">CrUtil.isDate</div>
185
+ <div>判断是否为字符串</div>
186
+ <div className="text-lg margin-bottom">CrUtil.isString</div>
187
+ <div>判断是否为数字</div>
188
+ <div className="text-lg margin-bottom">CrUtil.isNumber</div>
189
+ <div>判断是否为文件 File</div>
190
+ <div className="text-lg margin-bottom">CrUtil.isFile</div>
191
+ <div>判断是否为 Boolean</div>
192
+ <div className="text-lg margin-bottom">CrUtil.isBoolean</div>
193
+ </div>
194
+ </>
195
+ );
196
+ };
package/docs/guide.md ADDED
@@ -0,0 +1,24 @@
1
+ # @zcrkey/js-utils
2
+
3
+ <!-- [![NPM version](https://img.shields.io/npm/v/js-utils.svg?style=flat)](https://g-nhxn7953.coding.net/public-artifacts/zcrkey/npm/@zcrkey/js-utils/version/40193242/overview)
4
+ [![NPM downloads](http://img.shields.io/npm/dm/js-utils.svg?style=flat)](https://g-nhxn7953.coding.net/public-artifacts/zcrkey/npm/@zcrkey/js-utils/version/40193242/overview) -->
5
+
6
+ 一个 javascript 实用函数库
7
+
8
+ ## git 规范
9
+
10
+ - feat: 新增特性 (feature)
11
+ - fix: 修复 Bug(bug fix)
12
+ - docs: 修改文档 (documentation)
13
+ - style: 代码格式修改(white-space, formatting, missing semi colons, etc)
14
+ - refactor: 代码重构(refactor)
15
+ - perf: 改善性能(A code change that improves performance)
16
+ - test: 测试(when adding missing tests)
17
+ - build: 变更项目构建或外部依赖(例如 scopes: webpack、gulp、npm 等)
18
+ - ci: 更改持续集成软件的配置文件和 package 中的 scripts 命令,例如 scopes: Travis, Circle 等
19
+ - chore: 变更构建流程或辅助工具(比如更改测试环境)
20
+ - revert: 代码回退
21
+
22
+ ## LICENSE
23
+
24
+ MIT
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@zcrkey/js-utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "一个 javascript 实用函数库",
5
5
  "keywords": [
6
6
  "js"
7
7
  ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://cnb.cool/cnbkey/zcrkey/js-utils.git"
11
+ },
8
12
  "license": "MIT",
9
13
  "main": "dist/index.umd.js",
10
14
  "module": "dist/index.js",
11
15
  "types": "dist/index.d.ts",
12
- "files": [
13
- "dist"
14
- ],
15
16
  "dependencies": {
17
+ "lodash": "^4.17.21",
16
18
  "qs": "^6.11.2"
17
19
  },
18
20
  "publishConfig": {
@@ -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
+ }
@@ -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
+ }