sculp-js 1.13.3-beta.0 → 1.13.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 (119) hide show
  1. package/dist/cjs/_virtual/_commonjsHelpers.js +3 -3
  2. package/dist/cjs/array.js +36 -46
  3. package/dist/cjs/async.js +41 -43
  4. package/dist/cjs/base64.js +77 -66
  5. package/dist/cjs/clipboard.js +54 -53
  6. package/dist/cjs/cloneDeep.js +90 -91
  7. package/dist/cjs/cookie.js +27 -30
  8. package/dist/cjs/date.js +101 -98
  9. package/dist/cjs/dom.js +114 -121
  10. package/dist/cjs/download.js +77 -78
  11. package/dist/cjs/easing.js +29 -32
  12. package/dist/cjs/file.js +147 -152
  13. package/dist/cjs/func.js +64 -78
  14. package/dist/cjs/index.js +1 -3
  15. package/dist/cjs/math.js +32 -38
  16. package/dist/cjs/node_modules/bezier-easing/src/index.js +35 -23
  17. package/dist/cjs/number.js +58 -61
  18. package/dist/cjs/object.js +112 -126
  19. package/dist/cjs/path.js +39 -43
  20. package/dist/cjs/qs.js +34 -41
  21. package/dist/cjs/random.js +31 -34
  22. package/dist/cjs/string.js +62 -63
  23. package/dist/cjs/tooltip.js +66 -69
  24. package/dist/cjs/tree.js +265 -263
  25. package/dist/cjs/type.js +42 -46
  26. package/dist/cjs/unique.js +39 -42
  27. package/dist/cjs/url.js +47 -48
  28. package/dist/cjs/validator.js +38 -38
  29. package/dist/cjs/variable.js +27 -21
  30. package/dist/cjs/watermark.js +78 -67
  31. package/dist/esm/array.js +36 -46
  32. package/dist/esm/async.js +41 -43
  33. package/dist/esm/base64.js +77 -66
  34. package/dist/esm/clipboard.js +55 -54
  35. package/dist/esm/cloneDeep.js +90 -91
  36. package/dist/esm/cookie.js +27 -30
  37. package/dist/esm/date.js +101 -98
  38. package/dist/esm/dom.js +114 -121
  39. package/dist/esm/download.js +78 -79
  40. package/dist/esm/easing.js +29 -32
  41. package/dist/esm/file.js +147 -152
  42. package/dist/esm/func.js +64 -78
  43. package/dist/esm/index.js +96 -8
  44. package/dist/esm/math.js +32 -38
  45. package/dist/esm/number.js +58 -61
  46. package/dist/esm/object.js +124 -127
  47. package/dist/esm/path.js +39 -43
  48. package/dist/esm/qs.js +34 -41
  49. package/dist/esm/random.js +31 -34
  50. package/dist/esm/string.js +74 -64
  51. package/dist/esm/tooltip.js +66 -69
  52. package/dist/esm/tree.js +265 -263
  53. package/dist/esm/type.js +68 -47
  54. package/dist/esm/unique.js +39 -42
  55. package/dist/esm/url.js +47 -48
  56. package/dist/esm/validator.js +56 -39
  57. package/dist/esm/variable.js +27 -21
  58. package/dist/esm/watermark.js +79 -68
  59. package/dist/types/array.d.ts +0 -1
  60. package/dist/types/async.d.ts +0 -1
  61. package/dist/types/base64.d.ts +0 -1
  62. package/dist/types/clipboard.d.ts +0 -1
  63. package/dist/types/cloneDeep.d.ts +0 -1
  64. package/dist/types/cookie.d.ts +0 -1
  65. package/dist/types/date.d.ts +0 -1
  66. package/dist/types/dom.d.ts +0 -1
  67. package/dist/types/download.d.ts +0 -1
  68. package/dist/types/easing.d.ts +0 -1
  69. package/dist/types/file.d.ts +0 -1
  70. package/dist/types/func.d.ts +0 -1
  71. package/dist/types/index.d.ts +0 -1
  72. package/dist/types/math.d.ts +0 -1
  73. package/dist/types/number.d.ts +0 -1
  74. package/dist/types/object.d.ts +0 -1
  75. package/dist/types/path.d.ts +0 -1
  76. package/dist/types/qs.d.ts +0 -1
  77. package/dist/types/random.d.ts +0 -1
  78. package/dist/types/string.d.ts +0 -1
  79. package/dist/types/tooltip.d.ts +0 -1
  80. package/dist/types/tree.d.ts +2 -2
  81. package/dist/types/type.d.ts +0 -1
  82. package/dist/types/unique.d.ts +0 -1
  83. package/dist/types/url.d.ts +0 -1
  84. package/dist/types/validator.d.ts +0 -1
  85. package/dist/types/variable.d.ts +0 -1
  86. package/dist/types/watermark.d.ts +0 -1
  87. package/dist/umd/index.min.js +2 -2
  88. package/package.json +12 -6
  89. package/dist/types/array.d.ts.map +0 -1
  90. package/dist/types/async.d.ts.map +0 -1
  91. package/dist/types/base64.d.ts.map +0 -1
  92. package/dist/types/clipboard.d.ts.map +0 -1
  93. package/dist/types/cloneDeep.d.ts.map +0 -1
  94. package/dist/types/cookie.d.ts.map +0 -1
  95. package/dist/types/core-index.d.ts +0 -17
  96. package/dist/types/core-index.d.ts.map +0 -1
  97. package/dist/types/date.d.ts.map +0 -1
  98. package/dist/types/dom.d.ts.map +0 -1
  99. package/dist/types/download.d.ts.map +0 -1
  100. package/dist/types/easing.d.ts.map +0 -1
  101. package/dist/types/file.d.ts.map +0 -1
  102. package/dist/types/func.d.ts.map +0 -1
  103. package/dist/types/index.d.ts.map +0 -1
  104. package/dist/types/index.js +0 -3278
  105. package/dist/types/math.d.ts.map +0 -1
  106. package/dist/types/number.d.ts.map +0 -1
  107. package/dist/types/object.d.ts.map +0 -1
  108. package/dist/types/path.d.ts.map +0 -1
  109. package/dist/types/qs.d.ts.map +0 -1
  110. package/dist/types/random.d.ts.map +0 -1
  111. package/dist/types/string.d.ts.map +0 -1
  112. package/dist/types/tooltip.d.ts.map +0 -1
  113. package/dist/types/tree.d.ts.map +0 -1
  114. package/dist/types/type.d.ts.map +0 -1
  115. package/dist/types/unique.d.ts.map +0 -1
  116. package/dist/types/url.d.ts.map +0 -1
  117. package/dist/types/validator.d.ts.map +0 -1
  118. package/dist/types/variable.d.ts.map +0 -1
  119. package/dist/types/watermark.d.ts.map +0 -1
