@zcrkey/js-utils 0.0.5 → 0.0.7
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 +9 -0
- package/dist/cjs/color.d.ts +195 -0
- package/dist/cjs/color.js +353 -0
- package/{src/eventCenter.ts → dist/cjs/eventCenter.js} +120 -112
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +54 -0
- package/dist/cjs/obj.js +37 -0
- package/{src/storage.ts → dist/cjs/storage.js} +118 -101
- package/dist/cjs/tree.js +145 -0
- package/dist/{util.d.ts → cjs/util.d.ts} +3 -3
- package/{src/util.ts → dist/cjs/util.js} +173 -257
- package/dist/esm/color.d.ts +195 -0
- package/dist/esm/color.js +500 -0
- package/dist/esm/eventCenter.d.ts +59 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/obj.d.ts +12 -0
- package/dist/{objUtil.js → esm/obj.js} +6 -6
- package/dist/esm/storage.d.ts +44 -0
- package/dist/esm/tree.d.ts +48 -0
- package/dist/esm/util.d.ts +209 -0
- package/dist/{util.js → esm/util.js} +129 -129
- package/dist/{index.umd.js → umd/index.umd.js} +1 -1
- package/package.json +15 -4
- package/.dumi/global.less +0 -1396
- package/.dumirc.ts +0 -36
- package/.fatherrc.ts +0 -14
- package/.husky/commit-msg +0 -4
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -2
- package/.stylelintrc +0 -10
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -5
- package/docs/api/eventCenter/index.md +0 -34
- package/docs/api/index.md +0 -5
- package/docs/api/storage/index.md +0 -9
- package/docs/api/storage/local.tsx +0 -91
- package/docs/api/storage/session.tsx +0 -85
- package/docs/api/treeUtil/index.md +0 -5
- package/docs/api/treeUtil/index.tsx +0 -266
- package/docs/api/util/index.md +0 -6
- package/docs/api/util/index.tsx +0 -405
- package/docs/api/util/is.tsx +0 -196
- package/docs/guide.md +0 -24
- package/src/index.ts +0 -8
- package/src/objUtil.ts +0 -20
- package/src/treeUtil.ts +0 -164
- package/tsconfig.json +0 -18
- /package/dist/{eventCenter.d.ts → cjs/eventCenter.d.ts} +0 -0
- /package/dist/{objUtil.d.ts → cjs/obj.d.ts} +0 -0
- /package/dist/{storage.d.ts → cjs/storage.d.ts} +0 -0
- /package/dist/{treeUtil.d.ts → cjs/tree.d.ts} +0 -0
- /package/dist/{eventCenter.js → esm/eventCenter.js} +0 -0
- /package/dist/{storage.js → esm/storage.js} +0 -0
- /package/dist/{treeUtil.js → esm/tree.js} +0 -0
package/docs/api/util/index.tsx
DELETED
|
@@ -1,405 +0,0 @@
|
|
|
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
|
-
};
|
package/docs/api/util/is.tsx
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# @zcrkey/js-utils
|
|
2
|
-
|
|
3
|
-
<!-- [](https://g-nhxn7953.coding.net/public-artifacts/zcrkey/npm/@zcrkey/js-utils/version/40193242/overview)
|
|
4
|
-
[](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/src/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|