From 811568ee9aa7da6ae7168a3db535054e7cf317cb Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Fri, 8 Jan 2021 16:48:40 -0600 Subject: [PATCH] fix(core): adjust getFlatteningSourcePath to take into account multiple parts path with naming conventions --- .../utils/get-flattening-source-paths.util.ts | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/core/src/lib/utils/get-flattening-source-paths.util.ts b/packages/core/src/lib/utils/get-flattening-source-paths.util.ts index 4e3f5823a..32e75715b 100644 --- a/packages/core/src/lib/utils/get-flattening-source-paths.util.ts +++ b/packages/core/src/lib/utils/get-flattening-source-paths.util.ts @@ -6,30 +6,49 @@ export function getFlatteningSourcePaths( namingConventions: [NamingConvention, NamingConvention] ) { const [sourceNamingConvention] = namingConventions; - const [first, ...paths] = srcPath + const splitSourcePaths = srcPath .split(sourceNamingConvention.splittingExpression) .filter(Boolean) .filter((p) => p !== '.'); - if (!paths.length || !src.hasOwnProperty(first)) { + const lengthWithoutFirstPart = splitSourcePaths.length - 1; + + if (!lengthWithoutFirstPart) { + return; + } + + const [first, ...paths] = splitSourcePaths.slice(0, lengthWithoutFirstPart); + let trueFirstPartOfSource = first; + let stopIndex = 0; + let found = false; + + if (src.hasOwnProperty(trueFirstPartOfSource)) { + found = true; + } else { + for (let i = 0, len = paths.length; i < len; i++) { + trueFirstPartOfSource = sourceNamingConvention.transformPropertyName([ + trueFirstPartOfSource, + paths[i], + ]); + if (src.hasOwnProperty(trueFirstPartOfSource)) { + stopIndex = i + 1; + found = true; + break; + } + } + } + + if (!found) { return; } - const sourcePaths = [ - [first] + return [ + [trueFirstPartOfSource] .concat( - paths.map((p) => sourceNamingConvention.transformPropertyName([p])) + sourceNamingConvention.transformPropertyName( + splitSourcePaths.slice(stopIndex + 1, splitSourcePaths.length + 1) + ) ) .join('.'), ]; - - if (paths.length > 1) { - sourcePaths.push( - [first] - .concat(sourceNamingConvention.transformPropertyName(paths)) - .join('.') - ); - } - - return sourcePaths; }