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
|
* Transforms object with arrays into array of objects
|
||||||
*
|
*
|
||||||
* @param {Record<string, any>} input - Object with arrays
|
* @param {Record<string, any>} input - Object with arrays
|
||||||
@ -6,10 +10,6 @@
|
|||||||
* @returns {Record<string, any>[]} Array of objects
|
* @returns {Record<string, any>[]} Array of objects
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function objectWithArraysToArrayOfObjects(
|
|
||||||
input: Record<string, any>,
|
|
||||||
arraysToArrays: string[],
|
|
||||||
): Record<string, any>[] {
|
|
||||||
const inputCopy = { ...input };
|
const inputCopy = { ...input };
|
||||||
arraysToArrays.forEach((k) => {
|
arraysToArrays.forEach((k) => {
|
||||||
if (Array.isArray(inputCopy[k]) && !Array.isArray(inputCopy[k][0])) {
|
if (Array.isArray(inputCopy[k]) && !Array.isArray(inputCopy[k][0])) {
|
||||||
@ -24,7 +24,7 @@ export function objectWithArraysToArrayOfObjects(
|
|||||||
acc.keys.push(key);
|
acc.keys.push(key);
|
||||||
return acc;
|
return acc;
|
||||||
},
|
},
|
||||||
{ keys: [] as string[], maxLength: 0 },
|
{ keys: [] as string[], maxLength: 0 }
|
||||||
);
|
);
|
||||||
|
|
||||||
const output: Record<string, any>[] = [];
|
const output: Record<string, any>[] = [];
|
||||||
@ -42,7 +42,11 @@ export function objectWithArraysToArrayOfObjects(
|
|||||||
return output;
|
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
|
* Transforms array of objects into object with arrays
|
||||||
*
|
*
|
||||||
* @param {Record<string, any>[]} array - Array of objects
|
* @param {Record<string, any>[]} array - Array of objects
|
||||||
@ -50,12 +54,7 @@ export function objectWithArraysToArrayOfObjects(
|
|||||||
* @returns {object} Merged object with arrays
|
* @returns {object} Merged object with arrays
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
return array.reduce((acc, obj) => {
|
||||||
array: T[],
|
|
||||||
mergeObject: Record<string, any> = {},
|
|
||||||
): Record<string, any> {
|
|
||||||
return array.reduce(
|
|
||||||
(acc, obj) => {
|
|
||||||
const mergedObj = { ...obj, ...mergeObject };
|
const mergedObj = { ...obj, ...mergeObject };
|
||||||
Object.keys(mergedObj).forEach((key) => {
|
Object.keys(mergedObj).forEach((key) => {
|
||||||
if (!acc[key]) {
|
if (!acc[key]) {
|
||||||
@ -64,12 +63,14 @@ export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
|||||||
acc[key].push(mergedObj[key]);
|
acc[key].push(mergedObj[key]);
|
||||||
});
|
});
|
||||||
return acc;
|
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
|
* Filter certain keys from object
|
||||||
*
|
*
|
||||||
* @param {Record<string, any>} obj - Object to filter
|
* @param {Record<string, any>} obj - Object to filter
|
||||||
@ -77,11 +78,7 @@ export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
|||||||
* @returns {object} Filtered object
|
* @returns {object} Filtered object
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function filterObject(
|
|
||||||
obj: Record<string, any>,
|
|
||||||
filter: string[],
|
|
||||||
): Record<string, any> {
|
|
||||||
return Object.fromEntries(
|
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