diff --git a/src/builder.spec.ts b/src/builder.spec.ts index a0d4f09..6695e64 100644 --- a/src/builder.spec.ts +++ b/src/builder.spec.ts @@ -1191,6 +1191,43 @@ describe('Builder', () => { } ); }); + test('remove newline in description of xlf2 when trim=false and collapseWhitespace=true', async () => { + await runTest({ + messagesBefore: '\n' + + ' \n' + + ' \n' + + ' \n' + + ' some-file.ts:281,286\n' + + ' some-other-file.ts:281\n' + + ' test description\n' + + '\n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + '', + options: { + format: 'xlf2', + includeContext: true + }, + messagesExpected: '\n' + + ' \n' + + ' \n' + + ' \n' + + ' some-file.ts:281,286\n' + + ' some-other-file.ts:281\n' + + ' test description \n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + '', + }); + }); test('retain whitespaces when trim=false and collapseWhitespace=true', async () => { await runTest( { diff --git a/src/builder.ts b/src/builder.ts index 50819f1..566d6d8 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -112,10 +112,10 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext const translationSourceFile = fromXlf(await fs.readFile(sourcePath, 'utf8')); const sort: Options['sort'] = options.sort ?? 'stableAppendNew'; - const identityMapper = (x: string) => x; + const identityMapper = (x: T) => x; const mapper = pipe( (options.collapseWhitespace ?? true) ? doCollapseWhitespace : identityMapper, - (options.trim ?? false) ? ((text: string) => text.trim()) : identityMapper + options.trim ?? false ? (text: T): T => text?.trim() as T : identityMapper ); const removeContextSource = options.includeContext !== true && options.includeContext !== 'sourceFileOnly'; const normalizedTranslationSourceFile = translationSourceFile.mapUnitsList(units => { @@ -126,8 +126,8 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext source: mapper(unit.source), target: unit.target !== undefined ? mapper(unit.target) : undefined, locations: removeContextSource ? [] : unit.locations, - description: removeContextSource ? undefined : unit.description, - meaning: removeContextSource ? undefined : unit.meaning + description: removeContextSource ? undefined : mapper(unit.description), + meaning: removeContextSource ? undefined : mapper(unit.meaning) })); if (sort === 'idAsc') { return updatedUnits.sort((a, b) => a.id.localeCompare(b.id)); @@ -157,8 +157,8 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext source: mapper(unit.source), target: unit.target !== undefined ? mapper(unit.target) : undefined, locations: options.includeContext === true ? unit.locations : [], - meaning: options.includeContext === true ? unit.meaning : undefined, - description: options.includeContext === true ? unit.description : undefined, + meaning: options.includeContext === true ? mapper(unit.meaning) : undefined, + description: options.includeContext === true ? mapper(unit.description) : undefined, // reset to original state, if source was changed to target from sourceLangTarget: state: idsOfUnitsWithSourceChangedToSourceLangTarget.has(unit.id) ? (translationTargetFile.units.find(u => u.id === unit.id)?.state ?? unit.state) : unit.state })); diff --git a/src/stringUtils.ts b/src/stringUtils.ts index 01d0da1..66301c8 100644 --- a/src/stringUtils.ts +++ b/src/stringUtils.ts @@ -1,5 +1,3 @@ - - -export function doCollapseWhitespace(destSourceText: string): string { - return destSourceText.replace(/\s+/g, ' '); +export function doCollapseWhitespace(destSourceText: T): T { + return destSourceText?.replace(/\s+/g, ' ') as T; }