@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.
@@ -16,27 +16,27 @@ export declare function big(x: NumLike): BigNumber;
16
16
  /**
17
17
  * 高精度加法(支持多个参数连加)。
18
18
  * @example
19
- * bigAdd(0.1, 0.2); // => 0.3
20
- * bigAdd('0.1', '0.2'); // => 0.3
21
- * bigAdd(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
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 bigAdd(a: NumLike, ...rest: NumLike[]): number;
23
+ export declare function bigPlus(...rest: NumLike[]): number;
24
24
  /**
25
25
  * 高精度减法(支持多个参数连减)。
26
26
  * @example
27
- * bigSub(1, 0.9); // => 0.1
28
- * bigSub('1.1', '0.2'); // => 0.9
29
- * bigSub(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
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 bigSub(a: NumLike, ...rest: NumLike[]): number;
31
+ export declare function bigMinus(...rest: NumLike[]): number;
32
32
  /**
33
33
  * 高精度乘法(支持多个参数连乘)。
34
34
  * @example
35
- * bigMul(0.1, 0.2); // => 0.02
36
- * bigMul('1.5', '3'); // => 4.5
37
- * bigMul(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
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 bigMul(a: NumLike, ...rest: NumLike[]): number;
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(a: NumLike, ...rest: NumLike[]): number;
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
- * toFixed('1'); // => '1.00'
79
- * +toFixed('1'); // => 1
80
- * toFixed(1.2345); // => '1.23'
81
- * toFixed(1.2345, 3); // => '1.235'
82
- * toFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
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 toFixed(x: NumLike, dp?: number, rm?: BigNumber.RoundingMode): string;
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
- * bigGt(2, 1); // => true
104
- * bigGt(1, 2); // => false
103
+ * bigGreaterThan(2, 1); // => true
104
+ * bigGreaterThan(1, 2); // => false
105
105
  */
106
- export declare function bigGt(a: NumLike, b: NumLike): boolean;
106
+ export declare function bigGreaterThan(a: NumLike, b: NumLike): boolean;
107
107
  /**
108
108
  * 判断 a 是否大于等于 b。
109
109
  * @example
110
- * bigGte(2, 2); // => true
111
- * bigGte(1, 2); // => false
110
+ * bigGreaterThanOrEqual(2, 2); // => true
111
+ * bigGreaterThanOrEqual(1, 2); // => false
112
112
  */
113
- export declare function bigGte(a: NumLike, b: NumLike): boolean;
113
+ export declare function bigGreaterThanOrEqualTo(a: NumLike, b: NumLike): boolean;
114
114
  /**
115
115
  * 判断 a 是否小于 b。
116
116
  * @example
117
- * bigLt(1, 2); // => true
118
- * bigLt(2, 1); // => false
117
+ * bigLessThan(1, 2); // => true
118
+ * bigLessThan(2, 1); // => false
119
119
  */
120
- export declare function bigLt(a: NumLike, b: NumLike): boolean;
120
+ export declare function bigLessThan(a: NumLike, b: NumLike): boolean;
121
121
  /**
122
122
  * 判断 a 是否小于等于 b。
123
123
  * @example
124
- * bigLte(2, 2); // => true
125
- * bigLte(1, 2); // => true
126
- * bigLte(2, 1); // => false
124
+ * bigLessThanOrEqual(2, 2); // => true
125
+ * bigLessThanOrEqual(1, 2); // => true
126
+ * bigLessThanOrEqual(2, 1); // => false
127
127
  */
