fix: 调整翻译条目筛选逻辑
- 将输出由数组改为单个对象
This commit is contained in:
@ -23,15 +23,11 @@ test('filterTranslations - basic functionality', () => {
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
translations: [
|
||||
{ languages_code: 'en-US', name: 'English Name1' }
|
||||
]
|
||||
translations: { languages_code: 'en-US', name: 'English Name1' }
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
translations: [
|
||||
{ languages_code: 'en-US', name: 'English Name2' }
|
||||
]
|
||||
translations: { languages_code: 'en-US', name: 'English Name2' }
|
||||
}
|
||||
]);
|
||||
});
|
||||
@ -50,7 +46,7 @@ test('filterTranslations - no matching language', () => {
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
translations: []
|
||||
translations: {}
|
||||
}
|
||||
]);
|
||||
});
|
||||
@ -85,9 +81,7 @@ test('filterTranslations - translations in deeper structure', () => {
|
||||
{
|
||||
id: 1,
|
||||
details: {
|
||||
translations: [
|
||||
{ languages_code: 'zh-CN', name: '中文名称1' }
|
||||
]
|
||||
translations: { languages_code: 'zh-CN', name: '中文名称1' }
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
@ -33,15 +33,11 @@ type Translation = {
|
||||
* // [
|
||||
* // {
|
||||
* // id: 1,
|
||||
* // translations: [
|
||||
* // { languages_code: 'zh-CN', name: '中文名称1' }
|
||||
* // ]
|
||||
* // translations: { languages_code: 'zh-CN', name: '中文名称1' }
|
||||
* // },
|
||||
* // {
|
||||
* // id: 2,
|
||||
* // translations: [
|
||||
* // { languages_code: 'zh-CN', name: '中文名称2' }
|
||||
* // ]
|
||||
* // translations: { languages_code: 'zh-CN', name: '中文名称2' }
|
||||
* // }
|
||||
* // ]
|
||||
*/
|
||||
@ -73,7 +69,13 @@ export function filterTranslations<T>(data: T[], langCode: string): T[] {
|
||||
}
|
||||
return false;
|
||||
});
|
||||
result[key] = filtered;
|
||||
|
||||
// 展平结果为单个对象或空对象
|
||||
if (filtered.length <= 1) {
|
||||
result[key] = filtered[0] || {};
|
||||
} else {
|
||||
result[key] = filtered;
|
||||
}
|
||||
} else {
|
||||
// 递归处理其他字段
|
||||
result[key] = deepFilter(value);
|
||||
|
||||
Reference in New Issue
Block a user