Skip to content

Commit 00c11ed

Browse files
committed
chore: translate English comments to Chinese
1 parent 853ef05 commit 00c11ed

File tree

26 files changed

+334
-300
lines changed

26 files changed

+334
-300
lines changed

src/checkBrowserSupport/compareVersions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
2-
* Compare two version numbers
2+
* 比较两个版本号
33
*
44
* @private
5-
* @param version1 First version number
6-
* @param version2 Second version number
7-
* @returns {number} Returns 1 if version1 > version2, returns -1 if version1 < version2, returns 0 if equal
5+
* @param version1 第一个版本号
6+
* @param version2 第二个版本号
7+
* @returns {number} version1 > version2 时返回 1 ,当 version1 < version2 时返回 -1 ,相等时返回 0
88
*/
99
function compareVersions(version1: string, version2: string): number {
1010
const v1Parts = version1.split('.').map(Number);

src/checkBrowserSupport/index.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,38 @@ export interface IBrowser {
1010
}
1111

1212
/**
13-
* Checks if the current browser meets minimum version requirements
13+
* 检查当前浏览器是否满足最低版本要求
1414
*
15-
* @category Environment Detection
15+
* @category 环境检测
1616
* @description
17-
* A utility function that validates whether the current browser satisfies specified minimum version
18-
* requirements. The function accepts an array of browser specifications and performs version
19-
* comparison checks.
17+
* 该函数会检查当前浏览器的版本是否大于或等于指定的最低版本要求。
18+
* 如果当前浏览器不在指定的浏览器列表中,将返回 false。
2019
*
21-
* @param {IBrowser[]} browsers - An array of browser specifications, where each specification contains:
22-
* - `name`: Browser name (e.g., 'chrome', 'firefox', 'safari')
23-
* - `version`: Minimum required version number
24-
* @returns {boolean} Returns `true` if current browser meets version requirements, `false` otherwise
20+
* @param {IBrowser[]} browsers - 浏览器最低版本配置数组
21+
* - `name`: 浏览器名称,不区分大小写(如:'chrome'、'firefox'、'safari'等)
22+
* - `version`: 最低版本号要求,支持字符串或数字格式(如:'80'、75、'13.0.1'等)
23+
* @returns {boolean} 返回检查结果
24+
* - 当前浏览器版本满足要求时,返回 `true`
25+
* - 当前浏览器版本不满足要求或浏览器类型不匹配时,返回 `false`
26+
* - 传入空数组时,返回 `false`
2527
*
2628
* @example
2729
* ```typescript
2830
* import { checkBrowserSupport } from 'dt-utils';
2931
*
30-
* // Single browser check
32+
* // 检查单个浏览器版本要求
3133
* checkBrowserSupport([
3234
* { name: 'chrome', version: '80' }
3335
* ]);
3436
*
35-
* // Multiple browser support
37+
* // 检查多个浏览器版本要求(满足其中之一即可)
3638
* checkBrowserSupport([
3739
* { name: 'chrome', version: '80' },
3840
* { name: 'firefox', version: '75' },
3941
* { name: 'safari', version: '13' }
4042
* ]);
4143
*
42-
* // Version can be string or number
44+
* // 版本号支持字符串或数字格式
4345
* checkBrowserSupport([
4446
* { name: 'chrome', version: 80 },
4547
* { name: 'firefox', version: '75.0.1' }

src/copy/createFakeElement.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
/**
2-
* Creates a temporary textarea element for clipboard operations
3-
* This element is positioned off-screen and used as an intermediary for copying text
4-
* when the Clipboard API is not available
2+
* 创建一个用于剪贴板操作的临时文本区域元素
53
*
64
* @private
7-
* @param {string} value - Text content to be copied to clipboard
8-
* @returns {HTMLTextAreaElement} A configured textarea element ready for clipboard operations
5+
* @param {string} value - 要复制到剪贴板的文本内容
6+
* @returns {HTMLTextAreaElement} 一个配置好的、可用于剪贴板操作的文本区域元素
97
*/
108
function createFakeElement(value: string): HTMLTextAreaElement {
119
const isRTL = document.documentElement.getAttribute('dir') === 'rtl';
1210
const fakeElement = document.createElement('textarea');
1311

1412
/**
15-
* Prevent zooming on iOS and reset box model
16-
* Move element out of screen horizontally
13+
* 防止在iOS上缩放并重置盒模型
14+
* 将元素水平移出屏幕
1715
*/
1816
Object.assign(fakeElement.style, {
1917
visibility: 'hidden',

src/copy/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import createFakeElement from './createFakeElement';
22
/**
3-
* A robust cross-browser clipboard utility function.
3+
* 浏览器剪贴板复制工具函数。
44
*
55
* @category Utils
66
* @description
7-
* This function provides a consistent and reliable way to copy text to the clipboard across different browsers.
8-
* It leverages the modern Clipboard API when available, falling back to the execCommand('copy') method for legacy browsers.
7+
* 提供一个一致且可靠的方式来在不同浏览器中复制文本到剪贴板。
8+
* 它优先使用现代的 Clipboard API,当不可用时会降级使用 execCommand('copy') 方法来支持旧版浏览器。
99
*
10-
* @param {string} text - The text content to copy
11-
* @returns {Promise<boolean>} Copy operation result
12-
* - Resolves to true if copy succeeds
13-
* - Resolves to false if copy fails
10+
* @param {string} text - 要复制的文本内容
11+
* @returns {Promise<boolean>} 复制操作结果
12+
* - 复制成功时返回 true
13+
* - 复制失败时返回 false
1414
*
1515
* @example
1616
* ```typescript
1717
* import { copy } from 'dt-utils';
1818
*
19-
* // Simple copy
19+
* // 简单复制
2020
* const text = "Hello, clipboard!";
2121
* const success = await copy(text);
2222
* console.log(success ? "Copied!" : "Copy failed");
2323
*
24-
* // Copy with error handling
24+
* // 带错误处理的复制
2525
* try {
2626
* const text = "Sensitive data";
2727
* if (await copy(text)) {
@@ -33,7 +33,7 @@ import createFakeElement from './createFakeElement';
3333
* handleError(error);
3434
* }
3535
*
36-
* // Copy in button click handler
36+
* // 在按钮点击处理程序中复制
3737
* button.addEventListener('click', async () => {
3838
* const success = await copy('Click to copy text');
3939
* button.textContent = success ? 'Copied!' : 'Try again';

src/downloadFile/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,41 @@ export interface DownloadOptions extends Omit<RequestInit, 'body' | 'method'> {
55
}
66

77
/**
8-
* Downloads a file from the specified URL using a POST request
8+
* 从指定 URL 下载文件
99
*
1010
* @category Utils
1111
* @description
12-
* This function sends a POST request to the specified URL with the provided data.
13-
* The response is expected to be a file. If the response is successful, the file is downloaded
14-
* with the specified filename. If the response is not successful, an error is thrown.
12+
* 通过向指定 URL 发送 HTTP 请求(默认为 POST 方法)来下载文件。
1513
*
16-
* Note: This function assumes that the server responds with a file. If the server responds
17-
* with a different content type, an error will be thrown.
14+
* 主要功能:
15+
* - 支持自定义请求数据和下载文件名
16+
* - 自动从响应头获取文件名(如果未指定)
17+
* - 处理下载失败的错误情况
1818
*
19-
* @param {string} url - The URL to download the file from
20-
* @param {RequestData} data - The data to be sent in the request body (optional)
21-
* @param {DownloadOptions} options - Request options including optional fileName
22-
* @returns {Promise<Response>} Promise that resolves with the fetch Response object
19+
* 注意事项:
20+
* - 服务端必须返回文件流,若返回 JSON 格式将抛出错误
21+
* - 下载失败时会返回原始响应对象以便进一步处理
22+
* - 仅支持浏览器环境,依赖 document API
23+
*
24+
* @param {string} url - 下载文件的目标 URL
25+
* @param {RequestData} data - POST 请求携带的数据对象(可选)
26+
* @param {DownloadOptions} options - 下载配置项,可包含自定义文件名和其他 fetch 选项
27+
* @returns {Promise<Response>} 返回原始响应对象的 Promise
2328
*
2429
* @example
2530
* ```typescript
2631
* import { downloadFile } from 'dt-utils';
2732
*
28-
* // Basic usage
33+
* // 基本用法
2934
* await downloadFile('https://api.example.com/download', { id: 123 });
3035
*
31-
* // With custom filename
36+
* // 使用自定义文件名
3237
* await downloadFile('https://api.example.com/download',
3338
* { id: 123 },
3439
* { fileName: 'custom.pdf' }
3540
* );
3641
*
37-
* // With additional fetch options
42+
* // 使用额外的 fetch 选项
3843
* await downloadFile('https://api.example.com/download',
3944
* { id: 123 },
4045
* {

src/formatBytes/index.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,48 @@ type ByteUnit = 'B' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB' | 'EB' | 'ZB' | 'YB';
33
type FormattedBytes = `${number} ${ByteUnit}` | 'Invalid value' | '0 B';
44

55
/**
6-
* Formats a numeric byte value into a string with appropriate size units.
6+
* 将字节数值格式化为带单位的字符串表示。
77
*
8-
* @category Formatting
8+
* @category 格式化
99
* @description
10-
* Converts a byte value into a human-readable string representation with the most appropriate
11-
* size unit, ranging from Bytes (B) to Yottabytes (YB). The conversion uses 1024 as the base
12-
* unit multiplier (1 KB = 1024 B).
10+
* 自动将字节数值转换为最适合的单位表示形式,支持从字节(B)到尧字节(YB)的单位范围。
11+
* 转换采用二进制换算(1024进制),即:
12+
* - 1 KB = 1024 B
13+
* - 1 MB = 1024 KB
14+
* - 以此类推...
1315
*
14-
* @param {number} value - The numeric value in bytes to format
15-
* @param {number} [decimals=2] - The number of decimal places to round to (defaults to 2)
16-
* @returns {FormattedBytes} A formatted string in the format "value unit" (e.g. "1.5 MB") or error message
16+
* 特殊情况处理:
17+
* - 当输入为负数、NaN或Infinity时,返回"Invalid value"
18+
* - 当输入为0时,返回"0 B"
19+
*
20+
* @param {number} value - 要格式化的字节数值
21+
* @param {number} [decimals=2] - 结果保留的小数位数,默认为2位
22+
* @returns {FormattedBytes} 格式化后的字符串,例如"1.5 MB"。若输入无效则返回"Invalid value"
1723
*
1824
* @example
1925
* ```typescript
2026
* import { formatBytes } from 'dt-utils';
2127
*
22-
* // Basic formatting
28+
* // 基本格式化
2329
* formatBytes(1024) // => "1 KB"
2430
* formatBytes(1536) // => "1.5 KB"
2531
* formatBytes(1048576) // => "1 MB"
2632
*
27-
* // Specifying decimal places
33+
* // 指定小数位数
2834
* formatBytes(1234567, 1) // => "1.2 MB"
2935
* formatBytes(1234567, 3) // => "1.178 MB"
3036
*
31-
* // Large numbers
37+
* // 大数值
3238
* formatBytes(1.5e12) // => "1.36 TB"
3339
*
34-
* // Edge cases
40+
* // 边界情况
3541
* formatBytes(0) // => "0 B"
3642
* formatBytes(-1024) // => "Invalid value"
3743
* formatBytes(Infinity) // => "Invalid value"
3844
* formatBytes(NaN) // => "Invalid value"
3945
* ```
4046
*
41-
* @see {@link https://en.wikipedia.org/wiki/Byte#Multiple-byte_units} for more information about byte units
47+
* @see {@link https://en.wikipedia.org/wiki/Byte#Multiple-byte_units} 查看更多关于字节单位的信息
4248
*/
4349
const formatBytes = (value: number, decimals = 2): FormattedBytes => {
4450
if (!Number.isFinite(value)) return 'Invalid value';

0 commit comments

Comments
 (0)