Optimizing generics file
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
/*
|
||||
export function objectWithArraysToArrayOfObjects(
|
||||
input: Record<string, any>,
|
||||
arraysToArrays: string[]
|
||||
): Record<string, any>[] {
|
||||
/*
|
||||
* Transforms object with arrays into array of objects
|
||||
*
|
||||
* @param {Record<string, any>} input - Object with arrays
|
||||
@ -6,10 +10,6 @@
|
||||
* @returns {Record<string, any>[]} Array of objects
|
||||
*
|
||||
*/
|
||||
export function objectWithArraysToArrayOfObjects(
|
||||
input: Record<string, any>,
|
||||
arraysToArrays: string[],
|
||||
): Record<string, any>[] {
|
||||
const inputCopy = { ...input };
|
||||
arraysToArrays.forEach((k) => {
|
||||
if (Array.isArray(inputCopy[k]) && !Array.isArray(inputCopy[k][0])) {
|
||||
@ -24,7 +24,7 @@ export function objectWithArraysToArrayOfObjects(
|
||||
acc.keys.push(key);
|
||||
return acc;
|
||||
},
|
||||
{ keys: [] as string[], maxLength: 0 },
|
||||
{ keys: [] as string[], maxLength: 0 }
|
||||
);
|
||||
|
||||
const output: Record<string, any>[] = [];
|
||||
@ -42,7 +42,11 @@ export function objectWithArraysToArrayOfObjects(
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
||||
array: T[],
|
||||
mergeObject: Record<string, any> = {}
|
||||
): Record<string, any> {
|
||||
/*
|
||||
* Transforms array of objects into object with arrays
|
||||
*
|
||||
* @param {Record<string, any>[]} array - Array of objects
|
||||
@ -50,12 +54,7 @@ export function objectWithArraysToArrayOfObjects(
|
||||
* @returns {object} Merged object with arrays
|
||||
*
|
||||
*/
|
||||
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
||||
array: T[],
|
||||
mergeObject: Record<string, any> = {},
|
||||
): Record<string, any> {
|
||||
return array.reduce(
|
||||
(acc, obj) => {
|
||||
return array.reduce((acc, obj) => {
|
||||
const mergedObj = { ...obj, ...mergeObject };
|
||||
Object.keys(mergedObj).forEach((key) => {
|
||||
if (!acc[key]) {
|
||||
@ -64,12 +63,14 @@ export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
||||
acc[key].push(mergedObj[key]);
|
||||
});
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, any>,
|
||||
);
|
||||
}, {} as Record<string, any>);
|
||||
}
|
||||
|
||||
/*
|
||||
export function filterObject(
|
||||
obj: Record<string, any>,
|
||||
filter: string[]
|
||||
): Record<string, any> {
|
||||
/*
|
||||
* Filter certain keys from object
|
||||
*
|
||||
* @param {Record<string, any>} obj - Object to filter
|
||||
@ -77,11 +78,7 @@ export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
||||
* @returns {object} Filtered object
|
||||
*
|
||||
*/
|
||||
export function filterObject(
|
||||
obj: Record<string, any>,
|
||||
filter: string[],
|
||||
): Record<string, any> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(obj).filter(([key]) => filter.includes(key)),
|
||||
Object.entries(obj).filter(([key]) => filter.includes(key))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user