Skip to content

Commit

Permalink
fix(morphix): ignoreMissing not working as expected with nested data (#…
Browse files Browse the repository at this point in the history
…283)

---------

Co-authored-by: Thomas.G <[email protected]>
  • Loading branch information
PierreDemailly and fraxken authored Jul 14, 2024
1 parent 78e25f8 commit 474929c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/morphix/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ export async function morphix(

const replace = async(placeholder: string, key: string, func: string | undefined) => {
let value: string | undefined = undefined;
for (const property of key?.split(".")) {
// eslint-disable-next-line no-nested-ternary
value = value ? value[property] : data ? data[property] : undefined;
const keys = key?.split(".") ?? [];
for (const property of keys) {
if (data[property] !== void 0) {
value = data[property];
}
else if (value && value[property]) {
value = value[property];
}
else {
break;
}
}

const transformedValue = transform({ value, key });
Expand Down
7 changes: 7 additions & 0 deletions src/morphix/test/index.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe("Morphix", () => {
assert.equal(await morphix(template, {}, options), template);
});

it("should ignore missing (nested)", async() => {
assert.equal(
await morphix("FOO {bar.foo} BAR", { foo: "!" }, { ignoreMissing: true }),
"FOO {bar.foo} BAR"
);
});

it("throw on undefined by default", async() => {
assert.rejects(async() => {
await morphix("{foo}", {});
Expand Down

0 comments on commit 474929c

Please sign in to comment.