@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.
- package/dist/base-tools-ts.umd.global.js +20 -15
- package/dist/base-tools-ts.umd.global.js.map +1 -1
- package/dist/day/index.d.ts +1 -1
- package/dist/index.cjs +21 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +20 -15
- package/dist/index.js.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 +1 -1
- package/src/ts/string/other.ts +36 -24
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
|
@@ -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/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
|
}
|