@zcrkey/js-utils 0.0.5 → 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.
Files changed (48) hide show
  1. package/README.md +9 -0
  2. package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
  3. package/dist/cjs/index.js +51 -0
  4. package/dist/cjs/objUtil.js +37 -0
  5. package/{src/storage.ts → dist/cjs/storage.js} +118 -101
  6. package/dist/cjs/treeUtil.js +145 -0
  7. package/{src/util.ts → dist/cjs/util.js} +170 -254
  8. package/dist/esm/eventCenter.d.ts +59 -0
  9. package/{src/index.ts → dist/esm/index.d.ts} +8 -8
  10. package/dist/esm/objUtil.d.ts +12 -0
  11. package/dist/esm/storage.d.ts +44 -0
  12. package/dist/esm/treeUtil.d.ts +48 -0
  13. package/dist/esm/util.d.ts +209 -0
  14. package/package.json +15 -4
  15. package/.dumi/global.less +0 -1396
  16. package/.dumirc.ts +0 -36
  17. package/.fatherrc.ts +0 -14
  18. package/.husky/commit-msg +0 -4
  19. package/.husky/pre-commit +0 -4
  20. package/.prettierignore +0 -2
  21. package/.stylelintrc +0 -10
  22. package/docs/api/eventCenter/index.md +0 -34
  23. package/docs/api/index.md +0 -5
  24. package/docs/api/storage/index.md +0 -9
  25. package/docs/api/storage/local.tsx +0 -91
  26. package/docs/api/storage/session.tsx +0 -85
  27. package/docs/api/treeUtil/index.md +0 -5
  28. package/docs/api/treeUtil/index.tsx +0 -266
  29. package/docs/api/util/index.md +0 -6
  30. package/docs/api/util/index.tsx +0 -405
  31. package/docs/api/util/is.tsx +0 -196
  32. package/docs/guide.md +0 -24
  33. package/src/objUtil.ts +0 -20
  34. package/src/treeUtil.ts +0 -164
  35. package/tsconfig.json +0 -18
  36. /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
  37. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  38. /package/dist/{objUtil.d.ts → cjs/objUtil.d.ts} +0 -0
  39. /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
  40. /package/dist/{treeUtil.d.ts → cjs/treeUtil.d.ts} +0 -0
  41. /package/dist/{util.d.ts → cjs/util.d.ts} +0 -0
  42. /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
  43. /package/dist/{index.js → esm/index.js} +0 -0
  44. /package/dist/{objUtil.js → esm/objUtil.js} +0 -0
  45. /package/dist/{storage.js → esm/storage.js} +0 -0
  46. /package/dist/{treeUtil.js → esm/treeUtil.js} +0 -0
  47. /package/dist/{util.js → esm/util.js} +0 -0
  48. /package/dist/{index.umd.js → umd/index.umd.js} +0 -0
