Skip to content

Commit 1255cbd

Browse files
authored
Merge pull request #34 from JS-AK/feat/template/updates
fix: added check hasArraysInReplacements in substitute
2 parents 376cf30 + f1c672e commit 1255cbd

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

src/lib/template/template-memory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ export class TemplateMemory {
256256
const TABLE_REGEX = /\$\{table:([a-zA-Z0-9_]+)\.([a-zA-Z0-9_]+)\}/g;
257257

258258
const hasTablePlaceholders = TABLE_REGEX.test(sharedStringsContent) || TABLE_REGEX.test(sheetContent);
259+
const hasArraysInReplacements = Utils.foundArraysInReplacements(replacements);
259260

260-
if (hasTablePlaceholders) {
261+
if (hasTablePlaceholders && hasArraysInReplacements) {
261262
const result = this.#expandTableRows(sheetContent, sharedStringsContent, replacements);
262263

263264
sheetContent = result.sheet;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Recursively checks if an object contains any arrays in its values.
3+
*
4+
* @param {Record<string, unknown>} replacements - The object to check for arrays.
5+
* @returns {boolean} True if any arrays are found, false otherwise.
6+
*/
7+
8+
export function foundArraysInReplacements(replacements: Record<string, unknown>): boolean {
9+
let isFound = false;
10+
11+
for (const key in replacements) {
12+
const value = replacements[key];
13+
14+
if (Array.isArray(value)) {
15+
isFound = true;
16+
17+
return isFound;
18+
}
19+
20+
if (typeof value === "object" && value !== null) {
21+
isFound = foundArraysInReplacements(value as Record<string, unknown>);
22+
23+
if (isFound) {
24+
return isFound;
25+
}
26+
}
27+
}
28+
29+
return isFound;
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { foundArraysInReplacements } from "./found-arrays-in-replacements.js";
4+
5+
describe("foundArraysInReplacements", () => {
6+
it("should return true if the replacements object contains arrays at the top level", () => {
7+
const replacements = { key: ["value1", "value2"] };
8+
9+
const result = foundArraysInReplacements(replacements);
10+
11+
expect(result).toBe(true);
12+
});
13+
14+
it("should return true if the replacements object contains arrays at the second level", () => {
15+
const replacements = { key: { subkey: ["value1", "value2"] } };
16+
17+
const result = foundArraysInReplacements(replacements);
18+
19+
expect(result).toBe(true);
20+
});
21+
22+
it("should return false if the replacements object does not contain arrays", () => {
23+
const replacements = { key: "value" };
24+
25+
const result = foundArraysInReplacements(replacements);
26+
27+
expect(result).toBe(false);
28+
});
29+
});

src/lib/template/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./column-index-to-letter.js";
88
export * from "./compare-columns.js";
99
export * from "./escape-xml.js";
1010
export * from "./extract-xml-declaration.js";
11+
export * from "./found-arrays-in-replacements.js";
1112
export * from "./get-by-path.js";
1213
export * from "./get-max-row-number.js";
1314
export * from "./get-rows-above.js";

0 commit comments

Comments
 (0)