@base-web-kits/base-tools-ts 0.9.9 → 0.9.12
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/base-tools-ts.umd.global.js +45 -40
- package/dist/base-tools-ts.umd.global.js.map +1 -1
- package/dist/day/index.d.ts +3 -3
- package/dist/day/index.d.ts.map +1 -1
- package/dist/index.cjs +54 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +45 -40
- package/dist/index.js.map +1 -1
- package/dist/number/big.d.ts +33 -33
- package/dist/number/big.d.ts.map +1 -1
- package/dist/string/other.d.ts +13 -11
- package/dist/string/other.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/ts/day/index.ts +3 -3
- package/src/ts/number/big.ts +41 -41
- package/src/ts/string/other.ts +36 -24
package/dist/number/big.d.ts
CHANGED
|
@@ -16,27 +16,27 @@ export declare function big(x: NumLike): BigNumber;
|
|
|
16
16
|
/**
|
|
17
17
|
* 高精度加法(支持多个参数连加)。
|
|
18
18
|
* @example
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* bigPlus(0.1, 0.2); // => 0.3
|
|
20
|
+
* bigPlus('0.1', '0.2'); // => 0.3
|
|
21
|
+
* bigPlus(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
|
|
22
22
|
*/
|
|
23
|
-
export declare function
|
|
23
|
+
export declare function bigPlus(...rest: NumLike[]): number;
|
|
24
24
|
/**
|
|
25
25
|
* 高精度减法(支持多个参数连减)。
|
|
26
26
|
* @example
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
27
|
+
* bigMinus(1, 0.9); // => 0.1
|
|
28
|
+
* bigMinus('1.1', '0.2'); // => 0.9
|
|
29
|
+
* bigMinus(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
|
|
30
30
|
*/
|
|
31
|
-
export declare function
|
|
31
|
+
export declare function bigMinus(...rest: NumLike[]): number;
|
|
32
32
|
/**
|
|
33
33
|
* 高精度乘法(支持多个参数连乘)。
|
|
34
34
|
* @example
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* bigTimes(0.1, 0.2); // => 0.02
|
|
36
|
+
* bigTimes('1.5', '3'); // => 4.5
|
|
37
|
+
* bigTimes(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
|
|
38
38
|
*/
|
|
39
|
-
export declare function
|
|
39
|
+
export declare function bigTimes(...rest: NumLike[]): number;
|
|
40
40
|
/**
|
|
41
41
|
* 高精度除法(支持多个参数连除)。
|
|
42
42
|
* @example
|
|
@@ -44,7 +44,7 @@ export declare function bigMul(a: NumLike, ...rest: NumLike[]): number;
|
|
|
44
44
|
* bigDiv('10', '4'); // => 2.5
|
|
45
45
|
* bigDiv(100, 5, 2); // => 10 // 多参数连除: 100/5/2
|
|
46
46
|
*/
|
|
47
|
-
export declare function bigDiv(
|
|
47
|
+
export declare function bigDiv(...rest: NumLike[]): number;
|
|
48
48
|
/**
|
|
49
49
|
* 指数运算
|
|
50
50
|
* @param x 底数。
|
|
@@ -56,7 +56,7 @@ export declare function bigDiv(a: NumLike, ...rest: NumLike[]): number;
|
|
|
56
56
|
*/
|
|
57
57
|
export declare function bigPow(x: NumLike, y: NumLike): number;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
59
|
+
* 四舍五入到指定小数位数
|
|
60
60
|
* @param x 需要舍入的数值。
|
|
61
61
|
* @param dp 保留的小数位数,默认 `0`(取整)。
|
|
62
62
|
* @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
|
|
@@ -75,13 +75,13 @@ export declare function bigRound(x: NumLike, dp?: number, rm?: BigNumber.Roundin
|
|
|
75
75
|
* @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
|
|
76
76
|
* @returns 格式化后的字符串。
|
|
77
77
|
* @example
|
|
78
|
-
*
|
|
79
|
-
* +
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
78
|
+
* bigFixed('1'); // => '1.00'
|
|
79
|
+
* +bigFixed('1'); // => 1
|
|
80
|
+
* bigFixed(1.2345); // => '1.23'
|
|
81
|
+
* bigFixed(1.2345, 3); // => '1.235'
|
|
82
|
+
* bigFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
|
|
83
83
|
*/
|
|
84
|
-
export declare function
|
|
84
|
+
export declare function bigFixed(x: NumLike, dp?: number, rm?: BigNumber.RoundingMode): string;
|
|
85
85
|
/**
|
|
86
86
|
* 比较两个数值大小。
|
|
87
87
|
* @example
|
|
@@ -100,32 +100,32 @@ export declare function bigEqual(a: NumLike, b: NumLike): boolean;
|
|
|
100
100
|
/**
|
|
101
101
|
* 判断 a 是否大于 b。
|
|
102
102
|
* @example
|
|
103
|
-
*
|
|
104
|
-
*
|
|
103
|
+
* bigGreaterThan(2, 1); // => true
|
|
104
|
+
* bigGreaterThan(1, 2); // => false
|
|
105
105
|
*/
|
|
106
|
-
export declare function
|
|
106
|
+
export declare function bigGreaterThan(a: NumLike, b: NumLike): boolean;
|
|
107
107
|
/**
|
|
108
108
|
* 判断 a 是否大于等于 b。
|
|
109
109
|
* @example
|
|
110
|
-
*
|
|
111
|
-
*
|
|
110
|
+
* bigGreaterThanOrEqual(2, 2); // => true
|
|
111
|
+
* bigGreaterThanOrEqual(1, 2); // => false
|
|
112
112
|
*/
|
|
113
|
-
export declare function
|
|
113
|
+
export declare function bigGreaterThanOrEqualTo(a: NumLike, b: NumLike): boolean;
|
|
114
114
|
/**
|
|
115
115
|
* 判断 a 是否小于 b。
|
|
116
116
|
* @example
|
|
117
|
-
*
|
|
118
|
-
*
|
|
117
|
+
* bigLessThan(1, 2); // => true
|
|
118
|
+
* bigLessThan(2, 1); // => false
|
|
119
119
|
*/
|
|
120
|
-
export declare function
|
|
120
|
+
export declare function bigLessThan(a: NumLike, b: NumLike): boolean;
|
|
121
121
|
/**
|
|
122
122
|
* 判断 a 是否小于等于 b。
|
|
123
123
|
* @example
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
124
|
+
* bigLessThanOrEqual(2, 2); // => true
|
|
125
|
+
* bigLessThanOrEqual(1, 2); // => true
|
|
126
|
+
* bigLessThanOrEqual(2, 1); // => false
|
|
127
127
|
*/
|
|
128
|
-
export declare function
|
|
128
|
+
export declare function bigLessThanOrEqual(a: NumLike, b: NumLike): boolean;
|
|
129
129
|
/**
|
|
130
130
|
* 导出 BigNumber 类
|
|
131
131
|
* @example
|
package/dist/number/big.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"big.d.ts","sourceRoot":"","sources":["../../src/ts/number/big.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;AAElD;;;;;;;GAOG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,CAEzC;AAED;;;;;;GAMG;AACH,wBAAgB,
|
|
1
|
+
{"version":3,"file":"big.d.ts","sourceRoot":"","sources":["../../src/ts/number/big.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;AAElD;;;;;;;GAOG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,SAAS,CAEzC;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,UAIzC;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,UAI1C;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,UAI1C;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,UAIxC;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,UAE5C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,SAAI,EAAE,EAAE,GAAE,SAAS,CAAC,YAAsC,UAEhG;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,QAAQ,CACtB,CAAC,EAAE,OAAO,EACV,EAAE,SAAI,EACN,EAAE,GAAE,SAAS,CAAC,YAAsC,GACnD,MAAM,CAER;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAEpE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAExD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAEvE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAElE;AAED;;;;;;GAMG;AACH,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/string/other.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 1
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
* @returns 字符串的字节长度
|
|
2
|
+
* 获取字节长度 (支持字符串、Buffer/Uint8Array、File/Blob 等类型)
|
|
3
|
+
* - 字符串按 UTF-8 编码计算字节长度(每个字符 1-4 字节)
|
|
4
|
+
* - Buffer/Uint8Array 直接返回字节长度(每个元素 1 字节)
|
|
5
|
+
* - File/Blob 返回文件/Blob 大小(字节数)
|
|
6
|
+
* @param data 输入的数据
|
|
7
|
+
* @returns 数据的字节长度
|
|
9
8
|
* @example
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* getByteLength('abc') // 3
|
|
10
|
+
* getByteLength('中文') // 6
|
|
11
|
+
* getByteLength('😊') // 4
|
|
12
|
+
* getByteLength(new Uint8Array([0x41, 0x42, 0x43])) // 3
|
|
13
|
+
* getByteLength(new File(['abc'], 'test.txt')) // 3
|
|
14
|
+
* getByteLength(new Blob(['中文'], { type: 'text/plain' })) // 6
|
|
13
15
|
*/
|
|
14
|
-
export declare function
|
|
16
|
+
export declare function getByteLength(data: string | ArrayBuffer | ArrayBufferView | File | Blob): number;
|
|
15
17
|
//# sourceMappingURL=other.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"other.d.ts","sourceRoot":"","sources":["../../src/ts/string/other.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"other.d.ts","sourceRoot":"","sources":["../../src/ts/string/other.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,eAAe,GAAG,IAAI,GAAG,IAAI,GAAG,MAAM,CA6BhG"}
|
package/package.json
CHANGED
package/src/ts/day/index.ts
CHANGED
|
@@ -22,7 +22,7 @@ type BaseTime = number | string | Date | dayjs.Dayjs | null | undefined;
|
|
|
22
22
|
* @param t 各种规范或不规范的时间
|
|
23
23
|
* @returns dayjs 实例
|
|
24
24
|
* @example
|
|
25
|
-
* const d = toDayjs('2021-01-01'); // dayjs 实例
|
|
25
|
+
* const d = toDayjs('2021-01-01'); // dayjs 实例 (无参,则默认当前时间)
|
|
26
26
|
* d.format('YYYY-MM-DD HH:mm:ss'); // "2025-12-10 11:33:16"
|
|
27
27
|
* d.valueOf(); // 毫秒时间戳,如 1765337596913
|
|
28
28
|
* d.unix(); // 秒时间戳,如 1765337596
|
|
@@ -50,7 +50,7 @@ type BaseTime = number | string | Date | dayjs.Dayjs | null | undefined;
|
|
|
50
50
|
* d.diff(t, 'quarter'); // 与t相差的季度数
|
|
51
51
|
* d.diff(t, 'year'); // 与t相差的年数
|
|
52
52
|
*/
|
|
53
|
-
export function toDayjs(t
|
|
53
|
+
export function toDayjs(t?: BaseTime, fmt?: dayjs.OptionType) {
|
|
54
54
|
if (t === null || t === undefined) return dayjs();
|
|
55
55
|
if (typeof t === 'number') {
|
|
56
56
|
const s = String(Math.trunc(t));
|
|
@@ -121,7 +121,7 @@ export function getDateRangeAfter(offset: number, fmt = 'YYYY-MM-DD') {
|
|
|
121
121
|
* @returns 包含天、时、分、秒、毫秒的零填充对象
|
|
122
122
|
* @example
|
|
123
123
|
* const diff = toDayjs(t).diff(); // 毫秒差值
|
|
124
|
-
* const parts = getCountdownParts(diff); // { d: '
|
|
124
|
+
* const parts = getCountdownParts(diff); // { d: '00', h: '00', m: '00', s: '00', ms: '000' }
|
|
125
125
|
*/
|
|
126
126
|
export function getCountdownParts(diff: number) {
|
|
127
127
|
if (diff <= 0) return { d: '00', h: '00', m: '00', s: '00', ms: '000' };
|
package/src/ts/number/big.ts
CHANGED
|
@@ -21,39 +21,39 @@ export function big(x: NumLike): BigNumber {
|
|
|
21
21
|
/**
|
|
22
22
|
* 高精度加法(支持多个参数连加)。
|
|
23
23
|
* @example
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* bigPlus(0.1, 0.2); // => 0.3
|
|
25
|
+
* bigPlus('0.1', '0.2'); // => 0.3
|
|
26
|
+
* bigPlus(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
|
|
27
27
|
*/
|
|
28
|
-
export function
|
|
29
|
-
let acc = big(
|
|
30
|
-
for (const x of rest) acc = acc.plus(big(x));
|
|
28
|
+
export function bigPlus(...rest: NumLike[]) {
|
|
29
|
+
let acc = big(rest[0]);
|
|
30
|
+
for (const x of rest.slice(1)) acc = acc.plus(big(x));
|
|
31
31
|
return acc.toNumber();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* 高精度减法(支持多个参数连减)。
|
|
36
36
|
* @example
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
37
|
+
* bigMinus(1, 0.9); // => 0.1
|
|
38
|
+
* bigMinus('1.1', '0.2'); // => 0.9
|
|
39
|
+
* bigMinus(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
|
|
40
40
|
*/
|
|
41
|
-
export function
|
|
42
|
-
let acc = big(
|
|
43
|
-
for (const x of rest) acc = acc.minus(big(x));
|
|
41
|
+
export function bigMinus(...rest: NumLike[]) {
|
|
42
|
+
let acc = big(rest[0]);
|
|
43
|
+
for (const x of rest.slice(1)) acc = acc.minus(big(x));
|
|
44
44
|
return acc.toNumber();
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* 高精度乘法(支持多个参数连乘)。
|
|
49
49
|
* @example
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
50
|
+
* bigTimes(0.1, 0.2); // => 0.02
|
|
51
|
+
* bigTimes('1.5', '3'); // => 4.5
|
|
52
|
+
* bigTimes(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
|
|
53
53
|
*/
|
|
54
|
-
export function
|
|
55
|
-
let acc = big(
|
|
56
|
-
for (const x of rest) acc = acc.times(big(x));
|
|
54
|
+
export function bigTimes(...rest: NumLike[]) {
|
|
55
|
+
let acc = big(rest[0]);
|
|
56
|
+
for (const x of rest.slice(1)) acc = acc.times(big(x));
|
|
57
57
|
return acc.toNumber();
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -64,9 +64,9 @@ export function bigMul(a: NumLike, ...rest: NumLike[]) {
|
|
|
64
64
|
* bigDiv('10', '4'); // => 2.5
|
|
65
65
|
* bigDiv(100, 5, 2); // => 10 // 多参数连除: 100/5/2
|
|
66
66
|
*/
|
|
67
|
-
export function bigDiv(
|
|
68
|
-
let acc = big(
|
|
69
|
-
for (const x of rest) acc = acc.div(big(x));
|
|
67
|
+
export function bigDiv(...rest: NumLike[]) {
|
|
68
|
+
let acc = big(rest[0]);
|
|
69
|
+
for (const x of rest.slice(1)) acc = acc.div(big(x));
|
|
70
70
|
return acc.toNumber();
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -84,7 +84,7 @@ export function bigPow(x: NumLike, y: NumLike) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
87
|
+
* 四舍五入到指定小数位数
|
|
88
88
|
* @param x 需要舍入的数值。
|
|
89
89
|
* @param dp 保留的小数位数,默认 `0`(取整)。
|
|
90
90
|
* @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
|
|
@@ -106,13 +106,13 @@ export function bigRound(x: NumLike, dp = 0, rm: BigNumber.RoundingMode = BigNum
|
|
|
106
106
|
* @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
|
|
107
107
|
* @returns 格式化后的字符串。
|
|
108
108
|
* @example
|
|
109
|
-
*
|
|
110
|
-
* +
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
109
|
+
* bigFixed('1'); // => '1.00'
|
|
110
|
+
* +bigFixed('1'); // => 1
|
|
111
|
+
* bigFixed(1.2345); // => '1.23'
|
|
112
|
+
* bigFixed(1.2345, 3); // => '1.235'
|
|
113
|
+
* bigFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
|
|
114
114
|
*/
|
|
115
|
-
export function
|
|
115
|
+
export function bigFixed(
|
|
116
116
|
x: NumLike,
|
|
117
117
|
dp = 2,
|
|
118
118
|
rm: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP,
|
|
@@ -144,41 +144,41 @@ export function bigEqual(a: NumLike, b: NumLike): boolean {
|
|
|
144
144
|
/**
|
|
145
145
|
* 判断 a 是否大于 b。
|
|
146
146
|
* @example
|
|
147
|
-
*
|
|
148
|
-
*
|
|
147
|
+
* bigGreaterThan(2, 1); // => true
|
|
148
|
+
* bigGreaterThan(1, 2); // => false
|
|
149
149
|
*/
|
|
150
|
-
export function
|
|
150
|
+
export function bigGreaterThan(a: NumLike, b: NumLike): boolean {
|
|
151
151
|
return big(a).isGreaterThan(big(b));
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
155
|
* 判断 a 是否大于等于 b。
|
|
156
156
|
* @example
|
|
157
|
-
*
|
|
158
|
-
*
|
|
157
|
+
* bigGreaterThanOrEqual(2, 2); // => true
|
|
158
|
+
* bigGreaterThanOrEqual(1, 2); // => false
|
|
159
159
|
*/
|
|
160
|
-
export function
|
|
160
|
+
export function bigGreaterThanOrEqualTo(a: NumLike, b: NumLike): boolean {
|
|
161
161
|
return big(a).isGreaterThanOrEqualTo(big(b));
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
165
|
* 判断 a 是否小于 b。
|
|
166
166
|
* @example
|
|
167
|
-
*
|
|
168
|
-
*
|
|
167
|
+
* bigLessThan(1, 2); // => true
|
|
168
|
+
* bigLessThan(2, 1); // => false
|
|
169
169
|
*/
|
|
170
|
-
export function
|
|
170
|
+
export function bigLessThan(a: NumLike, b: NumLike): boolean {
|
|
171
171
|
return big(a).isLessThan(big(b));
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
175
|
* 判断 a 是否小于等于 b。
|
|
176
176
|
* @example
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
177
|
+
* bigLessThanOrEqual(2, 2); // => true
|
|
178
|
+
* bigLessThanOrEqual(1, 2); // => true
|
|
179
|
+
* bigLessThanOrEqual(2, 1); // => false
|
|
180
180
|
*/
|
|
181
|
-
export function
|
|
181
|
+
export function bigLessThanOrEqual(a: NumLike, b: NumLike): boolean {
|
|
182
182
|
return big(a).isLessThanOrEqualTo(big(b));
|
|
183
183
|
}
|
|
184
184
|
|
package/src/ts/string/other.ts
CHANGED
|
@@ -1,33 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 1
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
* @returns 字符串的字节长度
|
|
2
|
+
* 获取字节长度 (支持字符串、Buffer/Uint8Array、File/Blob 等类型)
|
|
3
|
+
* - 字符串按 UTF-8 编码计算字节长度(每个字符 1-4 字节)
|
|
4
|
+
* - Buffer/Uint8Array 直接返回字节长度(每个元素 1 字节)
|
|
5
|
+
* - File/Blob 返回文件/Blob 大小(字节数)
|
|
6
|
+
* @param data 输入的数据
|
|
7
|
+
* @returns 数据的字节长度
|
|
9
8
|
* @example
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* getByteLength('abc') // 3
|
|
10
|
+
* getByteLength('中文') // 6
|
|
11
|
+
* getByteLength('😊') // 4
|
|
12
|
+
* getByteLength(new Uint8Array([0x41, 0x42, 0x43])) // 3
|
|
13
|
+
* getByteLength(new File(['abc'], 'test.txt')) // 3
|
|
14
|
+
* getByteLength(new Blob(['中文'], { type: 'text/plain' })) // 6
|
|
13
15
|
*/
|
|
14
|
-
export function
|
|
15
|
-
|
|
16
|
+
export function getByteLength(data: string | ArrayBuffer | ArrayBufferView | File | Blob): number {
|
|
17
|
+
if (typeof data === 'string') {
|
|
18
|
+
let byteLen = 0;
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
for (let i = 0; i < data.length; i++) {
|
|
21
|
+
const code = data.charCodeAt(i);
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
if (code <= 0x7f) {
|
|
24
|
+
byteLen += 1; // (ASCII 基本拉丁)→ 包含数字 0-9、英文字母 A-Z/a-z、常见符号
|
|
25
|
+
} else if (code <= 0x7ff) {
|
|
26
|
+
byteLen += 2; // (拉丁扩展)→ 包含拉丁字母(含变音符)、希腊文、俄文/西里尔文、希伯来文、阿拉伯文等
|
|
27
|
+
} else if (code >= 0xd800 && code <= 0xdbff) {
|
|
28
|
+
byteLen += 4; // (UTF-16 代理项)→ 包含 emoji、稀有汉字(扩展区)、音乐符号等
|
|
29
|
+
i++;
|
|
30
|
+
} else {
|
|
31
|
+
byteLen += 3; // (BMP 绝大部分)→ 包含中文/日文/韩文的大多数字符(CJK 统一汉字)、以及大量其它脚本
|
|
32
|
+
}
|
|
29
33
|
}
|
|
34
|
+
|
|
35
|
+
return byteLen;
|
|
30
36
|
}
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
// Buffer/Uint8Array
|
|
39
|
+
if ('byteLength' in data) return data.byteLength;
|
|
40
|
+
|
|
41
|
+
// File/Blob
|
|
42
|
+
if ('size' in data) return data.size;
|
|
43
|
+
|
|
44
|
+
throw new TypeError('getByteLength: Unsupported type');
|
|
33
45
|
}
|