From c67f9abadc3c6824d9acbe4edf2f90932e73344b Mon Sep 17 00:00:00 2001 From: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:56:25 +0200 Subject: [PATCH] optimize_nodesToString --- src/TransWithoutContext.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/TransWithoutContext.js b/src/TransWithoutContext.js index 37b51084c..9260f315d 100644 --- a/src/TransWithoutContext.js +++ b/src/TransWithoutContext.js @@ -47,19 +47,22 @@ export const nodesToString = (children, i18nOptions) => { // expected e.g. lorem stringNode += `${child}`; } else if (isValidElement(child)) { - const childPropsCount = Object.keys(child.props).length; - const shouldKeepChild = keepArray.indexOf(child.type) > -1; - const childChildren = child.props.children; + const { props, type } = child; + const childPropsCount = Object.keys(props).length; + const shouldKeepChild = keepArray.indexOf(type) > -1; + const childChildren = props.children; - if (!childChildren && shouldKeepChild && childPropsCount === 0) { + if (!childChildren && shouldKeepChild && !childPropsCount) { // actual e.g. lorem
ipsum // expected e.g. lorem
ipsum - stringNode += `<${child.type}/>`; - } else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) { + stringNode += `<${type}/>`; + } else if ( + (!childChildren && (!shouldKeepChild || childPropsCount)) || + props.i18nIsDynamicList + ) { // actual e.g. lorem
ipsum // expected e.g. lorem <0> ipsum - stringNode += `<${childIndex}>`; - } else if (child.props.i18nIsDynamicList) { + // or // we got a dynamic list like // e.g. // expected e.g. "<0>", not e.g. "<0><0>a<1>b" @@ -67,7 +70,7 @@ export const nodesToString = (children, i18nOptions) => { } else if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) { // actual e.g. dolor bold amet // expected e.g. dolor bold amet - stringNode += `<${child.type}>${childChildren}`; + stringNode += `<${type}>${childChildren}`; } else { // regular case mapping the inner children const content = nodesToString(childChildren, i18nOptions);