jinbi-utils 1.0.0-beta.1

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 (72) hide show
  1. package/.babelrc +19 -0
  2. package/.cz-config.js +55 -0
  3. package/.dockerignore +3 -0
  4. package/.editorconfig +12 -0
  5. package/.eslintignore +8 -0
  6. package/.eslintrc.js +54 -0
  7. package/Dockerfile +3 -0
  8. package/README.md +160 -0
  9. package/api-extractor.json +15 -0
  10. package/commitlint.config.js +3 -0
  11. package/dist/index.esm.js +1277 -0
  12. package/dist/index.esm.min.js +15 -0
  13. package/dist/index.umd.js +1348 -0
  14. package/dist/index.umd.min.js +16 -0
  15. package/docs/assets/images/icons.png +0 -0
  16. package/docs/assets/images/icons@2x.png +0 -0
  17. package/docs/assets/images/widgets.png +0 -0
  18. package/docs/assets/images/widgets@2x.png +0 -0
  19. package/docs/assets/js/main.js +1 -0
  20. package/docs/assets/js/search.json +1 -0
  21. package/docs/globals.html +144 -0
  22. package/docs/index.html +147 -0
  23. package/docs/interfaces/file.compressimgqualitycallback.html +212 -0
  24. package/docs/interfaces/file.compressimgscalecallback.html +206 -0
  25. package/docs/interfaces/file.exportbyblobparams.html +294 -0
  26. package/docs/interfaces/file.filesizeobject.html +227 -0
  27. package/docs/interfaces/file.genexportbyblobparams.html +237 -0
  28. package/docs/modules/common.html +188 -0
  29. package/docs/modules/date.html +364 -0
  30. package/docs/modules/file.html +452 -0
  31. package/docs/modules/number.html +356 -0
  32. package/docs/modules/object.html +245 -0
  33. package/docs/modules/print.html +183 -0
  34. package/docs/modules/string.html +352 -0
  35. package/docs/modules/validate.html +389 -0
  36. package/jest.config.js +15 -0
  37. package/package.json +76 -0
  38. package/rollup.config.js +65 -0
  39. package/src/common/index.ts +323 -0
  40. package/src/constant/common.constant.ts +13 -0
  41. package/src/date/index.ts +143 -0
  42. package/src/file/index.ts +296 -0
  43. package/src/http/http.ts +79 -0
  44. package/src/http/httpEnums.ts +61 -0
  45. package/src/index.ts +10 -0
  46. package/src/number/index.ts +190 -0
  47. package/src/object/index.ts +54 -0
  48. package/src/print/index.ts +102 -0
  49. package/src/string/index.ts +111 -0
  50. package/src/validate/index.ts +78 -0
  51. package/src/wecom/wecom.ts +75 -0
  52. package/test/common/index.test.ts +19 -0
  53. package/test/date/index.test.ts +107 -0
  54. package/test/file/index.test.ts +104 -0
  55. package/test/number/index.test.ts +108 -0
  56. package/test/object/index.test.ts +20 -0
  57. package/test/string/index.test.ts +82 -0
  58. package/tsconfig.json +39 -0
  59. package/typedoc.json +9 -0
  60. package/types/common/index.d.ts +47 -0
  61. package/types/constant/common.constant.d.ts +12 -0
  62. package/types/date/index.d.ts +60 -0
  63. package/types/file/index.d.ts +96 -0
  64. package/types/http/http.d.ts +17 -0
  65. package/types/http/httpEnums.d.ts +53 -0
  66. package/types/index.d.ts +10 -0
  67. package/types/number/index.d.ts +62 -0
  68. package/types/object/index.d.ts +25 -0
  69. package/types/print/index.d.ts +11 -0
  70. package/types/string/index.d.ts +53 -0
  71. package/types/validate/index.d.ts +45 -0
  72. package/types/wecom/wecom.d.ts +3 -0
