jinbi-utils 1.0.19 → 1.0.21
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/.babelrc +19 -0
- package/.cz-config.js +55 -0
- package/.dockerignore +3 -0
- package/.editorconfig +12 -0
- package/.eslintignore +8 -0
- package/.eslintrc.js +54 -0
- package/.versionrc.json +9 -0
- package/CHUNK_OPTIMIZER_USAGE.md +132 -0
- package/Dockerfile +3 -0
- package/QUICK_RELEASE.md +85 -0
- package/RELEASE_GUIDE.md +243 -0
- package/api-extractor.json +15 -0
- package/commitlint.config.js +3 -0
- package/jest.config.js +15 -0
- package/package.json +8 -35
- package/rollup.config.chunk-optimizer.js +32 -0
- package/rollup.config.js +73 -0
- package/src/array/index.ts +85 -0
- package/src/build/chunk-optimizer/ARCHITECTURE.md +347 -0
- package/src/build/chunk-optimizer/QUICK_START.md +370 -0
- package/src/build/chunk-optimizer/README.md +240 -0
- package/src/build/chunk-optimizer/core/chunk-generator.ts +166 -0
- package/src/build/chunk-optimizer/core/classifier.ts +148 -0
- package/src/build/chunk-optimizer/core/dependency-reader.ts +138 -0
- package/src/build/chunk-optimizer/examples/basic-usage.ts +234 -0
- package/src/build/chunk-optimizer/index.ts +166 -0
- package/src/build/chunk-optimizer/rules/common-rules.ts +131 -0
- package/src/build/chunk-optimizer/rules/framework-rules.ts +93 -0
- package/src/build/chunk-optimizer/rules/index.ts +27 -0
- package/src/build/chunk-optimizer/test.ts +94 -0
- package/src/build/chunk-optimizer/types.ts +128 -0
- package/src/color/index.ts +58 -0
- package/src/common/index.ts +353 -0
- package/src/constant/common.constant.ts +13 -0
- package/src/date/index.ts +143 -0
- package/src/dom/index.ts +198 -0
- package/src/file/index.ts +319 -0
- package/src/http/apiBuilder/README.md +648 -0
- package/src/http/apiBuilder/api-builder.ts +502 -0
- package/src/http/apiBuilder/example.ts +243 -0
- package/src/http/apiBuilder/index.ts +1 -0
- package/src/http/apiBuilder//345/277/253/351/200/237/345/217/202/350/200/203.md +199 -0
- package/src/http/http.ts +79 -0
- package/src/http/httpEnums.ts +61 -0
- package/src/iam/index.ts +46 -0
- package/src/index.ts +20 -0
- package/src/middleware/requestLogger.middware.ts +371 -0
- package/src/middleware/requestLoggerUnified.ts +371 -0
- package/src/number/index.ts +362 -0
- package/src/object/index.ts +54 -0
- package/src/print/index.ts +102 -0
- package/src/string/index.ts +189 -0
- package/src/utils/curl.ts +108 -0
- package/src/validate/index.ts +100 -0
- package/src/websocket/emitter.ts +39 -0
- package/src/websocket/index.ts +6 -0
- package/src/websocket/manager.ts +151 -0
- package/src/websocket/pinia-store.ts +91 -0
- package/src/websocket/service.ts +34 -0
- package/src/websocket/types.ts +45 -0
- package/test/common/index.test.ts +19 -0
- package/test/date/index.test.ts +107 -0
- package/test/file/index.test.ts +104 -0
- package/test/number/index.test.ts +108 -0
- package/test/object/index.test.ts +20 -0
- package/test/string/index.test.ts +82 -0
- package/tsconfig.json +39 -0
- package/typedoc.json +12 -0
- package/types/file/index.d.ts +7 -0
- package/types/index.d.ts +1 -0
- package/types/websocket/emitter.d.ts +16 -0
- package/types/websocket/index.d.ts +6 -0
- package/types/websocket/manager.d.ts +36 -0
- package/types/websocket/pinia-store.d.ts +25 -0
- package/types/websocket/service.d.ts +13 -0
- package/types/websocket/types.d.ts +34 -0
- package/dist/chunk-optimizer.cjs +0 -703
- package/dist/index.esm.js +0 -2791
- package/dist/index.esm.min.js +0 -15
- package/dist/index.umd.js +0 -2899
- package/dist/index.umd.min.js +0 -16
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { compatibleDate, newDate, formatTime, fromNow, addDays } from '../../src';
|
|
2
|
+
|
|
3
|
+
describe('compatibleDate', () => {
|
|
4
|
+
test(`compatibleDate('2020-03-04') 返回 2020/03/04`, () => {
|
|
5
|
+
expect(compatibleDate('2020-03-04')).toBe('2020/03/04');
|
|
6
|
+
})
|
|
7
|
+
test(`compatibleDate(Date.now()) 返回 Date.now()`, () => {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
expect(compatibleDate(now)).toBe(now);
|
|
10
|
+
})
|
|
11
|
+
test(`compatibleDate(new Date()) 返回 new Date()`, () => {
|
|
12
|
+
const newDate = new Date();
|
|
13
|
+
expect(compatibleDate(newDate)).toBe(newDate);
|
|
14
|
+
})
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('newDate', () => {
|
|
18
|
+
const date = new Date();
|
|
19
|
+
test(`newDate() 返回 new Date()`, () => {
|
|
20
|
+
expect(new Date(date)).toEqual(date);
|
|
21
|
+
})
|
|
22
|
+
test(`newDate('2020-03-04') 返回 new Date(2020/03/04)`, () => {
|
|
23
|
+
expect(newDate('2020-03-04')).toEqual(new Date('2020/03/04'));
|
|
24
|
+
})
|
|
25
|
+
test(`newDate(date) 返回 date`, () => {
|
|
26
|
+
const date = new Date();
|
|
27
|
+
expect(newDate(date)).toEqual(date);
|
|
28
|
+
})
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('formatTime', () => {
|
|
32
|
+
test(`formatTime() 返回 ${formatTime()}`, () => {
|
|
33
|
+
expect(formatTime()).toBe(formatTime());
|
|
34
|
+
})
|
|
35
|
+
test(`formatTime('2018-02-02 00:00:00') 返回 2018-02-02 00:00:00`, () => {
|
|
36
|
+
expect(formatTime('2018-02-02 00:00:00')).toBe('2018-02-02 00:00:00');
|
|
37
|
+
})
|
|
38
|
+
test(`formatTime('2018-02-02', 'yyyy/MM/dd') 返回 2018/02/02`, () => {
|
|
39
|
+
expect(formatTime('2018-02-02', 'yyyy/MM/dd')).toBe('2018/02/02');
|
|
40
|
+
})
|
|
41
|
+
test(`formatTime('2018-02-02 23:50:50', 'yyyy/MM/dd HH:mm:ss') 返回 2018/02/02 23:50:50`, () => {
|
|
42
|
+
expect(formatTime('2018-02-02 23:50:50', 'yyyy/MM/dd HH:mm:ss')).toBe('2018/02/02 23:50:50');
|
|
43
|
+
})
|
|
44
|
+
test(`formatTime('2018-02-02 23:50:50', 'MM/dd HH:mm:ss') 返回 02/02 23:50:50`, () => {
|
|
45
|
+
expect(formatTime('2018-02-02 23:50:50', 'MM/dd HH:mm:ss')).toBe('02/02 23:50:50');
|
|
46
|
+
})
|
|
47
|
+
test(`formatTime(new Date('2018-02-02 23:50:50') + 200, 'yyyy/MM/dd HH:mm:ss S') 返回 2018/02/02 23:50:50 200`, () => {
|
|
48
|
+
const date = new Date('2018-02-02 23:50:50').getTime() + 200;
|
|
49
|
+
expect(formatTime(date, 'yyyy/MM/dd HH:mm:ss S')).toBe('2018/02/02 23:50:50 200');
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('fromNow', () => {
|
|
54
|
+
const date = new Date();
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
test(`fromNow('') 返回 -`, () => {
|
|
57
|
+
expect(fromNow('')).toBe('-');
|
|
58
|
+
})
|
|
59
|
+
test(`fromNow(Date.now() + 2000) 返回 Date.now() + 2000`, () => {
|
|
60
|
+
expect(fromNow(now + 2000)).toBe(now + 2000);
|
|
61
|
+
})
|
|
62
|
+
test(`fromNow(Date.now()) 返回 刚刚`, () => {
|
|
63
|
+
expect(fromNow(now)).toBe('刚刚');
|
|
64
|
+
})
|
|
65
|
+
test(`fromNow(new Date().getTime()) 返回 刚刚`, () => {
|
|
66
|
+
expect(fromNow(date.getTime())).toBe('刚刚');
|
|
67
|
+
})
|
|
68
|
+
// 之所返回的是 58 是因为 Date.now() 获取的时机在调用之后
|
|
69
|
+
test(`fromNow(now - 3420 * 1000) 返回 58分钟前面`, () => {
|
|
70
|
+
expect(fromNow(now - 3420 * 1000)).toBe('58分钟前');
|
|
71
|
+
})
|
|
72
|
+
test(`fromNow(now - 3600 * 1000 * 3) 返回 3小时前`, () => {
|
|
73
|
+
expect(fromNow(now - 3600 * 1000 * 3)).toBe('3小时前');
|
|
74
|
+
})
|
|
75
|
+
test(`fromNow(now - 3600 * 1000 * 24 * 1.5) 返回 1天前`, () => {
|
|
76
|
+
expect(fromNow(now - 3600 * 1000 * 24 * 1.5)).toBe('1天前');
|
|
77
|
+
})
|
|
78
|
+
const day5 = 3600 * 1000 * 24 * 5;
|
|
79
|
+
test(`fromNow(now - day5) 返回 formatTime(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒')`, () => {
|
|
80
|
+
expect(fromNow(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒')).toBe(formatTime(now - day5, 'yyyy年MM月dd日 HH时mm分ss秒'));
|
|
81
|
+
})
|
|
82
|
+
test(`fromNow('', '', 'defaultValue') 等于 defaultValue`, () => {
|
|
83
|
+
expect(fromNow('', '', 'defaultValue')).toBe('defaultValue');
|
|
84
|
+
})
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('addDays', () => {
|
|
88
|
+
test(`addDays('') 返回 -`, () => {
|
|
89
|
+
expect(addDays('')).toBe('-');
|
|
90
|
+
})
|
|
91
|
+
test(`addDays('2018-02-02') 返回 2018-02-02`, () => {
|
|
92
|
+
expect(addDays('2018-02-02')).toBe('2018-02-02');
|
|
93
|
+
})
|
|
94
|
+
test(`addDays('2018-02-02', 2) 返回 2018-02-04`, () => {
|
|
95
|
+
expect(addDays('2018-02-02', 2)).toBe('2018-02-04');
|
|
96
|
+
})
|
|
97
|
+
test(`addDays('2018-02-02', 2, 'yyyy/MM/dd') 返回 2018/02/04`, () => {
|
|
98
|
+
expect(addDays('2018-02-02', 2, 'yyyy/MM/dd')).toBe('2018/02/04');
|
|
99
|
+
})
|
|
100
|
+
test(`addDays(new Date('2020-03-04'), 2, 'yyyy/MM/dd') 返回 2020/03/06`, () => {
|
|
101
|
+
expect(addDays(new Date('2020-03-04'), 2, 'yyyy/MM/dd')).toBe('2020/03/06');
|
|
102
|
+
})
|
|
103
|
+
test(`addDays(Date.now(), 2, 'yyyy/MM/dd') 返回 今天 + 2天`, () => {
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
expect(addDays(now, 2, 'yyyy/MM/dd')).toBe(formatTime(now + 2 * 24 * 60 * 60 * 1000, 'yyyy/MM/dd'));
|
|
106
|
+
})
|
|
107
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import {
|
|
3
|
+
calcFileSize,
|
|
4
|
+
fileSizeFormat,
|
|
5
|
+
ceil,
|
|
6
|
+
blobToDataURL,
|
|
7
|
+
dataURLtoBlob,
|
|
8
|
+
genExportByBlob,
|
|
9
|
+
} from '../../src';
|
|
10
|
+
|
|
11
|
+
describe('calcFileSize', () => {
|
|
12
|
+
test(`calcFileSize(100) 返回 { size: 100, unit: 'B' }`, () => {
|
|
13
|
+
expect(calcFileSize(100)).toEqual({
|
|
14
|
+
size: 100,
|
|
15
|
+
unit: 'B',
|
|
16
|
+
});
|
|
17
|
+
})
|
|
18
|
+
test(`calcFileSize(1024) 返回 { size: 1, unit: 'KB' }`, () => {
|
|
19
|
+
expect(calcFileSize(1024)).toEqual({
|
|
20
|
+
size: 1,
|
|
21
|
+
unit: 'KB',
|
|
22
|
+
});
|
|
23
|
+
})
|
|
24
|
+
test(`calcFileSize(1024, 'KB') 返回 { size: 1, unit: 'MB' }`, () => {
|
|
25
|
+
expect(calcFileSize(1024, 'KB')).toEqual({
|
|
26
|
+
size: 1,
|
|
27
|
+
unit: 'MB'
|
|
28
|
+
});
|
|
29
|
+
})
|
|
30
|
+
test(`calcFileSize(1126.4, 'mb') 返回 { size: 1.1, unit: 'GB' }`, () => {
|
|
31
|
+
expect(calcFileSize(1126.4, 'mb' as any)).toEqual({
|
|
32
|
+
size: 1.1,
|
|
33
|
+
unit: 'GB'
|
|
34
|
+
});
|
|
35
|
+
})
|
|
36
|
+
test(`calcFileSize(Math.pow(1024, 2), 'kb') 返回 { size: 1, unit: 'GB' }`, () => {
|
|
37
|
+
expect(calcFileSize(Math.pow(1024, 2), 'kb' as any)).toEqual({
|
|
38
|
+
size: 1,
|
|
39
|
+
unit: 'GB'
|
|
40
|
+
});
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('fileSizeFormat', () => {
|
|
45
|
+
test(`fileSizeFormat('') 返回 -`, () => {
|
|
46
|
+
expect(fileSizeFormat('')).toBe('-');
|
|
47
|
+
})
|
|
48
|
+
test(`fileSizeFormat('1024') 返回 1KB`, () => {
|
|
49
|
+
expect(fileSizeFormat('1024')).toBe('1KB');
|
|
50
|
+
})
|
|
51
|
+
test(`fileSizeFormat('1024', 'KB') 返回 1MB`, () => {
|
|
52
|
+
expect(fileSizeFormat('1024', 'KB')).toBe('1MB');
|
|
53
|
+
})
|
|
54
|
+
test(`fileSizeFormat('2645', 'B') 返回 ceil(2645 / 1024, 2)MB`, () => {
|
|
55
|
+
expect(fileSizeFormat('2645', 'KB')).toBe(`${ceil(2645 / 1024, 2)}MB`);
|
|
56
|
+
})
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('blobToDataURL', () => {
|
|
60
|
+
test(`blobToDataURL(Blob) 返回 base64`, async () => {
|
|
61
|
+
const res = await axios.get('https://img.baibu.la/baibu_520ac52acfaa48ddbaa55b8f7f1b8a13.png', {
|
|
62
|
+
responseType: 'blob',
|
|
63
|
+
});
|
|
64
|
+
const base64 = await blobToDataURL(res.data);
|
|
65
|
+
expect(base64.includes('base64')).toBe(true);
|
|
66
|
+
})
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('dataURLtoBlob', () => {
|
|
70
|
+
test(`dataURLtoBlob(base64) 返回 Blob`, async () => {
|
|
71
|
+
const res = await axios.get('https://img.baibu.la/baibu_520ac52acfaa48ddbaa55b8f7f1b8a13.png', {
|
|
72
|
+
responseType: 'blob',
|
|
73
|
+
});
|
|
74
|
+
const base64 = await blobToDataURL(res.data);
|
|
75
|
+
const blob = dataURLtoBlob(base64);
|
|
76
|
+
expect(blob instanceof Blob).toBe(true);
|
|
77
|
+
})
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// describe('compressImg', () => {
|
|
81
|
+
// test(`compressImg(File) 返回 Blob`, async () => {
|
|
82
|
+
// const res = await axios.get('https://img.baibu.la/baibu_520ac52acfaa48ddbaa55b8f7f1b8a13.png', {
|
|
83
|
+
// responseType: 'blob',
|
|
84
|
+
// });
|
|
85
|
+
|
|
86
|
+
// const blob = await compressImg(res.data);
|
|
87
|
+
// expect(blob instanceof Blob).toBe(true);
|
|
88
|
+
// })
|
|
89
|
+
// });
|
|
90
|
+
|
|
91
|
+
describe('exportByBlob', () => {
|
|
92
|
+
test(`exportByBlob(File) 返回 Blob`, async () => {
|
|
93
|
+
const exportByBlob = genExportByBlob({
|
|
94
|
+
axiosRequest: axios,
|
|
95
|
+
})
|
|
96
|
+
const res = await exportByBlob({
|
|
97
|
+
filename: '测试.png',
|
|
98
|
+
url: 'https://img.baibu.la/baibu_520ac52acfaa48ddbaa55b8f7f1b8a13.png',
|
|
99
|
+
method: 'GET',
|
|
100
|
+
});
|
|
101
|
+
const data = (res as any).data;
|
|
102
|
+
expect(data instanceof Blob).toBe(true);
|
|
103
|
+
})
|
|
104
|
+
});
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { toThousands, convertCurrency, formatFloat, ceil, floor } from '../../src';
|
|
2
|
+
|
|
3
|
+
describe('toThousands', () => {
|
|
4
|
+
test(`toThousands('') 返回 '-'`, () => {
|
|
5
|
+
expect(toThousands('')).toBe('-');
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
test(`toThousands('', '|') 返回 '|'`, () => {
|
|
9
|
+
expect(toThousands('', '|')).toBe('|');
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test(`toThousands(123) 返回 '123'`, () => {
|
|
13
|
+
expect(toThousands(123)).toBe('123');
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test(`toThousands(12323) 返回 '12,323'`, () => {
|
|
17
|
+
expect(toThousands(12323)).toBe('12,323');
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test(`toThousands('12323.12') 返回 '12,323.12'`, () => {
|
|
21
|
+
expect(toThousands('12323.12')).toBe('12,323.12');
|
|
22
|
+
})
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('convertCurrency', () => {
|
|
26
|
+
test(`convertCurrency() 返回 ''`, () => {
|
|
27
|
+
expect(convertCurrency()).toBe('');
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test(`convertCurrency('') 返回 ''`, () => {
|
|
31
|
+
expect(convertCurrency('-')).toBe('零元整');
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test(`convertCurrency(1999999999999999) 返回 ''`, () => {
|
|
35
|
+
const value = '1999999999999999';
|
|
36
|
+
expect(convertCurrency(value)).toBe(value);
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test(`convertCurrency(0.012) 返回 '壹分贰毫'`, () => {
|
|
40
|
+
expect(convertCurrency('0.012')).toBe('壹分贰毫');
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test(`convertCurrency(0) 返回 '零元整'`, () => {
|
|
44
|
+
expect(convertCurrency('0')).toBe('零元整');
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test(`convertCurrency(12.12) 返回 '壹拾贰元壹角贰分'`, () => {
|
|
48
|
+
expect(convertCurrency('12.12')).toBe('壹拾贰元壹角贰分');
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test(`convertCurrency(1234567809) 返回 '壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰零玖元整'`, () => {
|
|
52
|
+
expect(convertCurrency('1234567809')).toBe('壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰零玖元整');
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('formatFloat', () => {
|
|
57
|
+
test(`formatFloat('NaN') 返回 ''`, () => {
|
|
58
|
+
expect(formatFloat('NaN')).toBe('');
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test(`formatFloat('1.2345') 返回 '1.23'`, () => {
|
|
62
|
+
expect(formatFloat('1.2345')).toBe('1.23');
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test(`formatFloat('1.2345') 返回 '1.235'`, () => {
|
|
66
|
+
expect(formatFloat('1.2345', 3)).toBe('1.235');
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test(`formatFloat('1', 3) 返回 '1.000'`, () => {
|
|
70
|
+
expect(formatFloat('1', 3)).toBe('1.000');
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('ceil', () => {
|
|
75
|
+
test(`ceil('') 返回 ''`, () => {
|
|
76
|
+
expect(ceil('')).toBe('');
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test(`ceil('1.23') 返回 2`, () => {
|
|
80
|
+
expect(ceil('1.23')).toBe(2);
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test(`ceil('1.234', 2) 返回 1.24`, () => {
|
|
84
|
+
expect(ceil('1.234', 2)).toBe(1.24);
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test(`ceil('1.230', 2) 返回 1.23`, () => {
|
|
88
|
+
expect(ceil('1.230', 2)).toBe(1.23);
|
|
89
|
+
})
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('floor', () => {
|
|
93
|
+
test(`floor('') 返回 ''`, () => {
|
|
94
|
+
expect(floor('')).toBe('');
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test(`floor('1.23') 返回 1`, () => {
|
|
98
|
+
expect(floor('1.23')).toBe(1);
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test(`floor('1.234', 2) 返回 1.23`, () => {
|
|
102
|
+
expect(floor('1.234', 2)).toBe(1.23);
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test(`floor('1.230', 2) 返回 1.23`, () => {
|
|
106
|
+
expect(floor('1.230', 2)).toBe(1.23);
|
|
107
|
+
})
|
|
108
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { deepEqual } from '../../src';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
describe('deepEqual', () => {
|
|
5
|
+
test(`deepEqual(null, null) 返回 true`, () => {
|
|
6
|
+
expect(deepEqual(null, null)).toBe(true);
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
test(`deepEqual({}, null) 返回 false`, () => {
|
|
10
|
+
expect(deepEqual({}, null)).toBe(false);
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
test(`deepEqual({a: 10}, {a: 10}) 返回 true`, () => {
|
|
14
|
+
expect(deepEqual({a: 10}, {a: 10})).toBe(true);
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test(`deepEqual({a: 10}, {a: 10, b: 20}) 返回 false`, () => {
|
|
18
|
+
expect(deepEqual({a: 10}, {a: 10, b: 20})).toBe(false);
|
|
19
|
+
})
|
|
20
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { formatEmptyValue, formatPhone, formatPhoneHide, formatBank, generateEnglishLetters } from '../../src';
|
|
2
|
+
|
|
3
|
+
describe('formatEmptyValue', () => {
|
|
4
|
+
test(`formatEmptyValue('') 返回 `, () => {
|
|
5
|
+
expect(formatEmptyValue('')).toBe('-');
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
test(`formatEmptyValue('123') 返回 123`, () => {
|
|
9
|
+
expect(formatEmptyValue('123')).toBe('123');
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test(`formatEmptyValue(null) 返回 '-'`, () => {
|
|
13
|
+
expect(formatEmptyValue(null)).toBe('-');
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test(`formatEmptyValue(null, ' ') 返回 ' '`, () => {
|
|
17
|
+
expect(formatEmptyValue(null, ' ')).toBe(' ');
|
|
18
|
+
})
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('formatPhone', () => {
|
|
22
|
+
test(`formatPhone('') 返回 `, () => {
|
|
23
|
+
expect(formatPhone('')).toBe('-');
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test(`formatPhone('', '-', ' ') 返回 ' '`, () => {
|
|
27
|
+
expect(formatPhone('', '-', ' ')).toBe(' ');
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('formatPhone(123) 返回 123', () => {
|
|
31
|
+
expect(formatPhone('123')).toBe('123');
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test(`formatPhone(18211572781) 返回 '182 1157 2781'`, () => {
|
|
35
|
+
expect(formatPhone(18211572781)).toBe('182 1157 2781');
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test(`formatPhone('18211572781', '-') 放回 '182-1157-2781'`, () => {
|
|
39
|
+
expect(formatPhone('18211572781', '-')).toBe('182-1157-2781');
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('formatPhoneHide', () => {
|
|
44
|
+
test(`formatPhoneHide('') 返回 ''`, () => {
|
|
45
|
+
expect(formatPhoneHide('')).toBe('');
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('formatPhoneHide(123) 返回 123', () => {
|
|
49
|
+
expect(formatPhoneHide('123')).toBe('123');
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test(`formatPhoneHide(18211572781) 返回 '182****2781'`, () => {
|
|
53
|
+
expect(formatPhoneHide(18211572781)).toBe('182****2781');
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test(`formatPhoneHide('18211572781', '-') 放回 '182****2781'`, () => {
|
|
57
|
+
expect(formatPhoneHide('18211572781', '-')).toBe('182****2781');
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('formatBank', () => {
|
|
62
|
+
test(`formatBank('') 返回 ''`, () => {
|
|
63
|
+
expect(formatBank('')).toBe('');
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test(`formatBank('', '-') 返回 '-'`, () => {
|
|
67
|
+
expect(formatBank('', '-')).toBe('-');
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test(`formatBank(6282356862823568123) 返回 '6282 3568 6282 3568 123'`, () => {
|
|
71
|
+
expect(formatBank('6282356862823568123')).toBe('6282 3568 6282 3568 123');
|
|
72
|
+
})
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
describe('generateEnglishLetters', () => {
|
|
77
|
+
test(`generateEnglishLetters() 返回 ['A', ..., 'Z']`, () => {
|
|
78
|
+
expect(generateEnglishLetters()).toContain('A');
|
|
79
|
+
expect(generateEnglishLetters()).toContain('Z');
|
|
80
|
+
expect(generateEnglishLetters()).toHaveLength(26);
|
|
81
|
+
})
|
|
82
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"module": "es6",
|
|
5
|
+
"lib": ["ESNext", "dom", "es2015.collection"],
|
|
6
|
+
"removeComments": false,
|
|
7
|
+
"preserveConstEnums": true,
|
|
8
|
+
"moduleResolution": "node",
|
|
9
|
+
"experimentalDecorators": true,
|
|
10
|
+
"noImplicitAny": false,
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"declarationDir": "./types",
|
|
14
|
+
"noUnusedLocals": true,
|
|
15
|
+
"noUnusedParameters": true,
|
|
16
|
+
"strictNullChecks": true,
|
|
17
|
+
"sourceMap": true,
|
|
18
|
+
"baseUrl": ".",
|
|
19
|
+
"rootDir": "./src/",
|
|
20
|
+
"jsx": "preserve",
|
|
21
|
+
"allowJs": true,
|
|
22
|
+
"resolveJsonModule": true,
|
|
23
|
+
"typeRoots": [
|
|
24
|
+
"node_modules/@types",
|
|
25
|
+
"global.d.ts"
|
|
26
|
+
],
|
|
27
|
+
"paths": {
|
|
28
|
+
"@/*": ["./src/*"]
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
"include": [
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"exclude": [
|
|
35
|
+
"node_modules",
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"compileOnSave": true
|
|
39
|
+
}
|
package/typedoc.json
ADDED
package/types/file/index.d.ts
CHANGED
|
@@ -94,3 +94,10 @@ export declare function genExportByBlob({ axiosRequest, notWithCredentials, }: {
|
|
|
94
94
|
axiosRequest: any;
|
|
95
95
|
notWithCredentials?: never[] | undefined;
|
|
96
96
|
}): (config: ExportByBlobParams) => Promise<unknown>;
|
|
97
|
+
/**
|
|
98
|
+
* 将 base64 字符串转换为 File 或 Blob
|
|
99
|
+
* @param {string} base64 - 带有 data URI 的 base64 字符串
|
|
100
|
+
* @param {string} [filename] - 如果提供了,则返回 File;否则返回 Blob
|
|
101
|
+
* @returns {File|Blob}
|
|
102
|
+
*/
|
|
103
|
+
export declare function base64ToFileOrBlob(base64: any, filename: any): Promise<Blob>;
|
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WebSocketMessageType } from './types';
|
|
2
|
+
export declare type WebSocketEmitterEvents = {
|
|
3
|
+
'websocket-connected': boolean;
|
|
4
|
+
'websocket-message': WebSocketMessageType;
|
|
5
|
+
'websocket-error': {
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
'websocket-closed': void;
|
|
9
|
+
};
|
|
10
|
+
export declare class Emitter<E extends Record<string, any>> {
|
|
11
|
+
private listeners;
|
|
12
|
+
on<K extends keyof E>(event: K, handler: (payload: E[K]) => void): () => boolean;
|
|
13
|
+
off<K extends keyof E>(event: K, handler: (payload: E[K]) => void): void;
|
|
14
|
+
emit<K extends keyof E>(event: K, payload: E[K]): void;
|
|
15
|
+
}
|
|
16
|
+
export declare const emitter: Emitter<WebSocketEmitterEvents>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { WebSocketMessageType } from './types';
|
|
2
|
+
declare type EventMap = {
|
|
3
|
+
connected: boolean;
|
|
4
|
+
message: WebSocketMessageType;
|
|
5
|
+
error: {
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
closed: void;
|
|
9
|
+
};
|
|
10
|
+
export interface WebSocketOptions {
|
|
11
|
+
url: string;
|
|
12
|
+
reconnectAttemptsLimit?: number;
|
|
13
|
+
reconnectInterval?: number;
|
|
14
|
+
heartbeatInterval?: number;
|
|
15
|
+
registerMessage?: unknown;
|
|
16
|
+
}
|
|
17
|
+
export declare class WebsocketManager {
|
|
18
|
+
private ws;
|
|
19
|
+
private reconnectAttempts;
|
|
20
|
+
private heartbeatTimer;
|
|
21
|
+
private listeners;
|
|
22
|
+
private readonly opts;
|
|
23
|
+
constructor(options: WebSocketOptions);
|
|
24
|
+
get url(): string;
|
|
25
|
+
on<E extends keyof EventMap>(event: E, handler: (payload: EventMap[E]) => void): () => boolean;
|
|
26
|
+
private emit;
|
|
27
|
+
connect(): void;
|
|
28
|
+
private setupEventListeners;
|
|
29
|
+
send(data: unknown): void;
|
|
30
|
+
private startHeartbeat;
|
|
31
|
+
private stopHeartbeat;
|
|
32
|
+
private reconnect;
|
|
33
|
+
disconnect(): void;
|
|
34
|
+
get isConnected(): boolean;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { WebsocketManager } from './manager';
|
|
2
|
+
import type { WebSocketMessageType } from './types';
|
|
3
|
+
import type { WebSocketOptions } from './manager';
|
|
4
|
+
declare type MessageWithMeta = WebSocketMessageType & {
|
|
5
|
+
timestamp?: number;
|
|
6
|
+
id?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const createWebSocketStore: (storeId?: string) => import("pinia").StoreDefinition<string, {
|
|
9
|
+
isConnected: boolean;
|
|
10
|
+
wsManager: WebsocketManager | null;
|
|
11
|
+
messages: MessageWithMeta[];
|
|
12
|
+
connectionError: string | null;
|
|
13
|
+
}, {
|
|
14
|
+
connectionStatus: (state: any) => "已连接" | "未连接";
|
|
15
|
+
lastMessage: (state: any) => any;
|
|
16
|
+
}, {
|
|
17
|
+
connect(options: WebSocketOptions): Promise<void>;
|
|
18
|
+
setupEventListeners(): void;
|
|
19
|
+
sendMessage(data: WebSocketMessageType): void;
|
|
20
|
+
addMessage(message: WebSocketMessageType): void;
|
|
21
|
+
clearMessages(): void;
|
|
22
|
+
disconnect(): void;
|
|
23
|
+
reset(): void;
|
|
24
|
+
}>;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { WebsocketManager } from './manager';
|
|
2
|
+
import type { WebSocketOptions } from './manager';
|
|
3
|
+
declare class WebSocketService {
|
|
4
|
+
private static instance;
|
|
5
|
+
private initialized;
|
|
6
|
+
private manager;
|
|
7
|
+
static getInstance(): WebSocketService;
|
|
8
|
+
init(options: WebSocketOptions): Promise<void>;
|
|
9
|
+
destroy(): void;
|
|
10
|
+
getManager(): WebsocketManager | null;
|
|
11
|
+
}
|
|
12
|
+
export declare const websocketService: WebSocketService;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface WebSocketMessage<T = unknown> {
|
|
2
|
+
type: string;
|
|
3
|
+
data?: T;
|
|
4
|
+
timestamp?: number;
|
|
5
|
+
messageId?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface WebSocketHeartbeat {
|
|
8
|
+
type: 'ping' | 'pong';
|
|
9
|
+
}
|
|
10
|
+
export interface WebSocketNotification {
|
|
11
|
+
type: 'notification';
|
|
12
|
+
data: {
|
|
13
|
+
title: string;
|
|
14
|
+
message: string;
|
|
15
|
+
level?: 'info' | 'success' | 'warning' | 'error';
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface WebSocketDataUpdate<T = unknown> {
|
|
19
|
+
type: 'data-update';
|
|
20
|
+
data: {
|
|
21
|
+
entity: string;
|
|
22
|
+
action: 'create' | 'update' | 'delete';
|
|
23
|
+
payload: T;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface WebSocketSystemMessage {
|
|
27
|
+
type: 'system';
|
|
28
|
+
data: {
|
|
29
|
+
code: string;
|
|
30
|
+
message: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export declare type WebSocketMessageType = WebSocketHeartbeat | WebSocketNotification | WebSocketDataUpdate | WebSocketSystemMessage | WebSocketMessage;
|
|
34
|
+
export declare type WebSocketStatus = 'connected' | 'disconnected' | 'connecting' | 'error';
|