@@ -0,0 +1,59 @@
1
+ export type TSubscribers = {
2
+ key: string;
3
+ listeners: Array<(...args: any) => void>;
4
+ };
5
+ export type TSubscription = {
6
+ key: string;
7
+ listener: (...args: any) => void;
8
+ remove: () => void;
9
+ };
10
+ export default class CrEventCenter {
11
+ /**
12
+ * 储存订阅者
13
+ */
14
+ static subscribers: TSubscribers[];
15
+ /**
16
+ * 新增订阅
17
+ *
18
+ * @param {*} key 名称
19
+ * @param {*} listener 执行函数
20
+ * @returns 订阅信息
21
+ * @example
22
+ this.subscription = CrEventCenter.on('名称', (参数) => {});
23
+ */
24
+ static on(key: string, listener: (...args: any) => void): TSubscription;
25
+ /**
26
+ * 移除订阅
27
+ *
28
+ * @param {*} subscription
29
+ * @param {*} type 'all'
30
+ * @example
31
+ CrEventCenter.off(this.subscription);
32
+ */
33
+ static off(subscription: TSubscription, type?: 'all'): void;
34
+ /**
35
+ * 派发事件
36
+ *
37
+ * @param {*} key 名称
38
+ * @param {*} args 其余参数
39
+ * @example
40
+ CrEventCenter.emit('名称', 参数数据);
41
+ */
42
+ static emit(key: string, ...args: any): void;
43
+ /**
44
+ * 查找事件
45
+ *
46
+ * @param {*} key 名称
47
+ * @example
48
+ let subscriber = CrEventCenter.find('名称');
49
+ */
50
+ static find(key: string): TSubscribers | undefined;
51
+ /**
52
+ * 清理所有事件
53
+ *
54
+ * @memberof CrEventCenter
55
+ * @example
56
+ CrEventCenter.clear();
57
+ */
58
+ static clear(): void;
59
+ }
@@ -1,8 +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';
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,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,44 @@
1
+ export default class CrStorage {
2
+ /**
3
+ * 设置本地存储
4
+ * @param key
5
+ * @param data
6
+ */
7
+ static setLocalItem(key: string, data: any): void;
8
+ /**
9
+ * 获取本地存储
10
+ * @param key
11
+ * @returns
12
+ */
13
+ static getLocalItem<T = any>(key: string): T | null;
14
+ /**
15
+ * 清除某个本地存储
16
+ * @param key
17
+ */
18
+ static removeLocalItem(key: string): void;
19
+ /**
20
+ * 清除所有本地存储
21
+ */
22
+ static clearLocal(): void;
23
+ /**
24
+ * 设置会话存储
25
+ * @param key
26
+ * @param data
27
+ */
28
+ static setSessionItem(key: string, data: any): void;
29
+ /**
30
+ * 获取会话存储
31
+ * @param key
32
+ * @returns
33
+ */
34
+ static getSessionItem<T = any>(key: string): T | null;
35
+ /**
36
+ * 清除某个会话存储
37
+ * @param key
38
+ */
39
+ static removeSessionItem(key: string): void;
40
+ /**
41
+ * 清除所有会话存储
42
+ */
43
+ static clearSession(): void;
44
+ }
@@ -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,209 @@
1
+ export default class CrUtil {
2
+ /**
3
+ * 判断是否为数组
4
+ * @param value
5
+ * @returns boolean
6
+ */
7
+ static isArray<T = any>(value: any): value is T[];
8
+ /**
9
+ * 判断是否为对象
10
+ * @param value
11
+ * @returns boolean
12
+ */
13
+ static isObject<T = any>(value: T): value is T extends object ? (T extends any[] ? never : T) : never;
14
+ /**
15
+ * 判断是否为空对象
16
+ * @param value
17
+ * @returns boolean
18
+ */
19
+ static isEmptyObject(value: object): value is Record<string | number, never>;
20
+ /**
21
+ * 判断是否对象的属性是否全部为空
22
+ * @param value
23
+ * @returns boolean
24
+ */
25
+ static isObjectPropertiesAllEmpty(value: Record<string | number, any>): boolean;
26
+ /**
27
+ * 判断是否为日期
28
+ * @param value
29
+ * @returns boolean
30
+ */
31
+ static isDate(value: unknown): boolean;
32
+ /**
33
+ * 判断是否为字符串
34
+ * @param value
35
+ * @returns boolean
36
+ */
37
+ static isString(value: unknown): value is string;
38
+ /**
39
+ * 判断是否为数字
40
+ * @param value
41
+ * @returns boolean
42
+ */
43
+ static isNumber(value: unknown): value is number;
44
+ /**
45
+ * 判断是否为文件 File
46
+ * @param value
47
+ * @returns boolean
48
+ */
49
+ static isFile(value: unknown): value is File;
50
+ /**
51
+ * 判断是否为 Boolean
52
+ * @param value
53
+ * @returns boolean
54
+ */
55
+ static isBoolean(value: unknown): value is boolean;
56
+ /**
57
+ * 判断是否为 Function
58
+ * @param value
59
+ * @returns boolean
60
+ */
61
+ static isFunction(value: unknown): value is (...args: any[]) => any;
62
+ /**
63
+ * 去掉字符串前后所有空格
64
+ * @param str
65
+ * @returns string
66
+ */
67
+ static trim(str: string): string;
68
+ /**
69
+ * 获取数组为几维数组
70
+ * @param arr
71
+ * @returns number
72
+ */
73
+ static getArrayDimension(arr: any[]): number;
74
+ /**
75
+ * 深拷贝
76
+ * @param value
77
+ * @returns
78
+ */
79
+ static cloneDeep<T>(value: T): T;
80
+ /**
81
+ * 深拷贝
82
+ * @param target
83
+ * @deprecated 即将移除,使用 {@link CrUtil.cloneDeep} 替代
84
+ * @returns
85
+ */
86
+ static deepCopy<T = any>(target: T): T;
87
+ /**
88
+ * 列表数据转树型数据
89
+ * @param listData
90
+ * @param settings
91
+ * @param settings.idField 默认值 id
92
+ * @param settings.pidField 默认值 parentId
93
+ * @param settings.childrenField 默认值 children
94
+ * @deprecated 即将移除,使用 {@link CrObjUtil.listToTree} 替代
95
+ * @returns
96
+ */
97
+ static listToTreeData(listData: any[], settings?: {
98
+ idField?: string;
99
+ pidField?: string;
100
+ childrenField?: string;
101
+ isDeepCopy?: boolean;
102
+ getData?: (item: any) => any;
103
+ }): any[];
104
+ /**
105
+ * 树型数据转列表数据
106
+ * @param treeData
107
+ * @param settings
108
+ * @param settings.idField 默认值 id
109
+ * @param settings.pidField 默认值 parentId
110
+ * @param settings.childrenField 默认值 children
111
+ * @deprecated 即将移除,使用 {@link CrObjUtil.treeToList} 替代
112
+ * @returns
113
+ */
114
+ static treeDataToListData(treeData: any[], pid?: string | number, settings?: {
115
+ childrenField?: string;
116
+ idField?: string;
117
+ pidField?: string;
118
+ isDeepCopy?: boolean;
119
+ }): any[];
120
+ /**
121
+ * 获取所有父级数据(包含自身)
122
+ * @param listData
123
+ * @param value
124
+ * @param settings
125
+ * @param settings.valueField 默认值 value
126
+ * @param settings.idField 默认值 id
127
+ * @param settings.pidField 默认值 parentId
128
+ * @deprecated 即将移除,使用 {@link CrObjUtil.getFlatParentDatas} 替代
129
+ * @returns
130
+ */
131
+ static getParentNodes<T, R extends Record<string, any>>(listData: R[], value: number | string, settings?: {
132
+ valueField?: string;
133
+ idField?: string;
134
+ pidField?: string;
135
+ getData?: (item: R) => T;
136
+ }): T[];
137
+ /**
138
+ * 参数序列化
139
+ * @param params {a:'1',b:{},c:[]}
140
+ * @returns
141
+ */
142
+ static paramsSerializer(params: Record<string, any>, settings?: {
143
+ isFilterNonNull: boolean;
144
+ }): string;
145
+ /**
146
+ * 参数解析
147
+ * @param {*} str
148
+ * @param {*} settings
149
+ * @returns
150
+ */
151
+ static paramsParse(str: string, settings?: {
152
+ ignoreQueryPrefix: boolean;
153
+ }): {};
154
+ /**
155
+ * 获取 URL 参数
156
+ * @param {*} url
157
+ * @returns
158
+ * @example
159
+ * "https://example.com?foo=bar&baz=qux" => {"query":{"foo":"bar","baz":"qux"},"hash":{},"all":{"foo":"bar","baz":"qux"}}
160
+ * "https://example.com/page?foo=1#/?a=1&b=2" => {"query":{"foo":"1"},"hash":{"/":"","a":"1","b":"2"},"all":{"foo":"1","/":"","a":"1","b":"2"}},
161
+ */
162
+ static getQueryParams(url: string): {
163
+ query: Record<string, any>;
164
+ hash: Record<string, any>;
165
+ all: Record<string, any>;
166
+ };
167
+ /**
168
+ * 版本号比较
169
+ * @param v1
170
+ * @param v2
171
+ * @returns number
172
+ * @description v1大于v2(1)、v1小于v2(-1)、v1等于v2(0)
173
+ */
174
+ static compareVersion(v1: string, v2: string): number;
175
+ /**
176
+ * 根据 ids 获取相对应的数据名称
177
+ * @param data
178
+ * @param ids
179
+ * @param settings
180
+ * @returns string
181
+ */
182
+ static getDataNameByIds<T extends Record<string, any>>(data: T[], ids: (string | number)[], settings?: {
183
+ sep?: string;
184
+ nameField?: string;
185
+ idField?: string;
186
+ }): string;
187
+ /**
188
+ * 追加html标签属性值
189
+ * @param htmlStr
190
+ * @param tagName
191
+ * @param attrName
192
+ * @param newAttrValue
193
+ * @returns string
194
+ */
195
+ static appendHtmlTagAttr(htmlStr: string, tagName: string, attrName: string, newAttrValue: string): string;
196
+ /**
197
+ * 比较数据差异
198
+ * @param data1
199
+ * @param data2
200
+ * @returns
201
+ */
202
+ static compareDataDiff(data1: any, data2: any): Record<string, "added" | "removed" | "modified">;
203
+ /**
204
+ * 格式化为千分位
205
+ * @param value
206
+ * @returns 12345.6789 => 12,345.6789
207
+ */
208
+ static fmtThousands(value: number | string | undefined): string;
209
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcrkey/js-utils",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "一个 javascript 实用函数库",
5
5
  "keywords": [
6
6
  "js"
@@ -10,9 +10,20 @@
10
10
  "url": "https://cnb.cool/cnbkey/zcrkey/js-utils.git"
11
11
  },
12
12
  "license": "MIT",
13
- "main": "dist/index.umd.js",
14
- "module": "dist/index.js",
15
- "types": "dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "require": "./dist/cjs/index.js",
16
+ "import": "./dist/esm/index.js"
17
+ }
18
+ },
19
+ "main": "dist/cjs/index.js",
20
+ "unpkg": "dist/umd/index.umd.js",
21
+ "module": "dist/esm/index.js",
22
+ "types": "dist/esm/index.d.ts",
23
+ "files": [
24
+ "dist",
25
+ "README.html"
26
+ ],
16
27
  "dependencies": {
17
28
  "lodash": "^4.17.21",
18
29
  "qs": "^6.11.2"