Skip to content

Commit

Permalink
feat: collect mapped/changed IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed May 5, 2022
1 parent 5a315a1 commit b191874
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ function getUnitAndDestUnit(inUnits: XmlElement[], removeNodes: XmlElement[], de
}
}

export function merge(inFileContent: string, destFileContent: string, options?: MergeOptions) {
export function merge(inFileContent: string, destFileContent: string, options?: MergeOptions): string {
const [mergedDestFileContent] = mergeWithMapping(inFileContent, destFileContent, options);
return mergedDestFileContent;
}

export function mergeWithMapping(inFileContent: string, destFileContent: string, options?: MergeOptions): [mergedDestFileContent: string, idMappging: { [oldId: string]: string }] {
const inDoc = new XmlDocument(inFileContent);
const destDoc = new XmlDocument(destFileContent);

Expand All @@ -120,6 +125,8 @@ export function merge(inFileContent: string, destFileContent: string, options?:
const originIds = new Set(inUnits.map(u => u.attr.id));
const removeNodes = getUnits(destDoc, xliffVersion)!.filter(destUnit => !originIds.has(destUnit.attr.id));

const idMapping: { [id: string]: string } = {};

// add missing units and update existing ones:
for (let [unit, destUnit] = getUnitAndDestUnit(inUnits, removeNodes, destUnitsParent, xliffVersion, options?.fuzzyMatch ?? true);
unit !== undefined;
Expand All @@ -141,6 +148,7 @@ export function merge(inFileContent: string, destFileContent: string, options?:
}
if (destUnit.attr.id !== unit.attr.id) {
console.debug(`matched unit with previous id "${destUnit.attr.id}" to new id: "${unit.attr.id}"`);
idMapping[destUnit.attr.id] = unit.attr.id;
removeNodes.splice(removeNodes.indexOf(destUnit), 1);
destUnit.attr.id = unit.attr.id;
resetTranslationState(destUnit, xliffVersion, options);
Expand Down Expand Up @@ -177,10 +185,11 @@ export function merge(inFileContent: string, destFileContent: string, options?:
const xmlDecMatch = destFileContent.match(/^<\?xml [^>]*>\s*/i);
const xmlDeclaration = xmlDecMatch ? xmlDecMatch[0] : '';

return xmlDeclaration + revertApostrophes(destDoc.toString({
const mergedContent = xmlDeclaration + revertApostrophes(destDoc.toString({
preserveWhitespace: true,
compressed: true
}), !options?.replaceApostrophe);
return [mergedContent, idMapping];
}

/**
Expand Down

0 comments on commit b191874

Please sign in to comment.