@base-web-kits/base-tools-ts 0.9.10 → 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.
@@ -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.10",
3
+ "version": "0.9.12",
4
4
  "sideEffects": false,
5
5
  "description": "Independent TS utilities package built from src/ts.",
6
6
  "keywords": [
@@ -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' };
@@ -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
  }