25 lines
674 B
TypeScript
25 lines
674 B
TypeScript
import { unflatten } from 'flatley';
|
|
import reduceFieldsToValues from './reduceFieldsToValues';
|
|
|
|
const getDataByPath = (fields, path) => {
|
|
const pathPrefixToRemove = path.substring(0, path.lastIndexOf('.') + 1);
|
|
const name = path.split('.').pop();
|
|
|
|
const data = Object.keys(fields).reduce((matchedData, key) => {
|
|
if (key.indexOf(`${path}.`) === 0) {
|
|
return {
|
|
...matchedData,
|
|
[key.replace(pathPrefixToRemove, '')]: fields[key],
|
|
};
|
|
}
|
|
|
|
return matchedData;
|
|
}, {});
|
|
|
|
const values = reduceFieldsToValues(data, true);
|
|
const unflattenedData = unflatten(values);
|
|
return unflattenedData?.[name];
|
|
};
|
|
|
|
export default getDataByPath;
|