57ea4d9858
- **docs**: 完善组件注释、README 目录结构及 AGENTS.md 文档。 - **refactor**: - 提取通用 `PageHeader`、`Button`、`DashboardCard` 及 `ErrorBoundary` 组件。 - 重构消息通信机制,采用 `@webext-core/messaging` 实现类型安全。 - 将全局通知系统重构为 `SnackbarProvider` (后合并至 `GlobalSnackbar`)。 - 迁移样式系统至 MUI 主题,移除冗余 CSS。 - 优化路由配置,支持独立标签页模式及页面懒加载。 - 移除未使用文件、URL 工具及表单映射相关功能。 - **feat**: - 新增配置导出功能(JSON)及状态提示。 - 新增侧边栏状态变化通知机制。 - 新增文本统计及 JWT 解析工具。 - 优化二维码生成与解析逻辑,换用更轻量的 `qrious` 和 `qr-scanner`。 - 增强高亮器功能,支持闪烁效果及 Shadow DOM 穿透。 - **style**: 优化仪表盘响应式网格布局及 UI 细节。 - **fix**: 修复 `useStorageState` 依赖缺失及路由初始化性能问题。 - **test**: 更新单元测试以覆盖新增的工具函数及功能特性。
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
/**
|
|
* 文本统计信息接口
|
|
*/
|
|
export interface TextStats {
|
|
/** 字符数(包含空格和特殊字符) */
|
|
characters: number;
|
|
/** 单词数 */
|
|
words: number;
|
|
/** 行数 */
|
|
lines: number;
|
|
/** 字节大小 */
|
|
bytes: number;
|
|
}
|
|
|
|
/**
|
|
* 计算文本统计信息
|
|
*
|
|
* @param text 输入的文本内容
|
|
* @returns 统计结果对象
|
|
*/
|
|
export function getTextStats(text: string): TextStats {
|
|
if (!text) {
|
|
return { characters: 0, words: 0, lines: 0, bytes: 0 };
|
|
}
|
|
|
|
// 1. 字符数:统计总字符数量
|
|
const characters = text.length;
|
|
|
|
// 2. 单词数:使用 Intl.Segmenter 识别单词边界
|
|
// 这能很好地处理中英文混合文本。中文会按词组切分,英文按单词切分。
|
|
let words = 0;
|
|
try {
|
|
const segmenter = new Intl.Segmenter(undefined, { granularity: 'word' });
|
|
const segments = segmenter.segment(text);
|
|
for (const segment of segments) {
|
|
// isWordLike 为 true 表示该片段是“类词”的(非空格、非标点)
|
|
if (segment.isWordLike) {
|
|
words++;
|
|
}
|
|
}
|
|
} catch {
|
|
// 降级方案:如果不支持 Intl.Segmenter,使用正则匹配英文单词
|
|
// 但对中文支持较差
|
|
const englishWords = text.match(/\b\w+\b/g) || [];
|
|
const chineseChars = text.match(/[\u4e00-\u9fa5]/g) || [];
|
|
words = englishWords.length + chineseChars.length;
|
|
}
|
|
|
|
// 3. 行数:统计换行符数量
|
|
// 空字符串已在上方处理。非空文本至少有一行。
|
|
const lines = text.split('\n').length;
|
|
|
|
// 4. 字节大小:计算文本内容的字节数 (UTF-8)
|
|
const bytes = new TextEncoder().encode(text).length;
|
|
|
|
return { characters, words, lines, bytes };
|
|
}
|
|
|
|
/**
|
|
* 格式化字节大小显示
|
|
*
|
|
* @param bytes 字节数
|
|
* @returns 格式化后的字符串,例如 "100 Bytes"
|
|
*/
|
|
export function formatByteSize(bytes: number): string {
|
|
return `${bytes} Bytes`;
|
|
}
|