package/dist/esm/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.13.3-beta.0
2
+ * sculp-js v1.13.6
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -13,21 +13,20 @@
13
13
  * @returns {DebounceFunc<F>}
14
14
  */
15
15
  const debounce = (func, wait) => {
16
- let timeout;
17
- let canceled = false;
18
- const f = function (...args) {
19
- if (canceled)
20
- return;
21
- clearTimeout(timeout);
22
- timeout = setTimeout(() => {
23
- func.call(this, ...args);
24
- }, wait);
25
- };
26
- f.cancel = () => {
27
- clearTimeout(timeout);
28
- canceled = true;
29
- };
30
- return f;
16
+ let timeout;
17
+ let canceled = false;
18
+ const f = function (...args) {
19
+ if (canceled) return;
20
+ clearTimeout(timeout);
21
+ timeout = setTimeout(() => {
22
+ func.call(this, ...args);
23
+ }, wait);
24
+ };
25
+ f.cancel = () => {
26
+ clearTimeout(timeout);
27
+ canceled = true;
28
+ };
29
+ return f;
31
30
  };
32
31
  /**
33
32
  * 节流函数
@@ -39,57 +38,53 @@ const debounce = (func, wait) => {
39
38
  * @returns {ThrottleFunc<F>}
40
39
  */
