Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
35 lines
1.4 KiB
Plaintext
35 lines
1.4 KiB
Plaintext
import { reduceFieldsToValues } from './reduceFieldsToValues.js';
|
|
import { unflatten } from './unflatten.js';
|
|
export const getSiblingData = (fields, path)=>{
|
|
if (!fields) {
|
|
return null;
|
|
}
|
|
if (path.indexOf('.') === -1) {
|
|
return reduceFieldsToValues(fields, true);
|
|
}
|
|
const siblingFields = {};
|
|
// Determine if the last segment of the path is an array-based row
|
|
const pathSegments = path.split('.');
|
|
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
const lastSegmentIsRowIndex = !Number.isNaN(Number(lastSegment));
|
|
let parentFieldPath;
|
|
if (lastSegmentIsRowIndex) {
|
|
// If the last segment is a row index,
|
|
// the sibling data is that row's contents
|
|
// so create a parent field path that will
|
|
// retrieve all contents of that row index only
|
|
parentFieldPath = `${path}.`;
|
|
} else {
|
|
// Otherwise, the last path segment is a field name
|
|
// and it should be removed
|
|
parentFieldPath = path.substring(0, path.lastIndexOf('.') + 1);
|
|
}
|
|
Object.keys(fields).forEach((fieldKey)=>{
|
|
if (!fields[fieldKey]?.disableFormData && fieldKey.indexOf(parentFieldPath) === 0) {
|
|
siblingFields[fieldKey.replace(parentFieldPath, '')] = fields[fieldKey]?.value;
|
|
}
|
|
});
|
|
return unflatten(siblingFields);
|
|
};
|
|
|
|
//# sourceMappingURL=getSiblingData.js.map |