refactor: switched to a reduce, and now returning the value as well as the path array
Signed-off-by: Mark David Avery <mark@webark.cc>
This commit is contained in:
@@ -20,22 +20,20 @@ import lodash from 'lodash';
|
||||
import { RecursivePartial } from '../../util/RecursivePartial';
|
||||
import { parseStringsParam } from './common';
|
||||
|
||||
function getPathArray(input: Entity, field: string) {
|
||||
const pathArray = [];
|
||||
let currentPathPart = '';
|
||||
function getPathArrayAndValue(input: Entity, field: string) {
|
||||
return field.split('.').reduce(
|
||||
([pathArray, inputSubset], pathPart, index, fieldParts) => {
|
||||
if (Object.hasOwn(inputSubset, pathPart)) {
|
||||
return [pathArray.concat(pathPart), inputSubset[pathPart]];
|
||||
} else if (fieldParts[index + 1] !== undefined) {
|
||||
fieldParts[index + 1] = `${pathPart}.${fieldParts[index + 1]}`;
|
||||
return [pathArray, inputSubset];
|
||||
}
|
||||
|
||||
for (const pathPart of field.split('.')) {
|
||||
currentPathPart += pathPart;
|
||||
|
||||
if (lodash.has(input, pathArray.concat(currentPathPart))) {
|
||||
pathArray.push(currentPathPart);
|
||||
currentPathPart = '';
|
||||
} else {
|
||||
currentPathPart += '.';
|
||||
}
|
||||
}
|
||||
|
||||
return pathArray;
|
||||
return [pathArray, undefined];
|
||||
},
|
||||
[[] as unknown as string, input as any],
|
||||
);
|
||||
}
|
||||
|
||||
export function parseEntityTransformParams(
|
||||
@@ -64,9 +62,8 @@ export function parseEntityTransformParams(
|
||||
const output: RecursivePartial<Entity> = {};
|
||||
|
||||
for (const field of fields) {
|
||||
const pathArray = getPathArray(input, field);
|
||||
const [pathArray, value] = getPathArrayAndValue(input, field);
|
||||
|
||||
const value = lodash.get(input, pathArray);
|
||||
if (value !== undefined) {
|
||||
lodash.set(output, pathArray, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user