41
40
  const throttle = (func, wait, immediate) => {
42
- let timeout;
43
- let canceled = false;
44
- let lastCalledTime = 0;
45
- const f = function (...args) {
46
- if (canceled)
47
- return;
48
- const now = Date.now();
49
- const call = () => {
50
- lastCalledTime = now;
51
- func.call(this, ...args);
52
- };
53
- // 第一次执行
54
- if (lastCalledTime === 0) {
55
- if (immediate) {
56
- return call();
57
- }
58
- else {
59
- lastCalledTime = now;
60
- return;
61
- }
62
- }
63
- const remain = lastCalledTime + wait - now;
64
- if (remain > 0) {
65
- clearTimeout(timeout);
66
- timeout = setTimeout(() => call(), wait);
67
- }
68
- else {
69
- call();
70
- }
41
+ let timeout;
42
+ let canceled = false;
43
+ let lastCalledTime = 0;
44
+ const f = function (...args) {
45
+ if (canceled) return;
46
+ const now = Date.now();
47
+ const call = () => {
48
+ lastCalledTime = now;
49
+ func.call(this, ...args);
71
50
  };
72
- f.cancel = () => {
73
- clearTimeout(timeout);
74
- canceled = true;
75
- };
76
- return f;
51
+ // 第一次执行
52
+ if (lastCalledTime === 0) {
53
+ if (immediate) {
54
+ return call();
55
+ } else {
56
+ lastCalledTime = now;
57
+ return;
58
+ }
59
+ }
60
+ const remain = lastCalledTime + wait - now;
61
+ if (remain > 0) {
62
+ clearTimeout(timeout);
63
+ timeout = setTimeout(() => call(), wait);
64
+ } else {
65
+ call();
66
+ }
67
+ };
68
+ f.cancel = () => {
69
+ clearTimeout(timeout);
70
+ canceled = true;
71
+ };
72
+ return f;
77
73
  };
78
74
  /**
79
75
  * 单次函数
80
76
  * @param {AnyFunc} func
81
77
  * @returns {AnyFunc}
82
78
  */
83
- const once = (func) => {
84
- let called = false;
85
- let result;
86
- return function (...args) {
87
- if (called)
88
- return result;
89
- called = true;
90
- result = func.call(this, ...args);
91
- return result;
92
- };
79
+ const once = func => {
80
+ let called = false;
81
+ let result;
82
+ return function (...args) {
83
+ if (called) return result;
84
+ called = true;
85
+ result = func.call(this, ...args);
86
+ return result;
87
+ };
93
88
  };
94
89
  /**
95
90
  * 设置全局变量
@@ -97,16 +92,11 @@ const once = (func) => {
97
92
  * @param val
98
93
  */
99
94
  function setGlobal(key, val) {
100
- if (typeof globalThis !== 'undefined')
101
- globalThis[key] = val;
102
- else if (typeof window !== 'undefined')
103
- window[key] = val;
104
- else if (typeof global !== 'undefined')
105
- global[key] = val;
106
- else if (typeof self !== 'undefined')
107
- self[key] = val;
108
- else
109
- throw new SyntaxError('当前环境下无法设置全局属性');
95
+ if (typeof globalThis !== 'undefined') globalThis[key] = val;
96
+ else if (typeof window !== 'undefined') window[key] = val;
97
+ else if (typeof global !== 'undefined') global[key] = val;
98
+ else if (typeof self !== 'undefined') self[key] = val;
99
+ else throw new SyntaxError('当前环境下无法设置全局属性');
110
100
  }
111
101
  /**
112
102
  * 获取全局变量
@@ -114,14 +104,10 @@ function setGlobal(key, val) {
114
104
  * @param val
115
105
  */
116
106
  function getGlobal(key) {
117
- if (typeof globalThis !== 'undefined')
118
- return globalThis[key];
119
- else if (typeof window !== 'undefined')
120
- return window[key];
121
- else if (typeof global !== 'undefined')
122
- return global[key];
123
- else if (typeof self !== 'undefined')
124
- return self[key];
107
+ if (typeof globalThis !== 'undefined') return globalThis[key];
108
+ else if (typeof window !== 'undefined') return window[key];
109
+ else if (typeof global !== 'undefined') return global[key];
110
+ else if (typeof self !== 'undefined') return self[key];
125
111
  }
126
112
 
127
113
  export { debounce, getGlobal, once, setGlobal, throttle };
package/dist/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.13.3-beta.0
2
+ * sculp-js v1.13.6
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,27 +7,115 @@
7
7
  export { arrayEach, arrayEachAsync, arrayInsertBefore, arrayRemove } from './array.js';
8
8
  export { copyText, fallbackCopyText } from './clipboard.js';
9
9
  export { cookieDel, cookieGet, cookieSet } from './cookie.js';
10
- export { calculateDate, calculateDateTime, dateParse, dateToEnd, dateToStart, formatDate, isValidDate } from './date.js';
11
- export { addClass, getComputedCssVal, getStrWidthPx, getStyle, hasClass, removeClass, select, setStyle, smoothScroll } from './dom.js';
10
+ export {
11
+ calculateDate,
12
+ calculateDateTime,
13
+ dateParse,
14
+ dateToEnd,
15
+ dateToStart,
16
+ formatDate,
17
+ isValidDate
18
+ } from './date.js';
19
+ export {
20
+ addClass,
21
+ getComputedCssVal,
22
+ getStrWidthPx,
23
+ getStyle,
24
+ hasClass,
25
+ removeClass,
26
+ select,
27
+ setStyle,
28
+ smoothScroll
29
+ } from './dom.js';
12
30
  export { crossOriginDownload, downloadBlob, downloadData, downloadHref, downloadURL } from './download.js';
13
- export { isPlainObject, objectAssign, objectEach, objectEachAsync, objectFill, objectGet, objectMap, objectAssign as objectMerge, objectOmit, objectPick } from './object.js';
31
+ export {
32
+ isPlainObject,
33
+ objectAssign,
34
+ objectEach,
35
+ objectEachAsync,
36
+ objectFill,
37
+ objectGet,
38
+ objectMap,
39
+ objectAssign as objectMerge,
40
+ objectOmit,
41
+ objectPick
42
+ } from './object.js';
14
43
  export { pathJoin, pathNormalize } from './path.js';
15
44
  export { qsParse, qsStringify } from './qs.js';
16
- export { STRING_ARABIC_NUMERALS, STRING_LOWERCASE_ALPHA, STRING_UPPERCASE_ALPHA, parseQueryParams, stringAssign, stringCamelCase, stringEscapeHtml, stringFill, stringFormat, stringKebabCase } from './string.js';
17
- export { arrayLike, isArray, isBigInt, isBoolean, isDate, isEmpty, isError, isFunction, isJsonString, isNaN, isNodeList, isNull, isNullOrUnDef, isNullOrUnDef as isNullish, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isUndefined, objectHas, typeIs } from './type.js';
45
+ export {
46
+ STRING_ARABIC_NUMERALS,
47
+ STRING_LOWERCASE_ALPHA,
48
+ STRING_UPPERCASE_ALPHA,
49
+ parseQueryParams,
50
+ stringAssign,
51
+ stringCamelCase,
52
+ stringEscapeHtml,
53
+ stringFill,
54
+ stringFormat,
55
+ stringKebabCase
56
+ } from './string.js';
57
+ export {
58
+ arrayLike,
59
+ isArray,
60
+ isBigInt,
61
+ isBoolean,
62
+ isDate,
63
+ isEmpty,
64
+ isError,
65
+ isFunction,
66
+ isJsonString,
67
+ isNaN,
68
+ isNodeList,
69
+ isNull,
70
+ isNullOrUnDef,
71
+ isNullOrUnDef as isNullish,
72
+ isNumber,
73
+ isObject,
74
+ isPrimitive,
75
+ isRegExp,
76
+ isString,
77
+ isSymbol,
78
+ isUndefined,
79
+ objectHas,
80
+ typeIs
81
+ } from './type.js';
18
82
  export { urlDelParams, urlParse, urlSetParams, urlStringify } from './url.js';
19
83
  export { asyncMap, safeAwait, wait } from './async.js';
20
84
  export { chooseLocalFile, compressImg, supportCanvas } from './file.js';
21
85
  export { genCanvasWM } from './watermark.js';
22
86
  export { debounce, getGlobal, once, setGlobal, throttle } from './func.js';
23
87
  export { STRING_POOL, randomNumber, randomString, randomUuid } from './random.js';
24
- export { HEX_POOL, formatNumber as formatMoney, formatNumber, humanFileSize, numberAbbr, numberToHex } from './number.js';
88
+ export {
89
+ HEX_POOL,
90
+ formatNumber as formatMoney,
91
+ formatNumber,
92
+ humanFileSize,
93
+ numberAbbr,
94
+ numberToHex
95
+ } from './number.js';
25
96
  export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString } from './unique.js';
26
97
  export { tooltipEvent } from './tooltip.js';
27
98
  export { flatTree, forEachDeep, formatTree, fuzzySearchTree, mapDeep, searchTreeById } from './tree.js';
28
99
  export { add, divide, multiply, strip, subtract } from './math.js';
29
100
  export { b64decode, b64encode, weAtob, weBtoa } from './base64.js';
30
- export { EMAIL_REGEX, HTTP_URL_REGEX, IPV4_REGEX, IPV6_REGEX, PHONE_REGEX, URL_REGEX, isDigit, isEmail, isFloat, isIdNo, isInteger, isIpV4, isIpV6, isNumerical, isPhone, isUrl } from './validator.js';
101
+ export {
102
+ EMAIL_REGEX,
103
+ HTTP_URL_REGEX,
104
+ IPV4_REGEX,
105
+ IPV6_REGEX,
106
+ PHONE_REGEX,
107
+ URL_REGEX,
108
+ isDigit,
109
+ isEmail,
110
+ isFloat,
111
+ isIdNo,
112
+ isInteger,
113
+ isIpV4,
114
+ isIpV6,
115
+ isNumerical,
116
+ isPhone,
117
+ isUrl
118
+ } from './validator.js';
31
119
  export { escapeRegExp, executeInScope, parseVarFromString, replaceVarFromString, uniqueSymbol } from './variable.js';
32
120
  export { cloneDeep } from './cloneDeep.js';
33
121
  export { easingFunctional, easingStringify, getEasing, setEasing } from './easing.js';
package/dist/esm/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.13.3-beta.0
2
+ * sculp-js v1.13.6
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -10,14 +10,12 @@
10
10
  * @param arg2 数值2
11
11
  */
12
12
  const multiply = (arg1, arg2) => {
13
- let m = 0;
14
- const s1 = arg1.toString();
15
- const s2 = arg2.toString();
16
- if (s1.split('.')[1] !== undefined)
17
- m += s1.split('.')[1].length;
18
- if (s2.split('.')[1] !== undefined)
19
- m += s2.split('.')[1].length;
20
- return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
13
+ let m = 0;
14
+ const s1 = arg1.toString();
15
+ const s2 = arg2.toString();
16
+ if (s1.split('.')[1] !== undefined) m += s1.split('.')[1].length;
17
+ if (s2.split('.')[1] !== undefined) m += s2.split('.')[1].length;
18
+ return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
21
19
  };
22
20
  /**
23
21
  * 数值安全加法
@@ -25,23 +23,21 @@ const multiply = (arg1, arg2) => {
25
23
  * @param arg2 数值2
26
24
  */
27
25
  const add = (arg1, arg2) => {
28
- let r1 = 0;
29
- let r2 = 0;
30
- let m = 0;
31
- try {
32
- r1 = arg1.toString().split('.')[1].length;
33
- }
34
- catch (e) {
35
- r1 = 0;
36
- }
37
- try {
38
- r2 = arg2.toString().split('.')[1].length;
39
- }
40
- catch (e) {
41
- r2 = 0;
42
- }
43
- m = 10 ** Math.max(r1, r2);
44
- return (multiply(arg1, m) + multiply(arg2, m)) / m;
26
+ let r1 = 0;
27
+ let r2 = 0;
28
+ let m = 0;
29
+ try {
30
+ r1 = arg1.toString().split('.')[1].length;
31
+ } catch (e) {
32
+ r1 = 0;
33
+ }
34
+ try {
35
+ r2 = arg2.toString().split('.')[1].length;
36
+ } catch (e) {
37
+ r2 = 0;
38
+ }
39
+ m = 10 ** Math.max(r1, r2);
40
+ return (multiply(arg1, m) + multiply(arg2, m)) / m;
45
41
  };
46
42
  /**
47
43
  * 数值安全减法
@@ -55,17 +51,15 @@ const subtract = (arg1, arg2) => add(arg1, -arg2);
55
51
  * @param arg2 数值2
56
52
  */
57
53
  const divide = (arg1, arg2) => {
58
- let t1 = 0;
59
- let t2 = 0;
60
- let r1 = 0;
61
- let r2 = 0;
62
- if (arg1.toString().split('.')[1] !== undefined)
63
- t1 = arg1.toString().split('.')[1].length;
64
- if (arg2.toString().split('.')[1] !== undefined)
65
- t2 = arg2.toString().split('.')[1].length;
66
- r1 = Number(arg1.toString().replace('.', ''));
67
- r2 = Number(arg2.toString().replace('.', ''));
68
- return (r1 / r2) * Math.pow(10, t2 - t1);
54
+ let t1 = 0;
55
+ let t2 = 0;
56
+ let r1 = 0;
57
+ let r2 = 0;
58
+ if (arg1.toString().split('.')[1] !== undefined) t1 = arg1.toString().split('.')[1].length;
59
+ if (arg2.toString().split('.')[1] !== undefined) t2 = arg2.toString().split('.')[1].length;
60
+ r1 = Number(arg1.toString().replace('.', ''));
61
+ r2 = Number(arg2.toString().replace('.', ''));
62
+ return (r1 / r2) * Math.pow(10, t2 - t1);
69
63
  };
70
64
  /**
71
65
  * Correct the given number to specifying significant digits.
@@ -76,7 +70,7 @@ const divide = (arg1, arg2) => {
76
70
  * @example strip(0.09999999999999998) === 0.1 // true
77
71
  */
78
72
  function strip(num, precision = 15) {
79
- return +parseFloat(Number(num).toPrecision(precision));
73
+ return +parseFloat(Number(num).toPrecision(precision));
80
74
  }
81
75
 
82
76
  export { add, divide, multiply, strip, subtract };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.13.3-beta.0
2
+ * sculp-js v1.13.6
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -11,7 +11,7 @@ import { isNullOrUnDef, isNumber } from './type.js';
11
11
  const HEX_POOL = `${STRING_ARABIC_NUMERALS}${STRING_UPPERCASE_ALPHA}${STRING_LOWERCASE_ALPHA}`;
12
12
  const supportBigInt = typeof BigInt !== 'undefined';
13
13
  const jsbi = () => getGlobal('JSBI');
14
- const toBigInt = (n) => (supportBigInt ? BigInt(n) : jsbi().BigInt(n));
14
+ const toBigInt = n => (supportBigInt ? BigInt(n) : jsbi().BigInt(n));
15
15
  const divide = (x, y) => (supportBigInt ? x / y : jsbi().divide(x, y));
16
16
  const remainder = (x, y) => (supportBigInt ? x % y : jsbi().remainder(x, y));
17
17
  /**
@@ -21,27 +21,26 @@ const remainder = (x, y) => (supportBigInt ? x % y : jsbi().remainder(x, y));
21
21
  * @returns {string}
22
22
  */
23
23
  function numberToHex(decimal, hexPool = HEX_POOL) {
24
- if (hexPool.length < 2)
25
- throw new Error('进制池长度不能少于 2');
26
- if (!supportBigInt) {
27
- throw new Error('需要安装 jsbi 模块并将 JSBI 设置为全局变量:\nimport JSBI from "jsbi"; window.JSBI = JSBI;');
24
+ if (hexPool.length < 2) throw new Error('进制池长度不能少于 2');
25
+ if (!supportBigInt) {
26
+ throw new Error('需要安装 jsbi 模块并将 JSBI 设置为全局变量:\nimport JSBI from "jsbi"; window.JSBI = JSBI;');
27
+ }
28
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
29
+ let bigInt = toBigInt(decimal);
30
+ const ret = [];
31
+ const { length } = hexPool;
32
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
33
+ const bigLength = toBigInt(length);
34
+ const execute = () => {
35
+ const y = Number(remainder(bigInt, bigLength));
36
+ bigInt = divide(bigInt, bigLength);
37
+ ret.unshift(hexPool[y]);
38
+ if (bigInt > 0) {
39
+ execute();
28
40
  }
29
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
30
- let bigInt = toBigInt(decimal);
31
- const ret = [];
32
- const { length } = hexPool;
33
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
34
- const bigLength = toBigInt(length);
35
- const execute = () => {
36
- const y = Number(remainder(bigInt, bigLength));
37
- bigInt = divide(bigInt, bigLength);
38
- ret.unshift(hexPool[y]);
39
- if (bigInt > 0) {
40
- execute();
41
- }
42
- };
43
- execute();
44
- return ret.join('');
41
+ };
42
+ execute();
43
+ return ret.join('');
45
44
  }
46
45
  /**
47
46
  * 将数字转换为携带单位的字符串
@@ -51,19 +50,18 @@ function numberToHex(decimal, hexPool = HEX_POOL) {
51
50
  * @returns {string}
52
51
  */
53
52
  const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator: ' ' }) => {
54
- const { ratio = 1000, decimals = 0, separator = ' ' } = options;
55
- const { length } = units;
56
- if (length === 0)
57
- throw new Error('At least one unit is required');
58
- let num2 = Number(num);
59
- let times = 0;
60
- while (num2 >= ratio && times < length - 1) {
61
- num2 = num2 / ratio;
62
- times++;
63
- }
64
- const value = num2.toFixed(decimals);
65
- const unit = units[times];
66
- return String(value) + separator + unit;
53
+ const { ratio = 1000, decimals = 0, separator = ' ' } = options;
54
+ const { length } = units;
55
+ if (length === 0) throw new Error('At least one unit is required');
56
+ let num2 = Number(num);
57
+ let times = 0;
58
+ while (num2 >= ratio && times < length - 1) {
59
+ num2 = num2 / ratio;
60
+ times++;
61
+ }
62
+ const value = num2.toFixed(decimals);
63
+ const unit = units[times];
64
+ return String(value) + separator + unit;
67
65
  };
