Simplify patcher

Use map notation
This commit is contained in:
Dolan Miu
2023-02-25 22:18:31 +00:00
parent 4e875b4744
commit ce485dbc29
6 changed files with 52 additions and 268 deletions

View File

@ -26,12 +26,10 @@ type FilePatch = {
readonly children: readonly FileChild[];
};
export type IPatch = {
readonly text: string;
} & (ParagraphPatch | FilePatch);
export type IPatch = ParagraphPatch | FilePatch;
export interface PatchDocumentOptions {
readonly patches: readonly IPatch[];
readonly patches: { readonly [key: string]: IPatch };
}
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
@ -42,9 +40,10 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
for (const [key, value] of Object.entries(zipContent.files)) {
const json = toJson(await value.async("text"));
if (key.startsWith("word/")) {
for (const patch of options.patches) {
const renderedParagraphs = findLocationOfText(json, patch.text);
replacer(json, patch, renderedParagraphs);
for (const [patchKey, patchValue] of Object.entries(options.patches)) {
const patchText = `{{${patchKey}}}`;
const renderedParagraphs = findLocationOfText(json, patchText);
replacer(json, patchValue, patchText, renderedParagraphs);
}
}