@@ -0,0 +1,102 @@
1
+ /**
2
+ * print处理相关
3
+ * @packageDocumentation
4
+ * @module Print
5
+ * @preferred
6
+ */
7
+
8
+ // https://juejin.im/post/5de4f87cf265da05c7722c20
9
+ /**
10
+ * 获取、设置打印样式
11
+ * @returns
12
+ */
13
+ function getStyle() {
14
+ const styleContent = `#print-container {
15
+ display: none;
16
+ }
17
+ @media print {
18
+ body > :not(.print-container) {
19
+ display: none;
20
+ }
21
+ html,
22
+ body {
23
+ display: block !important;
24
+ }
25
+ #print-container {
26
+ display: block;
27
+ }
28
+ }`;
29
+ const style = document.createElement('style');
30
+ style.innerHTML = styleContent;
31
+ return style;
32
+ }
33
+
34
+ /**
35
+ * 清空打印内容
36
+ */
37
+ function cleanPrint() {
38
+ const div = document.getElementById('print-container');
39
+ if (div) {
40
+ document.querySelector('body')!.removeChild(div);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * 新建DOM,将需要打印的内容填充到DOM
46
+ *
47
+ * @param {*} html
48
+ * @returns
49
+ */
50
+ function getContainer(html) {
51
+ cleanPrint();
52
+ const container = document.createElement('div');
53
+ container.setAttribute('id', 'print-container');
54
+ container.innerHTML = html;
55
+ return container;
56
+ }
57
+
58
+ /**
59
+ * 图片完全加载后再调用打印方法
60
+ * @param {*} dom
61
+ * @returns
62
+ */
63
+ function getLoadPromise(dom) {
64
+ let imgs = dom.querySelectorAll('img');
65
+ imgs = [].slice.call(imgs);
66
+
67
+ if (imgs.length === 0) {
68
+ return Promise.resolve();
69
+ }
70
+
71
+ let finishedCount = 0;
72
+ return new Promise((resolve) => {
73
+ function check() {
74
+ finishedCount += 1;
75
+ if (finishedCount === imgs.length) {
76
+ resolve();
77
+ }
78
+ }
79
+ imgs.forEach((img) => {
80
+ img.addEventListener('load', check);
81
+ img.addEventListener('error', check);
82
+ });
83
+ });
84
+ }
85
+
86
+ /**
87
+ * 打印指定 HTML
88
+ * @param {*} html
89
+ */
90
+ export default function printHtml(html) {
91
+ const style = getStyle();
92
+ const container = getContainer(html);
93
+
94
+ document.body.appendChild(style);
95
+ document.body.appendChild(container);
96
+
97
+ getLoadPromise(container).then(() => {
98
+ window.print();
99
+ document.body.removeChild(style);
100
+ document.body.removeChild(container);
101
+ });
102
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * string处理相关
3
+ * @packageDocumentation
4
+ * @module String
5
+ * @preferred
6
+ */
7
+ import { isEmpty } from '../common';
8
+
9
+ /**
10
+ * 空值处理
11
+ */
12
+ export function formatEmptyValue(value: any, defaultValue = '-') {
13
+ if (isEmpty(value)) {
14
+ return defaultValue;
15
+ }
16
+ return value;
17
+ }
18
+
19
+ /**
20
+ * 格式化手机
21
+ #### 使用说明
22
+ * ```
23
+ * formatPhone('18211572781') => '182 1157 2781'
24
+ * formatPhone('18211572781', '-') => '182-1157-2781'
25
+ * ```
26
+ * @param {string | number} phone 手机号
27
+ * @param {string} [separator=' '] 连接符
28
+ */
29
+ export function formatPhone(phone: string | number, separator = ' ', defaultValue = '-') {
30
+ if (isEmpty(phone)) {
31
+ return defaultValue;
32
+ }
33
+ if (phone.toString().length !== 11) {
34
+ return phone;
35
+ }
36
+ const val = phone.toString().replace(/[^\d]/g, '');
37
+ const arr = val.split('');
38
+ let str = '';
39
+ arr.forEach((v, index) => {
40
+ if (index === 3 || index === 7) {
41
+ str += separator;
42
+ }
43
+ str += v;
44
+ });
45
+ return str;
46
+ }
47
+
48
+ /**
49
+ * 隐藏手机号
50
+ #### 使用说明
51
+ * ```
52
+ * formatPhoneHide('18211572781') => '182****2781'
53
+ * formatPhoneHide('', '-') => '-'
54
+ * ```
55
+ * @param {string | number} phone 手机号
56
+ * @param {string} defaultValue 没有值放回的默认值
57
+ */
58
+ export function formatPhoneHide(phone: string | number, defaultValue = '') {
59
+ if (isEmpty(phone)) {
60
+ return defaultValue;
61
+ }
62
+ if (phone.toString().length !== 11) {
63
+ return phone;
64
+ }
65
+ const phoneNumber = phone.toString();
66
+ return `${phoneNumber.substr(0, 3)}****${phoneNumber.substr(7, 11)}`;
67
+ }
68
+
69
+ /**
70
+ * 格式化银行卡 (4位一空格)
71
+ #### 使用说明
72
+ * ```
73
+ * formatBank('6282356862823568123') => '6282 3568 6282 3568 123'
74
+ * formatBank('', '-') => '-'
75
+ * ```
76
+ * @param {strubg | number} val 银行卡号
77
+ * @param {string} defaultValue 没有值放回的默认值
78
+ */
79
+ export function formatBank(val: string | number, defaultValue = '') {
80
+ if (isEmpty(val)) {
81
+ return defaultValue;
82
+ }
83
+ return val.toString().replace(/\s/g, '').replace(/(.{4})/g, '$1 ');
84
+ }
85
+
86
+ /**
87
+ * 生成26个字母列表
88
+ #### 使用说明
89
+ * ```
90
+ * generateEnglishLetters() => ['A', ..., 'Z'];
91
+ * ```
92
+ */
93
+ export function generateEnglishLetters() {
94
+ const englishLettersList: string[] = [];
95
+ for (let i = 65; i < 91; i += 1) {
96
+ englishLettersList.push(String.fromCharCode(i));
97
+ }
98
+ return englishLettersList;
99
+ }
100
+
101
+ /**
102
+ * 驼峰式命名转短横线命名
103
+ *
104
+ * @export
105
+ * @param {*} [string='']
106
+ * @returns
107
+ */
108
+ export function humpTurnDashed(string = '') {
109
+ const rE = /\B([A-Z])/g;
110
+ return string.replace(rE, '-$1').toLowerCase();
111
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * validate处理相关
3
+ * @packageDocumentation
4
+ * @module Validate
5
+ * @preferred
6
+ */
7
+
8
+ /** 是否是外部网址
9
+ * @param {string} path
10
+ * @returns {Boolean}
11
+ */
12
+ export function isExternal(path: string) {
13
+ return /^(https?:|mailto:|tel:)/.test(path);
14
+ }
15
+
16
+ /**
17
+ * 去掉前后空格
18
+ * @param {*} str
19
+ */
20
+ export function trimVal(str: string) {
21
+ if (!str) {
22
+ return str;
23
+ }
24
+
25
+ const reg = /^\s+|\s+$/g;
26
+ return str.replace(reg, '');
27
+ }
28
+
29
+ /**
30
+ * 校验 手机
31
+ * @param {*} val
32
+ */
33
+ export function isMobile(val: string | number) {
34
+ const reg = /^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
35
+ return reg.test(val.toString());
36
+ }
37
+
38
+ /**
39
+ * 校验 手机
40
+ * 规则: 以1为开头,总共11位数
41
+ * @export
42
+ * @param {*} val
43
+ * @returns
44
+ */
45
+ export function isMobileSimple(val: string | number) {
46
+ const reg = /^1/;
47
+ val = val.toString();
48
+ return reg.test(val) && val.length === 11;
49
+ }
50
+
51
+ /**
52
+ * 校验 固定电话
53
+ * 规则: 必须带区号
54
+ * @param {*} val
55
+ */
56
+ export function isTelephone(val: string | number) {
57
+ const reg = /^0[1-9][0-9]{1,2}-[2-8][0-9]{6,7}$/;
58
+ return reg.test(String(val));
59
+ }
60
+
61
+ /**
62
+ * 校验 邮箱
63
+ * @param {*} val
64
+ */
65
+ export function isEmail(val: string) {
66
+ /* eslint-disable no-useless-escape */
67
+ const reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
68
+ return reg.test(val);
69
+ }
70
+
71
+ /**
72
+ * 校验 QQ
73
+ * @param {*} val
74
+ */
75
+ export function isQQ(val: string | number) {
76
+ const reg = /^[1-9][0-9]{4,9}$/gim;
77
+ return reg.test(String(val));
78
+ }
@@ -0,0 +1,75 @@
1
+ // import { getJsapiTicket } from '@/pages/HomePage/api/api'
2
+
3
+ // import SHA1 from 'crypto-js/sha1'
4
+ const getTicket = () => {
5
+ return localStorage.getItem('jsapiTicket');
6
+ };
7
+ const setTicket = (jsapiTicket) => {
8
+ return localStorage.setItem('jsapiTicket', jsapiTicket);
9
+ };
10
+ export function initWWConfig(getJsapiTicket) {
11
+ // let CORPID = 'wwc09929466f15ef2f' // 测试环境
12
+ // const origin = location.origin
13
+ // const hostObj = {
14
+ // 'https://h5-wecom.evergrandeservice.com'() { // prod
15
+ // CORPID = 'wwd7e01a093dca513e'
16
+ // },
17
+ // 'https://uat3-h5-wecom.evergrandeservice.com'() { // prod
18
+ // CORPID = 'wwd7e01a093dca513e'
19
+ // AGENTID = '1000077'
20
+ // },
21
+ // 'https://uat3-h5-wecom.hengdayun.com'() { // prod
22
+ // CORPID = 'wwd7e01a093dca513e'
23
+ // AGENTID = '1000077'
24
+ // }
25
+ // }
26
+ // if (hostObj[origin]) {
27
+ // hostObj[origin]()
28
+ // }
29
+ const CORPID = localStorage.getItem('appId');
30
+ const ticket = getTicket();
31
+ if (ticket) {
32
+ registerWW(CORPID, ticket);
33
+ } else {
34
+ handleGetJsapiTicket(getJsapiTicket).then((res: string) => {
35
+ registerWW(CORPID, res);
36
+ });
37
+ }
38
+ // handleGetJsapiTicket().then(res=>{
39
+ // registerWW(CORPID,res)
40
+ // })
41
+ }
42
+
43
+ export function handleGetJsapiTicket(getJsapiTicket) {
44
+ return new Promise((resolve, reject) => {
45
+ const appType = localStorage.getItem('appType');
46
+ getJsapiTicket({ appType: appType, jsapiTicketType: 1, forceRefresh: false }).then((res: any) => {
47
+ res = res.data;
48
+ const jsapiTicket = res.data;
49
+ setTicket(jsapiTicket);
50
+ resolve(jsapiTicket);
51
+ }).catch((err) => {
52
+ reject(err);
53
+ });
54
+ });
55
+ }
56
+
57
+ export function registerWW(corpid, jsapiTicket, firstTime = true) {
58
+ if (ww) {
59
+ ww.register({
60
+ corpId: corpid, // 必填,当前用户企业所属企业ID
61
+ // corpId: 'wxf8b68862a99c5a9c', // 必填,当前用户企业所属企业ID
62
+ jsApiList: ['checkJsApi', 'onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseImage', 'getLocalImgData', 'scanQRCode', 'shareAppMessage'], // 必填,需要使用的JSAPI列表
63
+ onConfigFail: function(err) {
64
+ if (firstTime) {
65
+ handleGetJsapiTicket(getJsapiTicket).then((res: string) => {
66
+ registerWW(corpid, res, false)
67
+ });
68
+ }
69
+ },
70
+ getConfigSignature() {
71
+ return ww.getSignature(jsapiTicket);
72
+ }, // 必填,根据url生成企业签名的回调函数
73
+ });
74
+ }
75
+ }
@@ -0,0 +1,19 @@
1
+ import { isEmpty } from '../../src';
2
+
3
+ describe('isEmpty', () => {
4
+ test(`isEmpty('') 返回 true`, () => {
5
+ expect(isEmpty('')).toBe(true);
6
+ })
7
+
8
+ test(`isEmpty(null) 返回 true`, () => {
9
+ expect(isEmpty(null)).toBe(true);
10
+ })
11
+
12
+ test(`isEmpty(undefined) 返回 true`, () => {
13
+ expect(isEmpty(undefined)).toBe(true);
14
+ })
15
+
16
+ test(`isEmpty(12323) 返回 false`, () => {
17
+ expect(isEmpty(12323)).toBe(false);
18
+ })
19
+ });
@@ -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
+ });