68
66
  /**
69
67
  * Converting file size in bytes to human-readable string
@@ -77,23 +75,23 @@ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator:
77
75
  * @returns
78
76
  */
79
77
  function humanFileSize(num, options = { decimals: 0, si: false, separator: ' ' }) {
80
- const { decimals = 0, si = false, separator = ' ', baseUnit, maxUnit } = options;
81
- let units = si
82
- ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
83
- : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
84
- if (!isNullOrUnDef(baseUnit)) {
85
- const targetIndex = units.findIndex(el => el === baseUnit);
86
- if (targetIndex !== -1) {
87
- units = units.slice(targetIndex);
88
- }
78
+ const { decimals = 0, si = false, separator = ' ', baseUnit, maxUnit } = options;
79
+ let units = si
80
+ ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
81
+ : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
82
+ if (!isNullOrUnDef(baseUnit)) {
83
+ const targetIndex = units.findIndex(el => el === baseUnit);
84
+ if (targetIndex !== -1) {
85
+ units = units.slice(targetIndex);
89
86
  }
90
- if (!isNullOrUnDef(maxUnit)) {
91
- const targetIndex = units.findIndex(el => el === maxUnit);
92
- if (targetIndex !== -1) {
93
- units.splice(targetIndex + 1);
94
- }
87
+ }
88
+ if (!isNullOrUnDef(maxUnit)) {
89
+ const targetIndex = units.findIndex(el => el === maxUnit);
90
+ if (targetIndex !== -1) {
91
+ units.splice(targetIndex + 1);
95
92
  }
96
- return numberAbbr(num, units, { ratio: si ? 1000 : 1024, decimals, separator });
93
+ }
94
+ return numberAbbr(num, units, { ratio: si ? 1000 : 1024, decimals, separator });
97
95
  }
98
96
  /**
99
97
  * 将数字格式化成千位分隔符显示的字符串
@@ -102,17 +100,16 @@ function humanFileSize(num, options = { decimals: 0, si: false, separator: ' ' }
102
100
  * @returns {string}
103
101
  */
104
102
  function formatNumber(num, decimals) {
105
- if (isNullOrUnDef(decimals)) {
106
- return parseInt(String(num)).toLocaleString();
107
- }
108
- let prec = 0;
109
- if (!isNumber(decimals)) {
110
- throw new Error('Decimals must be a positive number not less than zero');
111
- }
112
- else if (decimals > 0) {
113
- prec = decimals;
114
- }
115
- return Number(Number(num).toFixed(prec)).toLocaleString('en-US');
103
+ if (isNullOrUnDef(decimals)) {
104
+ return parseInt(String(num)).toLocaleString();
105
+ }
106
+ let prec = 0;
107
+ if (!isNumber(decimals)) {
108
+ throw new Error('Decimals must be a positive number not less than zero');
109
+ } else if (decimals > 0) {
110
+ prec = decimals;
111
+ }
112
+ return Number(Number(num).toFixed(prec)).toLocaleString('en-US');
116
113
  }
117
114
 
118
115
  export { HEX_POOL, formatNumber as formatMoney, formatNumber, humanFileSize, numberAbbr, numberToHex };