Skip to content

Commit

Permalink
fix: refactor processElement to not exceed arguments limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Apr 8, 2024
1 parent e2d6124 commit db2bd59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ export const JSON_PARSER_OPTION = {
export interface XmlElement {
[key: string]: string | XmlElement | string[] | XmlElement[];
}

export interface ProcessElementParams {
element: XmlElement;
metadataPath: string;
uniqueIdElements: string | undefined;
rootElementName: string;
rootElementHeader: string;
key: string;
indent: string;
leafContent: string;
leafCount: number;
hasNestedElements: boolean;
}
10 changes: 5 additions & 5 deletions src/service/buildDisassembledFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function buildDisassembledFiles(
if (Array.isArray(rootElement[key])) {
for (const element of rootElement[key] as XmlElement[]) {
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement(
await processElement({
element,
metadataPath,
uniqueIdElements,
Expand All @@ -58,15 +58,15 @@ export async function buildDisassembledFiles(
leafContent,
leafCount,
hasNestedElements,
);
});
leafContent = updatedLeafContent;
leafCount = updatedLeafCount;
hasNestedElements = updatedHasNestedElements;
}
} else {
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] =
await processElement(
rootElement[key] as XmlElement,
await processElement({
element: rootElement[key] as XmlElement,
metadataPath,
uniqueIdElements,
rootElementName,
Expand All @@ -76,7 +76,7 @@ export async function buildDisassembledFiles(
leafContent,
leafCount,
hasNestedElements,
);
});
leafContent = updatedLeafContent;
leafCount = updatedLeafCount;
hasNestedElements = updatedHasNestedElements;
Expand Down
33 changes: 18 additions & 15 deletions src/service/processElement.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
"use strict";

import { XmlElement } from "@src/helpers/types";
import { buildNestedFile } from "@src/service/buildNestedFiles";
import { ProcessElementParams } from "@src/helpers/types";

export async function processElement(
element: XmlElement,
metadataPath: string,
uniqueIdElements: string | undefined,
rootElementName: string,
rootElementHeader: string,
key: string,
indent: string,
leafContent: string,
leafCount: number,
hasNestedElements: boolean,
params: ProcessElementParams,
): Promise<[string, number, boolean]> {
const {
element,
metadataPath,
uniqueIdElements,
rootElementName,
rootElementHeader,
key,
indent,
leafContent,
leafCount,
hasNestedElements,
} = params;

if (typeof element === "object") {
await buildNestedFile(
element,
Expand All @@ -25,11 +29,10 @@ export async function processElement(
key,
indent,
);
hasNestedElements = true;
return [leafContent, leafCount, true];
} else {
const fieldValue = element;
leafContent += `${indent}<${key}>${String(fieldValue)}</${key}>\n`;
leafCount++;
const updatedLeafContent = `${leafContent}${indent}<${key}>${String(fieldValue)}</${key}>\n`;
return [updatedLeafContent, leafCount + 1, hasNestedElements];
}
return [leafContent, leafCount, hasNestedElements];
}

0 comments on commit db2bd59

Please sign in to comment.