* feat(i18n): 优化i18n check脚本 * fix: 移除重复的代码行和错误信息 * fix: 重新排序 * fix: i18n sort * test * test * test * test * test * test * feat(i18n): 添加同步翻译脚本并重构检查脚本 重构 check-i18n 脚本为纯检查功能,不再自动修改翻译文件 新增 sync-i18n 脚本用于自动同步翻译文件结构 更新 package.json 添加 sync:i18n 命令 移除不再需要的 husky pre-commit.js 钩子 * docs(i18n): 移除未使用的测试翻译文本 * feat(scripts): 添加对象键名排序功能及测试 添加 lexicalSort 函数和 sortedObjectByKeys 函数用于按字典序排序对象键名 新增测试用例验证排序功能 * feat(i18n): 添加翻译文件键值排序检查功能 添加对i18n翻译文件键值字典序的检查功能,确保翻译文件保持一致的排序结构 * refactor(i18n): 优化同步逻辑并添加键排序功能 移除syncRecursively的返回值检查,简化同步流程 添加对翻译文件的键排序功能,使用sortedObjectByKeys工具 确保主模板和翻译文件在同步后都保持有序 * fix(i18n): 重新排序翻译文件 * style(scripts): 格式化sort.js * chore: 将 test:sort 重命名为 test:scripts
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
// https://github.com/Gudahtt/prettier-plugin-sort-json/blob/main/src/index.ts
|
|
/**
|
|
* Lexical sort function for strings, meant to be used as the sort
|
|
* function for `Array.prototype.sort`.
|
|
*
|
|
* @param a - First element to compare.
|
|
* @param b - Second element to compare.
|
|
* @returns A number indicating which element should come first.
|
|
*/
|
|
function lexicalSort(a: string, b: string): number {
|
|
if (a > b) {
|
|
return 1
|
|
}
|
|
if (a < b) {
|
|
return -1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* 对对象的键按照字典序进行排序(支持嵌套对象)
|
|
* @param obj 需要排序的对象
|
|
* @returns 返回排序后的新对象
|
|
*/
|
|
export function sortedObjectByKeys(obj: object): object {
|
|
const sortedKeys = Object.keys(obj).sort(lexicalSort)
|
|
|
|
const sortedObj = {}
|
|
for (const key of sortedKeys) {
|
|
let value = obj[key]
|
|
// 如果值是对象,递归排序
|
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
value = sortedObjectByKeys(value)
|
|
}
|
|
sortedObj[key] = value
|
|
}
|
|
|
|
return sortedObj
|
|
}
|