fix(frontend): fixed incorrect logic for extracting arrays/objects in forms

pull/85/head
Samuel Rowe 3 years ago
parent a0e400fb4b
commit 73a137bae2

@ -69,25 +69,40 @@ export type TTransformFunction = (
index: number index: number
) => Record<string, any>; ) => Record<string, any>;
export const loadObjectOrArrayAsObject = ( export const extractObjectOrArray = (
transform: TTransformFunction, seperator: string,
object?: any keyName: string,
valueName: string,
object: any
): any => { ): any => {
if (!object) { if (!object) {
return undefined; return undefined;
} }
if (Array.isArray(object)) { if (Array.isArray(object)) {
return object.map(transform); return object.map((item: string) => {
const [key, value] = splitKVPairs(item, seperator);
return {
[keyName]: key,
[valueName]: value
};
});
} }
return object; return Object.entries(object).map(([key, value]) => {
return {
[keyName]: key,
[valueName]: value
};
});
}; };
export const loadArrayAsObject = ( export const extractArray = (
transform: TTransformFunction, seperator: string,
array?: string[] keyName: string,
) => { valueName: string,
array: any
): any => {
if (!array) { if (!array) {
return undefined; return undefined;
} }
@ -96,33 +111,11 @@ export const loadArrayAsObject = (
throw new Error("Malformed document. Expected an array at this point."); throw new Error("Malformed document. Expected an array at this point.");
} }
return array.map(transform); return array.map((item: string) => {
};
export const extractObjectOrArray = (
seperator: string,
keyName: string,
valueName: string,
object: any
): any =>
loadObjectOrArrayAsObject((item: string) => {
const [key, value] = splitKVPairs(item, seperator); const [key, value] = splitKVPairs(item, seperator);
return { return {
[keyName]: key, [keyName]: key,
[valueName]: value [valueName]: value
}; };
}, object); });
};
export const extractArray = (
seperator: string,
keyName: string,
valueName: string,
object: any
): any =>
loadArrayAsObject((item: string) => {
const [key, value] = splitKVPairs(item, seperator);
return {
[keyName]: key,
[valueName]: value
};
}, object);

Loading…
Cancel
Save