fix: 调整翻译条目筛选逻辑

- 将输出由数组改为单个对象
This commit is contained in:
2025-10-24 08:01:51 +00:00
parent 2099fed318
commit ca613e7b34
2 changed files with 13 additions and 17 deletions

View File

@ -23,15 +23,11 @@ test('filterTranslations - basic functionality', () => {
expect(result).toEqual([ expect(result).toEqual([
{ {
id: 1, id: 1,
translations: [ translations: { languages_code: 'en-US', name: 'English Name1' }
{ languages_code: 'en-US', name: 'English Name1' }
]
}, },
{ {
id: 2, id: 2,
translations: [ translations: { languages_code: 'en-US', name: 'English Name2' }
{ languages_code: 'en-US', name: 'English Name2' }
]
} }
]); ]);
}); });
@ -50,7 +46,7 @@ test('filterTranslations - no matching language', () => {
expect(result).toEqual([ expect(result).toEqual([
{ {
id: 1, id: 1,
translations: [] translations: {}
} }
]); ]);
}); });
@ -85,9 +81,7 @@ test('filterTranslations - translations in deeper structure', () => {
{ {
id: 1, id: 1,
details: { details: {
translations: [ translations: { languages_code: 'zh-CN', name: '中文名称1' }
{ languages_code: 'zh-CN', name: '中文名称1' }
]
} }
} }
]); ]);

View File

@ -33,15 +33,11 @@ type Translation = {
* // [ * // [
* // { * // {
* // id: 1, * // id: 1,
* // translations: [ * // translations: { languages_code: 'zh-CN', name: '中文名称1' }
* // { languages_code: 'zh-CN', name: '中文名称1' }
* // ]
* // }, * // },
* // { * // {
* // id: 2, * // id: 2,
* // translations: [ * // translations: { languages_code: 'zh-CN', name: '中文名称2' }
* // { languages_code: 'zh-CN', name: '中文名称2' }
* // ]
* // } * // }
* // ] * // ]
*/ */
@ -73,7 +69,13 @@ export function filterTranslations<T>(data: T[], langCode: string): T[] {
} }
return false; return false;
}); });
result[key] = filtered;
// 展平结果为单个对象或空对象
if (filtered.length <= 1) {
result[key] = filtered[0] || {};
} else {
result[key] = filtered;
}
} else { } else {
// 递归处理其他字段 // 递归处理其他字段
result[key] = deepFilter(value); result[key] = deepFilter(value);