diff --git a/src/morphix/src/index.ts b/src/morphix/src/index.ts index 174d8ad..c04dd13 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]; + } + else if (value && value[property]) { + value = value[property]; + } + else { + 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}", {});