chore: turn cn comment to en for common space (#376)
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
/**
|
||||
* 中文字符的Unicode范围正则表达式
|
||||
* Unicode Range Regular Expressions for Chinese Characters
|
||||
*/
|
||||
const CHINESE_REGEX = /[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff]/;
|
||||
const CHINESE_EXTRACT_REGEX = /[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3000-\u303f\uff00-\uffef]+/g;
|
||||
|
||||
/**
|
||||
* 检测文本是否包含中文字符
|
||||
* Detect whether the text contains Chinese characters
|
||||
*/
|
||||
export const containsChinese = (text: string): boolean => {
|
||||
return CHINESE_REGEX.test(text);
|
||||
};
|
||||
|
||||
/**
|
||||
* 提取文本中的中文部分
|
||||
* Extract the Chinese part of the text
|
||||
*/
|
||||
export const extractChineseParts = (text: string): string[] => {
|
||||
return text.match(CHINESE_EXTRACT_REGEX) || [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 计算文本中中文字符的数量
|
||||
* Count the number of Chinese characters in a text
|
||||
*/
|
||||
export const countChineseCharacters = (text: string): number => {
|
||||
const matches = text.match(CHINESE_EXTRACT_REGEX);
|
||||
@@ -29,7 +29,7 @@ export const countChineseCharacters = (text: string): number => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 检测文本是否主要由中文组成
|
||||
* Detect whether the text is mainly composed of Chinese
|
||||
*/
|
||||
export const isPrimarilyChinese = (text: string, threshold: number = 0.5): boolean => {
|
||||
const totalLength = text.length;
|
||||
@@ -40,7 +40,7 @@ export const isPrimarilyChinese = (text: string, threshold: number = 0.5): boole
|
||||
};
|
||||
|
||||
/**
|
||||
* 清理注释文本,移除注释符号和多余空格
|
||||
* Clean up comment text, remove comment symbols and extra spaces
|
||||
*/
|
||||
export const cleanCommentText = (
|
||||
text: string,
|
||||
@@ -50,7 +50,7 @@ export const cleanCommentText = (
|
||||
let cleaned = text;
|
||||
|
||||
if (commentType === 'single-line') {
|
||||
// 根据语言类型移除不同的单行注释符号
|
||||
// Remove different single-line comment symbols based on language type
|
||||
switch (language) {
|
||||
case 'yaml':
|
||||
case 'toml':
|
||||
@@ -70,7 +70,7 @@ export const cleanCommentText = (
|
||||
cleaned = cleaned.replace(/^\/\/\s*/, '');
|
||||
}
|
||||
} else if (commentType === 'multi-line') {
|
||||
// 根据语言类型移除不同的多行注释符号
|
||||
// Remove different multi-line comment symbols based on language type
|
||||
switch (language) {
|
||||
case 'html':
|
||||
case 'xml':
|
||||
@@ -86,32 +86,32 @@ export const cleanCommentText = (
|
||||
default:
|
||||
// JavaScript/TypeScript/Go/Java/C/C++/C#/CSS style
|
||||
cleaned = cleaned.replace(/^\/\*\s*/, '').replace(/\s*\*\/$/, '');
|
||||
// 移除每行开头的 * 符号
|
||||
// Remove the * symbol at the beginning of each line
|
||||
cleaned = cleaned.replace(/^\s*\*\s?/gm, '');
|
||||
}
|
||||
}
|
||||
|
||||
// 移除多余的空格和换行
|
||||
// Remove extra spaces and newlines
|
||||
cleaned = cleaned.trim();
|
||||
|
||||
return cleaned;
|
||||
};
|
||||
|
||||
/**
|
||||
* 验证翻译结果是否有效
|
||||
* Verify whether the translation result is valid.
|
||||
*/
|
||||
export const isValidTranslation = (original: string, translated: string): boolean => {
|
||||
// 基本验证
|
||||
// basic verification
|
||||
if (!translated || translated.trim().length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否还包含中文(可能翻译失败)
|
||||
// Check if Chinese is also included (translation may fail)
|
||||
if (containsChinese(translated)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查长度是否合理(翻译后的文本不应该比原文长太多)
|
||||
// Check if the length is reasonable (the translated text should not be much longer than the original).
|
||||
if (translated.length > original.length * 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Result } from '../types/index';
|
||||
|
||||
/**
|
||||
* 函数组合 - 从左到右执行
|
||||
* Function composition - executed from left to right
|
||||
*/
|
||||
export const pipe =
|
||||
<T>(...fns: Function[]) =>
|
||||
@@ -9,7 +9,7 @@ export const pipe =
|
||||
fns.reduce((acc, fn) => fn(acc), value);
|
||||
|
||||
/**
|
||||
* 函数组合 - 从右到左执行
|
||||
* Function composition - executed from right to left
|
||||
*/
|
||||
export const compose =
|
||||
<T>(...fns: Function[]) =>
|
||||
@@ -17,7 +17,7 @@ export const compose =
|
||||
fns.reduceRight((acc, fn) => fn(acc), value);
|
||||
|
||||
/**
|
||||
* 柯里化函数
|
||||
* currying function
|
||||
*/
|
||||
export const curry =
|
||||
(fn: Function) =>
|
||||
@@ -27,7 +27,7 @@ export const curry =
|
||||
: (...more: any[]) => curry(fn)(...args, ...more);
|
||||
|
||||
/**
|
||||
* 异步映射
|
||||
* asynchronous mapping
|
||||
*/
|
||||
export const asyncMap = curry(
|
||||
async <T, U>(fn: (item: T) => Promise<U>, items: T[]): Promise<U[]> =>
|
||||
@@ -35,7 +35,7 @@ export const asyncMap = curry(
|
||||
);
|
||||
|
||||
/**
|
||||
* 异步过滤
|
||||
* asynchronous filtering
|
||||
*/
|
||||
export const asyncFilter = curry(
|
||||
async <T>(
|
||||
@@ -48,7 +48,7 @@ export const asyncFilter = curry(
|
||||
);
|
||||
|
||||
/**
|
||||
* 异步归约
|
||||
* asynchronous reduction
|
||||
*/
|
||||
export const asyncReduce = curry(
|
||||
async <T, U>(
|
||||
@@ -65,12 +65,12 @@ export const asyncReduce = curry(
|
||||
);
|
||||
|
||||
/**
|
||||
* 创建成功结果
|
||||
* Create a successful result
|
||||
*/
|
||||
export const success = <T>(data: T): Result<T> => ({ success: true, data });
|
||||
|
||||
/**
|
||||
* 创建失败结果
|
||||
* Create failed result
|
||||
*/
|
||||
export const failure = <E>(error: E): Result<never, E> => ({
|
||||
success: false,
|
||||
@@ -78,7 +78,7 @@ export const failure = <E>(error: E): Result<never, E> => ({
|
||||
});
|
||||
|
||||
/**
|
||||
* 安全的异步操作包装
|
||||
* Safe asynchronous packaging
|
||||
*/
|
||||
export const tryCatch = async <T>(fn: () => Promise<T>): Promise<Result<T>> => {
|
||||
try {
|
||||
@@ -90,7 +90,7 @@ export const tryCatch = async <T>(fn: () => Promise<T>): Promise<Result<T>> => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 同步版本的安全操作包装
|
||||
* Synchronized version of the security operation wrapper
|
||||
*/
|
||||
export const tryCatchSync = <T>(fn: () => T): Result<T> => {
|
||||
try {
|
||||
@@ -102,7 +102,7 @@ export const tryCatchSync = <T>(fn: () => T): Result<T> => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 数组分块
|
||||
* Array chunking
|
||||
*/
|
||||
export const chunk = <T>(array: T[], size: number): T[][] => {
|
||||
const chunks: T[][] = [];
|
||||
@@ -113,13 +113,13 @@ export const chunk = <T>(array: T[], size: number): T[][] => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 延迟执行
|
||||
* delayed execution
|
||||
*/
|
||||
export const delay = (ms: number): Promise<void> =>
|
||||
new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
/**
|
||||
* 重试机制
|
||||
* retry mechanism
|
||||
*/
|
||||
export const retry = async <T>(
|
||||
fn: () => Promise<T>,
|
||||
@@ -135,7 +135,7 @@ export const retry = async <T>(
|
||||
lastError = error instanceof Error ? error : new Error(String(error));
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
await delay(delayMs * attempt); // 指数退避
|
||||
await delay(delayMs * attempt); // exponential backoff
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,7 +144,7 @@ export const retry = async <T>(
|
||||
};
|
||||
|
||||
/**
|
||||
* 深度合并对象
|
||||
* deep merge object
|
||||
*/
|
||||
export const deepMerge = <T extends Record<string, any>>(
|
||||
target: T,
|
||||
|
||||
@@ -4,7 +4,7 @@ import { tryCatch } from './fp';
|
||||
import { Result } from '../types/index';
|
||||
|
||||
/**
|
||||
* 获取Git仓库中的所有已跟踪文件
|
||||
* Get all tracked files in the Git repository
|
||||
*/
|
||||
export const getGitTrackedFiles = async (
|
||||
root: string,
|
||||
@@ -21,7 +21,7 @@ export const getGitTrackedFiles = async (
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取Git仓库中的所有文件(包括未跟踪的)
|
||||
* Get all files in the Git repository (including untracked ones)
|
||||
*/
|
||||
export const getAllGitFiles = async (
|
||||
root: string,
|
||||
@@ -29,20 +29,20 @@ export const getAllGitFiles = async (
|
||||
return tryCatch(async () => {
|
||||
const git = simpleGit(root);
|
||||
|
||||
// 获取已跟踪的文件
|
||||
// Get tracked files
|
||||
const trackedFiles = await git.raw(['ls-files']);
|
||||
const trackedFilesArray = trackedFiles
|
||||
.split('\n')
|
||||
.filter(Boolean)
|
||||
.map(file => path.resolve(root, file));
|
||||
|
||||
// 获取未跟踪的文件
|
||||
// Get untracked files
|
||||
const status = await git.status();
|
||||
const untrackedFiles = status.not_added.map(file =>
|
||||
path.resolve(root, file),
|
||||
);
|
||||
|
||||
// 合并并去重
|
||||
// Merge and deduplicate
|
||||
const allFiles = [...new Set([...trackedFilesArray, ...untrackedFiles])];
|
||||
|
||||
return allFiles;
|
||||
@@ -50,7 +50,7 @@ export const getAllGitFiles = async (
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查目录是否是Git仓库
|
||||
* Check if the directory is a Git repository
|
||||
*/
|
||||
export const isGitRepository = async (
|
||||
root: string,
|
||||
@@ -63,7 +63,7 @@ export const isGitRepository = async (
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取Git仓库的根目录
|
||||
* Get the root directory of the Git repository
|
||||
*/
|
||||
export const getGitRoot = async (cwd: string): Promise<Result<string>> => {
|
||||
return tryCatch(async () => {
|
||||
@@ -74,7 +74,7 @@ export const getGitRoot = async (cwd: string): Promise<Result<string>> => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查文件是否被Git忽略
|
||||
* Check if the file is ignored by Git
|
||||
*/
|
||||
export const isIgnoredByGit = async (
|
||||
root: string,
|
||||
@@ -86,9 +86,9 @@ export const isIgnoredByGit = async (
|
||||
|
||||
try {
|
||||
await git.raw(['check-ignore', relativePath]);
|
||||
return true; // 文件被忽略
|
||||
return true; // File is ignored
|
||||
} catch {
|
||||
return false; // 文件未被忽略
|
||||
return false; // File is not ignored
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { SourceFileLanguage, CommentPattern } from '../types/index';
|
||||
|
||||
/**
|
||||
* 根据文件扩展名识别编程语言
|
||||
* Identify programming languages by file extension
|
||||
*/
|
||||
export const detectLanguage = (filePath: string): SourceFileLanguage => {
|
||||
const ext = filePath.toLowerCase().split('.').pop();
|
||||
@@ -51,14 +51,14 @@ export const detectLanguage = (filePath: string): SourceFileLanguage => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 根据文件扩展名过滤文件
|
||||
* Filter files by file extension
|
||||
*/
|
||||
export const filterFilesByExtensions = (
|
||||
files: string[],
|
||||
extensions: string[]
|
||||
): string[] => {
|
||||
if (extensions.length === 0) {
|
||||
// 默认支持的文本文件扩展名
|
||||
// Default supported text file extensions
|
||||
const defaultExtensions = [
|
||||
'.ts', '.tsx', '.js', '.jsx', '.go', '.md', '.txt', '.json',
|
||||
'.yaml', '.yml', '.toml', '.ini', '.conf', '.config',
|
||||
@@ -75,7 +75,7 @@ export const filterFilesByExtensions = (
|
||||
const lowerFile = file.toLowerCase();
|
||||
return extensions.some(ext => {
|
||||
const lowerExt = ext.toLowerCase();
|
||||
// 如果扩展名已经有点号,直接使用;否则添加点号
|
||||
// If the extension is already numbered, use it directly; otherwise, add a dot.
|
||||
const extWithDot = lowerExt.startsWith('.') ? lowerExt : `.${lowerExt}`;
|
||||
return lowerFile.endsWith(extWithDot);
|
||||
});
|
||||
@@ -83,7 +83,7 @@ export const filterFilesByExtensions = (
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取不同编程语言的注释模式
|
||||
* Obtain comment modes for different programming languages
|
||||
*/
|
||||
export const getCommentPatterns = (language: SourceFileLanguage): CommentPattern | null => {
|
||||
const commentPatterns: Record<SourceFileLanguage, CommentPattern> = {
|
||||
@@ -108,33 +108,33 @@ export const getCommentPatterns = (language: SourceFileLanguage): CommentPattern
|
||||
multiEnd: /-->/g
|
||||
},
|
||||
text: {
|
||||
single: /^(.*)$/gm, // 文本文件每行都可能是注释
|
||||
single: /^(.*)$/gm, // Every line of a text file can be a comment
|
||||
multiStart: /^/g,
|
||||
multiEnd: /$/g
|
||||
},
|
||||
json: {
|
||||
single: /\/\/(.*)$/gm, // JSON通常不支持注释,但一些工具支持
|
||||
single: /\/\/(.*)$/gm, // JSON usually doesn't support comments, but some tools do
|
||||
multiStart: /\/\*/g,
|
||||
multiEnd: /\*\//g
|
||||
},
|
||||
yaml: {
|
||||
single: /#(.*)$/gm,
|
||||
multiStart: /^$/g, // YAML不支持多行注释
|
||||
multiStart: /^$/g, // YAML does not support multi-line comments
|
||||
multiEnd: /^$/g
|
||||
},
|
||||
toml: {
|
||||
single: /#(.*)$/gm,
|
||||
multiStart: /^$/g, // TOML不支持多行注释
|
||||
multiStart: /^$/g, // TOML does not support multi-line comments
|
||||
multiEnd: /^$/g
|
||||
},
|
||||
ini: {
|
||||
single: /[;#](.*)$/gm, // INI文件支持 ; 和 # 作为注释
|
||||
multiStart: /^$/g, // INI不支持多行注释
|
||||
single: /[;#](.*)$/gm, // INI file support; and #as comments
|
||||
multiStart: /^$/g, // INI does not support multi-line comments
|
||||
multiEnd: /^$/g
|
||||
},
|
||||
shell: {
|
||||
single: /#(.*)$/gm,
|
||||
multiStart: /^$/g, // Shell脚本不支持多行注释
|
||||
multiStart: /^$/g, // Shell scripts do not support multi-line comments
|
||||
multiEnd: /^$/g
|
||||
},
|
||||
python: {
|
||||
@@ -143,22 +143,22 @@ export const getCommentPatterns = (language: SourceFileLanguage): CommentPattern
|
||||
multiEnd: /[\s\S]*?"""/gm
|
||||
},
|
||||
css: {
|
||||
single: /^$/g, // CSS不支持单行注释
|
||||
single: /^$/g, // CSS does not support single-line comments
|
||||
multiStart: /\/\*/g,
|
||||
multiEnd: /\*\//g
|
||||
},
|
||||
html: {
|
||||
single: /^$/g, // HTML不支持单行注释
|
||||
single: /^$/g, // HTML does not support single-line comments
|
||||
multiStart: /<!--/g,
|
||||
multiEnd: /-->/g
|
||||
},
|
||||
xml: {
|
||||
single: /^$/g, // XML不支持单行注释
|
||||
single: /^$/g, // XML does not support single-line comments
|
||||
multiStart: /<!--/g,
|
||||
multiEnd: /-->/g
|
||||
},
|
||||
php: {
|
||||
single: /(?:\/\/|#)(.*)$/gm, // PHP支持 // 和 # 作为单行注释
|
||||
single: /(?:\/\/|#)(.*)$/gm, // PHP supports//and #as single-line comments
|
||||
multiStart: /\/\*/g,
|
||||
multiEnd: /\*\//g
|
||||
},
|
||||
@@ -208,7 +208,7 @@ export const getCommentPatterns = (language: SourceFileLanguage): CommentPattern
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查文件是否支持处理
|
||||
* Check if the file supports processing
|
||||
*/
|
||||
export const isSupportedFile = (filePath: string): boolean => {
|
||||
const language = detectLanguage(filePath);
|
||||
@@ -216,7 +216,7 @@ export const isSupportedFile = (filePath: string): boolean => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取文件的MIME类型(用于判断是否为文本文件)
|
||||
* Get the MIME type of the file (used to determine whether it is a text file)
|
||||
*/
|
||||
export const isTextFile = (filePath: string): boolean => {
|
||||
const textExtensions = [
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* 信号量并发控制类
|
||||
* semaphore concurrency control class
|
||||
*/
|
||||
export class Semaphore {
|
||||
private permits: number;
|
||||
@@ -10,7 +10,7 @@ export class Semaphore {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取许可
|
||||
* Get permission
|
||||
*/
|
||||
async acquire(): Promise<void> {
|
||||
if (this.permits > 0) {
|
||||
@@ -24,7 +24,7 @@ export class Semaphore {
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放许可
|
||||
* release permission
|
||||
*/
|
||||
release(): void {
|
||||
this.permits++;
|
||||
@@ -36,14 +36,14 @@ export class Semaphore {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前可用许可数
|
||||
* Get the number of currently available licenses
|
||||
*/
|
||||
available(): number {
|
||||
return this.permits;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取等待队列长度
|
||||
* Get the waiting queue length
|
||||
*/
|
||||
waitingCount(): number {
|
||||
return this.waiting.length;
|
||||
|
||||
Reference in New Issue
Block a user