@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,266 @@
1
+ /**
2
+ * title: 基本使用
3
+ */
4
+
5
+ import { CrTreeUtil } from '@zcrkey/js-utils';
6
+ import React, { useState } from 'react';
7
+
8
+ const listData = [
9
+ { id: '1', parentId: null, name: '节点 1' },
10
+ { id: '1-1', parentId: '1', name: '节点 1-1' },
11
+ { id: '1-1-1', parentId: '1-1', name: '节点 1-1-1' },
12
+ { id: '1-1-2', parentId: '1-1', name: '节点 1-1-2' },
13
+ { id: '1-2', parentId: '1', name: '节点 1-2' },
14
+ { id: '1-2-1', parentId: '1-2', name: '节点 1-2-1' },
15
+ { id: '1-2-1-1', parentId: '1-2-1', name: '节点 1-2-1-1' },
16
+
17
+ { id: '2', parentId: null, name: '节点 2' },
18
+ { id: '2-1', parentId: '2', name: '节点 2-1' },
19
+ { id: '2-1-1', parentId: '2-1', name: '节点 2-1-1' },
20
+ { id: '2-1-2', parentId: '2-1', name: '节点 2-1-2' },
21
+ { id: '2-2', parentId: '2', name: '节点 2-2' },
22
+
23
+ { id: '3', parentId: null, name: '节点 3' },
24
+ { id: '3-1', parentId: '3', name: '节点 3-1' },
25
+ { id: '3-1-1', parentId: '3-1', name: '节点 3-1-1' },
26
+ { id: '3-1-1-1', parentId: '3-1-1', name: '节点 3-1-1-1' },
27
+ { id: '3-2', parentId: '3', name: '节点 3-2' },
28
+ { id: '3-2-1', parentId: '3-2', name: '节点 3-2-1' },
29
+
30
+ { id: '4', parentId: null, name: '节点 4' },
31
+ ];
32
+
33
+ const treeData = [
34
+ {
35
+ id: '1',
36
+ parentId: null,
37
+ name: '节点 1',
38
+ children: [
39
+ {
40
+ id: '1-1',
41
+ parentId: '1',
42
+ name: '节点 1-1',
43
+ children: [
44
+ {
45
+ id: '1-1-1',
46
+ parentId: '1-1',
47
+ name: '节点 1-1-1',
48
+ },
49
+ {
50
+ id: '1-1-2',
51
+ parentId: '1-1',
52
+ name: '节点 1-1-2',
53
+ },
54
+ ],
55
+ },
56
+ {
57
+ id: '1-2',
58
+ parentId: '1',
59
+ name: '节点 1-2',
60
+ children: [
61
+ {
62
+ id: '1-2-1',
63
+ parentId: '1-2',
64
+ name: '节点 1-2-1',
65
+ children: [
66
+ {
67
+ id: '1-2-1-1',
68
+ parentId: '1-2-1',
69
+ name: '节点 1-2-1-1',
70
+ },
71
+ ],
72
+ },
73
+ ],
74
+ },
75
+ ],
76
+ },
77
+ {
78
+ id: '2',
79
+ parentId: null,
80
+ name: '节点 2',
81
+ children: [
82
+ {
83
+ id: '2-1',
84
+ parentId: '2',
85
+ name: '节点 2-1',
86
+ children: [
87
+ {
88
+ id: '2-1-1',
89
+ parentId: '2-1',
90
+ name: '节点 2-1-1',
91
+ },
92
+ {
93
+ id: '2-1-2',
94
+ parentId: '2-1',
95
+ name: '节点 2-1-2',
96
+ },
97
+ ],
98
+ },
99
+ {
100
+ id: '2-2',
101
+ parentId: '2',
102
+ name: '节点 2-2',
103
+ },
104
+ ],
105
+ },
106
+ {
107
+ id: '3',
108
+ parentId: null,
109
+ name: '节点 3',
110
+ children: [
111
+ {
112
+ id: '3-1',
113
+ parentId: '3',
114
+ name: '节点 3-1',
115
+ children: [
116
+ {
117
+ id: '3-1-1',
118
+ parentId: '3-1',
119
+ name: '节点 3-1-1',
120
+ children: [
121
+ {
122
+ id: '3-1-1-1',
123
+ parentId: '3-1-1',
124
+ name: '节点 3-1-1-1',
125
+ },
126
+ ],
127
+ },
128
+ ],
129
+ },
130
+ {
131
+ id: '3-2',
132
+ parentId: '3',
133
+ name: '节点 3-2',
134
+ children: [
135
+ {
136
+ id: '3-2-1',
137
+ parentId: '3-2',
138
+ name: '节点 3-2-1',
139
+ },
140
+ ],
141
+ },
142
+ ],
143
+ },
144
+ {
145
+ id: '4',
146
+ parentId: null,
147
+ name: '节点 4',
148
+ },
149
+ ];
150
+
151
+ export default () => {
152
+ const [result, setResult] = useState<any>({});
153
+
154
+ const listToTree = () => {
155
+ let res = CrTreeUtil.listToTree(listData, {
156
+ isDeepCopy: true,
157
+ });
158
+ setResult({
159
+ ...result,
160
+ listToTree: res,
161
+ });
162
+ };
163
+
164
+ const treeToList = () => {
165
+ let res = CrTreeUtil.treeToList(treeData, {
166
+ isDeepCopy: true,
167
+ });
168
+ setResult({
169
+ ...result,
170
+ treeToList: res,
171
+ });
172
+ };
173
+
174
+ const getFlatParentDatas = () => {
175
+ let res = CrTreeUtil.getFlatParentDatas(listData, '3-1-1-1', {
176
+ valueField: 'id',
177
+ getData: (item: any) => item,
178
+ });
179
+ setResult({
180
+ ...result,
181
+ getFlatParentDatas: res,
182
+ });
183
+ };
184
+
185
+ return (
186
+ <>
187
+ <div className="flex flex-direction">
188
+ <div>列表数据转树型数据</div>
189
+ <div className="text-lg">CrTreeUtil.listToTree</div>
190
+ <div>
191
+ <button
192
+ type="button"
193
+ className="cr-btn line-blue radius"
194
+ onClick={listToTree}
195
+ >
196
+ 调用
197
+ </button>
198
+ <div>
199
+ <div style={{ display: 'inline-grid', width: '50%' }}>
200
+ <pre style={{ marginBottom: '0px' }}>
201
+ {JSON.stringify(listData, null, 2)}
202
+ </pre>
203
+ </div>
204
+ <div style={{ display: 'inline-grid', width: '50%' }}>
205
+ <pre style={{ marginBottom: '0px' }}>
206
+ {JSON.stringify(result.listToTree, null, 2)}
207
+ </pre>
208
+ </div>
209
+ </div>
210
+ </div>
211
+ </div>
212
+ <div className="divider"></div>
213
+ <div className="flex flex-direction">
214
+ <div>树型数据转列表数据</div>
215
+ <div className="text-lg">CrTreeUtil.treeToList</div>
216
+ <div>
217
+ <button
218
+ type="button"
219
+ className="cr-btn line-blue radius"
220
+ onClick={treeToList}
221
+ >
222
+ 调用
223
+ </button>
224
+ <div>
225
+ <div style={{ display: 'inline-grid', width: '50%' }}>
226
+ <pre style={{ marginBottom: '0px' }}>
227
+ {JSON.stringify(treeData, null, 2)}
228
+ </pre>
229
+ </div>
230
+ <div style={{ display: 'inline-grid', width: '50%' }}>
231
+ <pre style={{ marginBottom: '0px' }}>
232
+ {JSON.stringify(result.treeToList, null, 2)}
233
+ </pre>
234
+ </div>
235
+ </div>
236
+ </div>
237
+ </div>
238
+ <div className="divider"></div>
239
+ <div className="flex flex-direction">
240
+ <div>获取扁平化父级数据</div>
241
+ <div className="text-lg">CrTreeUtil.getFlatParentDatas</div>
242
+ <div>
243
+ <button
244
+ type="button"
245
+ className="cr-btn line-blue radius"
246
+ onClick={getFlatParentDatas}
247
+ >
248
+ 调用
249
+ </button>
250
+ <div>
251
+ <div style={{ display: 'inline-grid', width: '50%' }}>
252
+ <pre style={{ marginBottom: '0px' }}>
253
+ 值:'3-1-1-1' 数据源:{JSON.stringify(listData, null, 2)}
254
+ </pre>
255
+ </div>
256
+ <div style={{ display: 'inline-grid', width: '50%' }}>
257
+ <pre style={{ marginBottom: '0px' }}>
258
+ {JSON.stringify(result.getFlatParentDatas, null, 2)}
259
+ </pre>
260
+ </div>
261
+ </div>
262
+ </div>
263
+ </div>
264
+ </>
265
+ );
266
+ };
@@ -0,0 +1,6 @@
1
+ # CrUtil 实用函数
2
+
3
+ 实用函数
4
+
5
+ <code src="./index.tsx"></code>
6
+ <code src="./is.tsx"></code>
@@ -0,0 +1,405 @@
1
+ /**
2
+ * title: 基本使用
3
+ */
4
+
5
+ import { CrUtil } from '@zcrkey/js-utils';
6
+ import React, { useState } from 'react';
7
+
8
+ export default () => {
9
+ const [result, setResult] = useState<any>({});
10
+
11
+ const trim = () => {
12
+ let value1 = ' XXX ';
13
+ let value2 = 'XXX ';
14
+ let value3 = ' XXX';
15
+ let value4 = ' X X X ';
16
+ let res: any[] = [
17
+ `${value1} => ${CrUtil.trim(value1)}`,
18
+ `${value2} => ${CrUtil.trim(value2)}`,
19
+ `${value3} => ${CrUtil.trim(value3)}`,
20
+ `${value4} => ${CrUtil.trim(value4)}`,
21
+ ];
22
+ setResult({
23
+ ...result,
24
+ trim: res,
25
+ });
26
+ };
27
+
28
+ const getArrayDimension = () => {
29
+ let value1: any[] = [];
30
+ let value2: any[] = [[], 1];
31
+ let value3: any[] = [[], 1, [[], 34]];
32
+ let value4: any[] = [1, 2, 3];
33
+ let res: any[] = [
34
+ `${JSON.stringify(value1)} => ${CrUtil.getArrayDimension(value1)}维`,
35
+ `${JSON.stringify(value2)} => ${CrUtil.getArrayDimension(value2)}维`,
36
+ `${JSON.stringify(value3)} => ${CrUtil.getArrayDimension(value3)}维`,
37
+ `${JSON.stringify(value4)} => ${CrUtil.getArrayDimension(value4)}维`,
38
+ ];
39
+ setResult({
40
+ ...result,
41
+ getArrayDimension: res,
42
+ });
43
+ };
44
+
45
+ const deepCopy = () => {
46
+ let value1: any[] = [
47
+ {
48
+ number: 1,
49
+ string: 'zcr',
50
+ obj: {},
51
+ arr: [1, 2, 3],
52
+ },
53
+ ];
54
+ let value2: any = {
55
+ number: 1,
56
+ string: 'zcr',
57
+ obj: {
58
+ a: 123,
59
+ },
60
+ arr: [],
61
+ };
62
+
63
+ let value3 = CrUtil.deepCopy(value1);
64
+ let value4 = CrUtil.deepCopy(value2);
65
+
66
+ value3[0].arr.push(4);
67
+ value4.obj.a = 456;
68
+
69
+ let res: any[] = [
70
+ `${JSON.stringify(value1)} => ${JSON.stringify(value3)}`,
71
+ `${JSON.stringify(value2)} => ${JSON.stringify(value4)}`,
72
+ ];
73
+ setResult({
74
+ ...result,
75
+ deepCopy: res,
76
+ });
77
+ };
78
+
79
+ const compareVersion = () => {
80
+ let v1 = '1.2.3';
81
+ let v2 = '1.0.3';
82
+ let res: any[] = [
83
+ `${v1}和${v2} => ${CrUtil.compareVersion(v1, v2)}`,
84
+ `${v2}和${v1} => ${CrUtil.compareVersion(v2, v1)}`,
85
+ `${v1}和${v1} => ${CrUtil.compareVersion(v1, v1)}`,
86
+ ];
87
+ setResult({
88
+ ...result,
89
+ compareVersion: res,
90
+ });
91
+ };
92
+
93
+ const compareDataDiff = () => {
94
+ let data1: any = {
95
+ a: 1,
96
+ b: 1,
97
+ d: 4,
98
+ arr: [1],
99
+ arr2: [1, 2],
100
+ arr3: [{ aa: 1 }],
101
+ arr4: [{ aa: 2 }, { cc: 2 }],
102
+ };
103
+ let data2: any = {
104
+ a: 1,
105
+ b: 2,
106
+ c: 3,
107
+ arr: [1, 2],
108
+ arr2: [1],
109
+ arr3: [{ aa: 2 }, { cc: 2 }],
110
+ arr4: [{ aa: 1 }],
111
+ };
112
+ let res: any[] = [
113
+ `${JSON.stringify(data1)}和${JSON.stringify(data2)} => ${JSON.stringify(
114
+ CrUtil.compareDataDiff(data1, data2),
115
+ )}`,
116
+ ];
117
+ setResult({
118
+ ...result,
119
+ compareDataDiff: res,
120
+ });
121
+ };
122
+
123
+ const paramsSerializer = () => {
124
+ let v1 = { a: '1', b: {}, c: [] };
125
+ let v2 = { a: '1', b: { b1: 1, b2: 'zcr' }, c: [1, 2, 3] };
126
+ let res: any[] = [
127
+ `${JSON.stringify(v1)} => ${CrUtil.paramsSerializer(v1)}`,
128
+ `${JSON.stringify(v2)} => ${CrUtil.paramsSerializer(v2)}`,
129
+ ];
130
+ setResult({
131
+ ...result,
132
+ paramsSerializer: res,
133
+ });
134
+ };
135
+
136
+ const paramsParse = () => {
137
+ let v1 = `a=1`;
138
+ let v2 = `a=1&b%5Bb1%5D=1&b%5Bb2%5D=zcr&c%5B0%5D=1&c%5B1%5D=2&c%5B2%5D=3`;
139
+ let res: any[] = [
140
+ `${v1} => ${JSON.stringify(CrUtil.paramsParse(v1))}`,
141
+ `${v2} => ${JSON.stringify(CrUtil.paramsParse(v2))}`,
142
+ ];
143
+ setResult({
144
+ ...result,
145
+ paramsParse: res,
146
+ });
147
+ };
148
+
149
+ const listData = [
150
+ // { id: 1, name: '1', parentId: 0 },
151
+ // { id: 2, name: '1-1', parentId: 1 },
152
+ // { id: 3, name: '1-2', parentId: 1 },
153
+ // { id: 4, name: '1-1-1', parentId: 2 },
154
+ // { id: 5, name: '1-1-2', parentId: 2 },
155
+ { id: 1, parentId: null, name: 'Root A' },
156
+ { id: 2, parentId: 1, name: 'Child A1' },
157
+ { id: 3, parentId: 1, name: 'Child A2' },
158
+ { id: 4, parentId: 2, name: 'Grandchild A1-1' },
159
+ { id: 5, parentId: 0, name: 'Root B' },
160
+ { id: 6, parentId: 5, name: 'Child B1' },
161
+ { id: 7, parentId: 999, name: 'Orphan Node' },
162
+ ];
163
+
164
+ const listToTreeData = () => {
165
+ let res = CrUtil.listToTreeData(listData, {
166
+ isDeepCopy: true,
167
+ });
168
+ setResult({
169
+ ...result,
170
+ listToTreeData: res,
171
+ });
172
+ };
173
+
174
+ const urls = [
175
+ 'https://example.com/#/?a=1&b=2',
176
+ 'https://example.com/page?foo=1#/?a=1&b=2',
177
+ 'https://example.com/page#/?a=1&b=2',
178
+ 'https://example.com/page#/page?a=123&b=456',
179
+ 'http://10.0.9.200:8862/index.html#/menu?clientType=pc',
180
+ 'http://10.0.9.200:8862/index.html#/url=http://https://example.com?a=1&b=2&c=3&d=4',
181
+ 'http://10.0.9.200:8862/index.html#/?url=http://https://example.com?a=1&b=2&c=3&d=4',
182
+ 'https://example.com',
183
+ 'https://example.com/path/to/resource',
184
+ 'https://example.com?foo=bar&baz=qux',
185
+ 'https://example.com#section2',
186
+ 'https://example.com/path?foo=1#bar=2&baz=3',
187
+ 'https://example.com/page?url=http%3A%2F%2Fother.com%3Fa%3D1%26b%3D2',
188
+ 'https://example.com/page?foo=1&bar=2#id=3&name=Tom',
189
+ 'https://example.com/page#id=2&flag',
190
+ 'https://example.com/page',
191
+ ];
192
+
193
+ const getQueryParams = () => {
194
+ let res = [];
195
+ for (let url of urls) {
196
+ res.push(CrUtil.getQueryParams(url));
197
+ }
198
+ setResult({
199
+ ...result,
200
+ getQueryParams: res,
201
+ });
202
+ };
203
+
204
+ function stringifyFirstLevelOnly(obj: Record<string, any>): string {
205
+ if (obj === null || typeof obj !== 'object') {
206
+ return JSON.stringify(obj);
207
+ }
208
+ const entries = Object.keys(obj).map((key) => {
209
+ const valueStr = JSON.stringify(obj[key]);
210
+ return ` "${key}": ${valueStr}`;
211
+ });
212
+ return `{\n${entries.join(',\n')}\n}`;
213
+ }
214
+
215
+ return (
216
+ <>
217
+ <div className="flex flex-direction">
218
+ <div>获取URL参数</div>
219
+ <div className="text-lg">CrUtil.getQueryParams</div>
220
+ <div>
221
+ <button
222
+ type="button"
223
+ className="cr-btn line-blue radius"
224
+ onClick={getQueryParams}
225
+ >
226
+ 调用
227
+ </button>
228
+ <div>
229
+ <div style={{ display: 'inline-grid', width: '50%' }}>
230
+ <pre style={{ marginBottom: '0px' }}>
231
+ {JSON.stringify(urls, null, 2)}
232
+ </pre>
233
+ </div>
234
+ <div style={{ display: 'inline-grid', width: '50%' }}>
235
+ <pre style={{ marginBottom: '0px' }}>
236
+ {stringifyFirstLevelOnly(result.getQueryParams)}
237
+ </pre>
238
+ </div>
239
+ </div>
240
+ </div>
241
+ </div>
242
+ <div className="divider"></div>
243
+ <div className="flex flex-direction">
244
+ <div>去掉字符串前后所有空格</div>
245
+ <div className="text-lg">CrUtil.trim</div>
246
+ <div>
247
+ <button
248
+ type="button"
249
+ className="cr-btn line-blue radius"
250
+ onClick={trim}
251
+ >
252
+ 调用
253
+ </button>
254
+ <pre>
255
+ {result.trim &&
256
+ result.trim.map((str: string) => {
257
+ return <div key={str}>{str}</div>;
258
+ })}
259
+ </pre>
260
+ </div>
261
+ </div>
262
+ <div className="divider"></div>
263
+ <div className="flex flex-direction">
264
+ <div>获取数组为几维数组</div>
265
+ <div className="text-lg">CrUtil.getArrayDimension</div>
266
+ <div>
267
+ <button
268
+ type="button"
269
+ className="cr-btn line-blue radius"
270
+ onClick={getArrayDimension}
271
+ >
272
+ 调用
273
+ </button>
274
+ <div>
275
+ {result.getArrayDimension &&
276
+ result.getArrayDimension.map((str: number) => {
277
+ return <div key={str}>{str}</div>;
278
+ })}
279
+ </div>
280
+ </div>
281
+ </div>
282
+ <div className="divider"></div>
283
+ <div className="flex flex-direction">
284
+ <div>深拷贝</div>
285
+ <div className="text-lg">CrUtil.deepCopy</div>
286
+ <div>
287
+ <button
288
+ type="button"
289
+ className="cr-btn line-blue radius"
290
+ onClick={deepCopy}
291
+ >
292
+ 调用
293
+ </button>
294
+ <div>
295
+ {result.deepCopy &&
296
+ result.deepCopy.map((str: string) => {
297
+ return <div key={str}>{str}</div>;
298
+ })}
299
+ </div>
300
+ </div>
301
+ </div>
302
+ <div className="divider"></div>
303
+ <div className="flex flex-direction">
304
+ <div>比较数据差异</div>
305
+ <div className="text-lg">CrUtil.compareDataDiff</div>
306
+ <div>
307
+ <button
308
+ type="button"
309
+ className="cr-btn line-blue radius"
310
+ onClick={compareDataDiff}
311
+ >
312
+ 调用
313
+ </button>
314
+ <div>
315
+ {result.compareDataDiff &&
316
+ result.compareDataDiff.map((str: string) => {
317
+ return <div key={str}>{str}</div>;
318
+ })}
319
+ </div>
320
+ </div>
321
+ </div>
322
+ <div className="divider"></div>
323
+ <div className="flex flex-direction">
324
+ <div>版本号比较</div>
325
+ <div className="text-lg">CrUtil.compareVersion</div>
326
+ <div>
327
+ <button
328
+ type="button"
329
+ className="cr-btn line-blue radius"
330
+ onClick={compareVersion}
331
+ >
332
+ 调用
333
+ </button>
334
+ <div>
335
+ {result.compareVersion &&
336
+ result.compareVersion.map((str: string) => {
337
+ return <div key={str}>{str}</div>;
338
+ })}
339
+ </div>
340
+ </div>
341
+ </div>
342
+ <div className="divider"></div>
343
+ <div className="flex flex-direction">
344
+ <div>参数序列化</div>
345
+ <div className="text-lg">CrUtil.paramsSerializer</div>
346
+ <div>参数解析</div>
347
+ <div className="text-lg">CrUtil.paramsParse</div>
348
+ <div>
349
+ <button
350
+ type="button"
351
+ className="cr-btn line-blue radius"
352
+ onClick={paramsSerializer}
353
+ >
354
+ 序列化
355
+ </button>
356
+ <button
357
+ type="button"
358
+ className="cr-btn line-blue radius"
359
+ onClick={paramsParse}
360
+ >
361
+ 参数解析
362
+ </button>
363
+ <div>
364
+ {result.paramsSerializer &&
365
+ result.paramsSerializer.map((str: string) => {
366
+ return <div key={str}>{str}</div>;
367
+ })}
368
+ </div>
369
+ <div>
370
+ {result.paramsParse &&
371
+ result.paramsParse.map((str: string) => {
372
+ return <div key={str}>{str}</div>;
373
+ })}
374
+ </div>
375
+ </div>
376
+ </div>
377
+ <div className="divider"></div>
378
+ <div className="flex flex-direction">
379
+ <div>列表数据转树型数据</div>
380
+ <div className="text-lg">CrUtil.listToTreeData</div>
381
+ <div>
382
+ <button
383
+ type="button"
384
+ className="cr-btn line-blue radius"
385
+ onClick={listToTreeData}
386
+ >
387
+ 调用
388
+ </button>
389
+ <div>
390
+ <div style={{ display: 'inline-grid', width: '50%' }}>
391
+ <pre style={{ marginBottom: '0px' }}>
392
+ {JSON.stringify(listData, null, 2)}
393
+ </pre>
394
+ </div>
395
+ <div style={{ display: 'inline-grid', width: '50%' }}>
396
+ <pre style={{ marginBottom: '0px' }}>
397
+ {JSON.stringify(result.listToTreeData, null, 2)}
398
+ </pre>
399
+ </div>
400
+ </div>
401
+ </div>
402
+ </div>
403
+ </>
404
+ );
405
+ };