Skip to content

Commit

Permalink
chore: remove async from startVisiting
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Sep 27, 2024
1 parent c6fcdf0 commit 3d178c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/docx/doc-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function rebuildBodyElements(items: BodyElement[]): any[] {
}

// @internal
export async function collectBodyElements(body: any): Promise<BodyElement[]> {
export function collectBodyElements(body: any): BodyElement[] {
// collect all the elements inside this body
const bodyChildren = Array.isArray(body) ? body : [body];
const items: BodyElement[] = [];
Expand All @@ -46,7 +46,7 @@ export async function collectBodyElements(body: any): Promise<BodyElement[]> {
// this is a paragraph
let curItemText: { path: string[]; text: string } | undefined;

await startVisiting(item, {
startVisiting(item, {
before: {
"w:t": [
(children, path) => {
Expand Down
14 changes: 7 additions & 7 deletions src/docx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function docxFillTemplate(
const parser = new XMLParser(options);
const doc = parser.parse(data);

const result = await templateDocument(doc, input);
const result = templateDocument(doc, input);

const builder = new XMLBuilder(options);
const newDoc: string = builder.build(result);
Expand All @@ -74,10 +74,10 @@ export async function docxFillTemplate(
zipWriter.close();
}

async function getBody(xml: any): Promise<any | undefined> {
function getBody(xml: any): any | undefined {
let bodyContent;

await startVisiting(xml, {
startVisiting(xml, {
before: {
"w:body": [(children) => (bodyContent = children)],
},
Expand All @@ -87,19 +87,19 @@ async function getBody(xml: any): Promise<any | undefined> {
return bodyContent;
}

async function templateDocument(xml: any, input: any): Promise<Result<any, DocAddr>> {
const body = await getBody(xml);
function templateDocument(xml: any, input: any): Result<any, DocAddr> {
const body = getBody(xml);
if (!body) return xml;

const items = await collectBodyElements(body);
const items = collectBodyElements(body);
const templatedItems = performDocumentTemplating(items, input);

if (templatedItems.status === "failed") return templatedItems;

const newBodyItems = rebuildBodyElements(templatedItems.result);

return success(
await startVisiting(xml, {
startVisiting(xml, {
before: {},
after: {
"w:body": [() => ({ newObj: newBodyItems })],
Expand Down
2 changes: 1 addition & 1 deletion src/visitor-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type Visitors = {
};

// @internal
export async function startVisiting(
export function startVisiting(
doc: any,
visitors: Visitors,
): Promise<any> {
Expand Down
46 changes: 22 additions & 24 deletions src/xlsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function xlsxFillTemplate(
continue;
}

const result = await templater.template(
const result = templater.template(
await streamToText(contentStream.readable),
input,
);
Expand Down Expand Up @@ -170,15 +170,13 @@ class XlsxTemplater {
console.log(this.sharedStrings);
}

async template(
template(
templateContent: string,
data: any,
): Promise<
{ xml: string; issues: Issue<SheetAddr>[] } & (
| { error: true; cause: Issue<SheetAddr> }
| { error: false }
)
> {
): { xml: string; issues: Issue<SheetAddr>[] } & (
| { error: true; cause: Issue<SheetAddr> }
| { error: false }
) {
const options: X2jOptions & XmlBuilderOptions = {
ignoreAttributes: false,
parseTagValue: false,
Expand All @@ -193,11 +191,11 @@ class XlsxTemplater {
const parsed = parser.parse(templateContent);

console.log(JSON.stringify(parsed));
const sheetFilled = await this.fillSheetWithSharedStrings(parsed);
const sheetFilled = this.fillSheetWithSharedStrings(parsed);
console.log(JSON.stringify(sheetFilled));

// then we turn this sheet into a Sheet object so we can work with it easier
const extractedData = await this.extract(sheetFilled);
const extractedData = this.extract(sheetFilled);

const rowInfo = extractedData.rowInfo;
const colInfo = extractedData.colInfo;
Expand Down Expand Up @@ -339,7 +337,7 @@ class XlsxTemplater {
rows[r] = { ...rowInfo[r], c: row.length === 1 ? row[0] : row };
}

await startVisiting(xlsxData, {
startVisiting(xlsxData, {
before: {},
after: {
dimension: [
Expand Down Expand Up @@ -401,15 +399,15 @@ class XlsxTemplater {
return xlsxData;
}

async extract(parsedSheet: any): Promise<{
extract(parsedSheet: any): {
sheet: Sheet<XlsxCell>;
rowInfo: Record<number, any>;
colInfo: (any & { min: number; max: number })[];
mergeInfo: {
start: { col: number; row: number };
end: { col: number; row: number };
}[];
}> {
} {
const sheet = new Sheet<XlsxCell>();
const rowInfo: Record<number, any> = {};
const colInfo: (any & { min: number; max: number })[] = [];
Expand All @@ -418,7 +416,7 @@ class XlsxTemplater {
end: { col: number; row: number };
}[] = [];

await startVisiting(parsedSheet, {
startVisiting(parsedSheet, {
before: {
mergeCell: [
(obj) => {
Expand Down Expand Up @@ -498,8 +496,8 @@ class XlsxTemplater {
};
}

async fillSheetWithSharedStrings(parsedSheet: any): Promise<any> {
return await startVisiting(parsedSheet, {
fillSheetWithSharedStrings(parsedSheet: any): Promise<any> {
return startVisiting(parsedSheet, {
before: {},
after: {
c: [
Expand All @@ -510,10 +508,10 @@ class XlsxTemplater {
newObj:
obj["@_t"] === "s" && isNumeric(obj["v"])
? {
...obj,
v: this.sharedStrings[parseInt(obj["v"])],
"@_t": "str",
}
...obj,
v: this.sharedStrings[parseInt(obj["v"])],
"@_t": "str",
}
: { ...obj },
};
}
Expand All @@ -524,10 +522,10 @@ class XlsxTemplater {
newObj: obj.map((o) =>
o["@_t"] === "s" && isNumeric(o["v"])
? {
...o,
v: this.sharedStrings[parseInt(o["v"])],
"@_t": "str",
}
...o,
v: this.sharedStrings[parseInt(o["v"])],
"@_t": "str",
}
: { ...o },
),
};
Expand Down

0 comments on commit 3d178c2

Please sign in to comment.