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.
- package/dist/cjs/_virtual/_commonjsHelpers.js +3 -3
- package/dist/cjs/array.js +36 -46
- package/dist/cjs/async.js +41 -43
- package/dist/cjs/base64.js +77 -66
- package/dist/cjs/clipboard.js +54 -53
- package/dist/cjs/cloneDeep.js +90 -91
- package/dist/cjs/cookie.js +27 -30
- package/dist/cjs/date.js +101 -98
- package/dist/cjs/dom.js +114 -121
- package/dist/cjs/download.js +77 -78
- package/dist/cjs/easing.js +29 -32
- package/dist/cjs/file.js +147 -152
- package/dist/cjs/func.js +64 -78
- package/dist/cjs/index.js +1 -3
- package/dist/cjs/math.js +32 -38
- package/dist/cjs/node_modules/bezier-easing/src/index.js +35 -23
- package/dist/cjs/number.js +58 -61
- package/dist/cjs/object.js +112 -126
- package/dist/cjs/path.js +39 -43
- package/dist/cjs/qs.js +34 -41
- package/dist/cjs/random.js +31 -34
- package/dist/cjs/string.js +62 -63
- package/dist/cjs/tooltip.js +66 -69
- package/dist/cjs/tree.js +265 -263
- package/dist/cjs/type.js +42 -46
- package/dist/cjs/unique.js +39 -42
- package/dist/cjs/url.js +47 -48
- package/dist/cjs/validator.js +38 -38
- package/dist/cjs/variable.js +27 -21
- package/dist/cjs/watermark.js +78 -67
- package/dist/esm/array.js +36 -46
- package/dist/esm/async.js +41 -43
- package/dist/esm/base64.js +77 -66
- package/dist/esm/clipboard.js +55 -54
- package/dist/esm/cloneDeep.js +90 -91
- package/dist/esm/cookie.js +27 -30
- package/dist/esm/date.js +101 -98
- package/dist/esm/dom.js +114 -121
- package/dist/esm/download.js +78 -79
- package/dist/esm/easing.js +29 -32
- package/dist/esm/file.js +147 -152
- package/dist/esm/func.js +64 -78
- package/dist/esm/index.js +96 -8
- package/dist/esm/math.js +32 -38
- package/dist/esm/number.js +58 -61
- package/dist/esm/object.js +124 -127
- package/dist/esm/path.js +39 -43
- package/dist/esm/qs.js +34 -41
- package/dist/esm/random.js +31 -34
- package/dist/esm/string.js +74 -64
- package/dist/esm/tooltip.js +66 -69
- package/dist/esm/tree.js +265 -263
- package/dist/esm/type.js +68 -47
- package/dist/esm/unique.js +39 -42
- package/dist/esm/url.js +47 -48
- package/dist/esm/validator.js +56 -39
- package/dist/esm/variable.js +27 -21
- package/dist/esm/watermark.js +79 -68
- package/dist/types/array.d.ts +0 -1
- package/dist/types/async.d.ts +0 -1
- package/dist/types/base64.d.ts +0 -1
- package/dist/types/clipboard.d.ts +0 -1
- package/dist/types/cloneDeep.d.ts +0 -1
- package/dist/types/cookie.d.ts +0 -1
- package/dist/types/date.d.ts +0 -1
- package/dist/types/dom.d.ts +0 -1
- package/dist/types/download.d.ts +0 -1
- package/dist/types/easing.d.ts +0 -1
- package/dist/types/file.d.ts +0 -1
- package/dist/types/func.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/math.d.ts +0 -1
- package/dist/types/number.d.ts +0 -1
- package/dist/types/object.d.ts +0 -1
- package/dist/types/path.d.ts +0 -1
- package/dist/types/qs.d.ts +0 -1
- package/dist/types/random.d.ts +0 -1
- package/dist/types/string.d.ts +0 -1
- package/dist/types/tooltip.d.ts +0 -1
- package/dist/types/tree.d.ts +2 -2
- package/dist/types/type.d.ts +0 -1
- package/dist/types/unique.d.ts +0 -1
- package/dist/types/url.d.ts +0 -1
- package/dist/types/validator.d.ts +0 -1
- package/dist/types/variable.d.ts +0 -1
- package/dist/types/watermark.d.ts +0 -1
- package/dist/umd/index.min.js +2 -2
- package/package.json +12 -6
- package/dist/types/array.d.ts.map +0 -1
- package/dist/types/async.d.ts.map +0 -1
- package/dist/types/base64.d.ts.map +0 -1
- package/dist/types/clipboard.d.ts.map +0 -1
- package/dist/types/cloneDeep.d.ts.map +0 -1
- package/dist/types/cookie.d.ts.map +0 -1
- package/dist/types/core-index.d.ts +0 -17
- package/dist/types/core-index.d.ts.map +0 -1
- package/dist/types/date.d.ts.map +0 -1
- package/dist/types/dom.d.ts.map +0 -1
- package/dist/types/download.d.ts.map +0 -1
- package/dist/types/easing.d.ts.map +0 -1
- package/dist/types/file.d.ts.map +0 -1
- package/dist/types/func.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3278
- package/dist/types/math.d.ts.map +0 -1
- package/dist/types/number.d.ts.map +0 -1
- package/dist/types/object.d.ts.map +0 -1
- package/dist/types/path.d.ts.map +0 -1
- package/dist/types/qs.d.ts.map +0 -1
- package/dist/types/random.d.ts.map +0 -1
- package/dist/types/string.d.ts.map +0 -1
- package/dist/types/tooltip.d.ts.map +0 -1
- package/dist/types/tree.d.ts.map +0 -1
- package/dist/types/type.d.ts.map +0 -1
- package/dist/types/unique.d.ts.map +0 -1
- package/dist/types/url.d.ts.map +0 -1
- package/dist/types/validator.d.ts.map +0 -1
- package/dist/types/variable.d.ts.map +0 -1
- 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.
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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 =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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.
|
|
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 {
|
|
11
|
-
|
|
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 {
|
|
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 {
|
|
17
|
-
|
|
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 {
|
|
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 {
|
|
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.
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
73
|
+
return +parseFloat(Number(num).toPrecision(precision));
|
|
80
74
|
}
|
|
81
75
|
|
|
82
76
|
export { add, divide, multiply, strip, subtract };
|
package/dist/esm/number.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sculp-js v1.13.
|
|
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 =
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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 };
|