Skip to content

Commit

Permalink
fix(core): adjust getFlatteningSourcePath to take into account multip…
Browse files Browse the repository at this point in the history
…le parts path with naming conventions
  • Loading branch information
nartc committed Jan 8, 2021
1 parent 4197eda commit 811568e
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions packages/core/src/lib/utils/get-flattening-source-paths.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 811568e

Please sign in to comment.