128
- export declare function bigLte(a: NumLike, b: NumLike): boolean;
128
+ export declare function bigLessThanOrEqual(a: NumLike, b: NumLike): boolean;
129
129
  /**
130
130
  * 导出 BigNumber 类
131
131
  * @example
@@ -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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Q,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"}
@@ -1,15 +1,17 @@
1
1
  /**
2
- * 计算字符串在 UTF-8 编码下的字节长度。
3
- * 使用场景:
4
- * 1) 按字节限制表单输入(避免超过后端/数据库字段上限)
5
- * 2) 评估网络传输、缓存(Redis/消息队列)开销
6
- * 3) 根据字节数截断或提示用户(而非按字符数)
7
- * @param str 输入的字符串
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
- * getStringByteLength('abc') // 3
11
- * getStringByteLength('中文') // 6
12
- * getStringByteLength('😊') // 4
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 getStringByteLength(str: string): number;
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;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAmBvD"}
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base-web-kits/base-tools-ts",
3
- "version": "0.9.9",
3
+ "version": "0.9.12",
4
4
  "sideEffects": false,
5
5
  "description": "Independent TS utilities package built from src/ts.",
6
6
  "keywords": [
@@ -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: BaseTime, fmt?: dayjs.OptionType) {
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: '01', h: '02', m: '03', s: '04', ms: '567' }
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' };
@@ -21,39 +21,39 @@ export function big(x: NumLike): BigNumber {
21
21
  /**
22
22
  * 高精度加法(支持多个参数连加)。
23
23
  * @example
24
- * bigAdd(0.1, 0.2); // => 0.3
25
- * bigAdd('0.1', '0.2'); // => 0.3
26
- * bigAdd(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
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 bigAdd(a: NumLike, ...rest: NumLike[]) {
29
- let acc = big(a);
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
- * bigSub(1, 0.9); // => 0.1
38
- * bigSub('1.1', '0.2'); // => 0.9
39
- * bigSub(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
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 bigSub(a: NumLike, ...rest: NumLike[]) {
42
- let acc = big(a);
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
- * bigMul(0.1, 0.2); // => 0.02
51
- * bigMul('1.5', '3'); // => 4.5
52
- * bigMul(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
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 bigMul(a: NumLike, ...rest: NumLike[]) {
55
- let acc = big(a);
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(a: NumLike, ...rest: NumLike[]) {
68
- let acc = big(a);
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
- * toFixed('1'); // => '1.00'
110
- * +toFixed('1'); // => 1
111
- * toFixed(1.2345); // => '1.23'
112
- * toFixed(1.2345, 3); // => '1.235'
113
- * toFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
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 toFixed(
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
- * bigGt(2, 1); // => true
148
- * bigGt(1, 2); // => false
147
+ * bigGreaterThan(2, 1); // => true
148
+ * bigGreaterThan(1, 2); // => false
149
149
  */
150
- export function bigGt(a: NumLike, b: NumLike): boolean {
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
- * bigGte(2, 2); // => true
158
- * bigGte(1, 2); // => false
157
+ * bigGreaterThanOrEqual(2, 2); // => true
158
+ * bigGreaterThanOrEqual(1, 2); // => false
159
159
  */
160
- export function bigGte(a: NumLike, b: NumLike): boolean {
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
- * bigLt(1, 2); // => true
168
- * bigLt(2, 1); // => false
167
+ * bigLessThan(1, 2); // => true
168
+ * bigLessThan(2, 1); // => false
169
169
  */
170
- export function bigLt(a: NumLike, b: NumLike): boolean {
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
- * bigLte(2, 2); // => true
178
- * bigLte(1, 2); // => true
179
- * bigLte(2, 1); // => false
177
+ * bigLessThanOrEqual(2, 2); // => true
178
+ * bigLessThanOrEqual(1, 2); // => true
179
+ * bigLessThanOrEqual(2, 1); // => false
180
180
  */
181
- export function bigLte(a: NumLike, b: NumLike): boolean {
181
+ export function bigLessThanOrEqual(a: NumLike, b: NumLike): boolean {
182
182
  return big(a).isLessThanOrEqualTo(big(b));
183
183
  }
184
184
 
@@ -1,33 +1,45 @@
1
1
  /**
2
- * 计算字符串在 UTF-8 编码下的字节长度。
3
- * 使用场景:
4
- * 1) 按字节限制表单输入(避免超过后端/数据库字段上限)
5
- * 2) 评估网络传输、缓存(Redis/消息队列)开销
6
- * 3) 根据字节数截断或提示用户(而非按字符数)
7
- * @param str 输入的字符串
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
- * getStringByteLength('abc') // 3
11
- * getStringByteLength('中文') // 6
12
- * getStringByteLength('😊') // 4
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 getStringByteLength(str: string): number {
15
- let byteLen = 0;
16
+ export function getByteLength(data: string | ArrayBuffer | ArrayBufferView | File | Blob): number {
17
+ if (typeof data === 'string') {
18
+ let byteLen = 0;
16
19
 
17
- for (let i = 0; i < str.length; i++) {
18
- const code = str.charCodeAt(i);
20
+ for (let i = 0; i < data.length; i++) {
21
+ const code = data.charCodeAt(i);
19
22
 
20
- if (code <= 0x7f) {
21
- byteLen += 1; // (ASCII 基本拉丁)→ 包含数字 0-9、英文字母 A-Z/a-z、常见符号
22
- } else if (code <= 0x7ff) {
23
- byteLen += 2; // (拉丁扩展)→ 包含拉丁字母(含变音符)、希腊文、俄文/西里尔文、希伯来文、阿拉伯文等
24
- } else if (code >= 0xd800 && code <= 0xdbff) {
25
- byteLen += 4; // (UTF-16 代理项)→ 包含 emoji、稀有汉字(扩展区)、音乐符号等
26
- i++;
27
- } else {
28
- byteLen += 3; // (BMP 绝大部分)→ 包含中文/日文/韩文的大多数字符(CJK 统一汉字)、以及大量其它脚本
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
- return byteLen;
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
  }