From 587c670e3e309200ea3cd026c5865f88c278863c Mon Sep 17 00:00:00 2001 From: PierreDemailly Date: Sun, 14 Jul 2024 09:08:10 +0200 Subject: [PATCH 1/2] fix(morphix): ignoreMissing not working as expected with nested data --- src/morphix/src/index.ts | 14 +++++++++++--- src/morphix/test/index.spec.mts | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/morphix/src/index.ts b/src/morphix/src/index.ts index 174d8ad..57e4deb 100644 --- a/src/morphix/src/index.ts +++ b/src/morphix/src/index.ts @@ -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]; + continue; + } + else if (value && value[property]) { + value = value[property]; + continue; + } + break; } const transformedValue = transform({ value, key }); diff --git a/src/morphix/test/index.spec.mts b/src/morphix/test/index.spec.mts index 8765281..ae4d06d 100644 --- a/src/morphix/test/index.spec.mts +++ b/src/morphix/test/index.spec.mts @@ -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}", {}); From e9e6d239e90d4706fed0b4bd2c626cb2b18ae0be Mon Sep 17 00:00:00 2001 From: PierreDemailly <39910767+PierreDemailly@users.noreply.github.com> Date: Sun, 14 Jul 2024 12:23:52 +0200 Subject: [PATCH 2/2] Update src/morphix/src/index.ts Co-authored-by: Thomas.G --- src/morphix/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/morphix/src/index.ts b/src/morphix/src/index.ts index 57e4deb..c04dd13 100644 --- a/src/morphix/src/index.ts +++ b/src/morphix/src/index.ts @@ -64,13 +64,13 @@ export async function morphix( for (const property of keys) { if (data[property] !== void 0) { value = data[property]; - continue; } else if (value && value[property]) { value = value[property]; - continue; } - break; + else { + break; + } } const transformedValue = transform({ value, key });