Optimizing generics file

This commit is contained in:
2023-11-26 13:35:40 +01:00
parent d717fc8410
commit c56d6b1688

View File

@ -1,15 +1,15 @@
/*
* Transforms object with arrays into array of objects
*
* @param {Record<string, any>} input - Object with arrays
* @param {string[]} ignoredKeys - Keys to ignore
* @returns {Record<string, any>[]} Array of objects
*
*/
export function objectWithArraysToArrayOfObjects( export function objectWithArraysToArrayOfObjects(
input: Record<string, any>, input: Record<string, any>,
arraysToArrays: string[], arraysToArrays: string[]
): Record<string, any>[] { ): Record<string, any>[] {
/*
* Transforms object with arrays into array of objects
*
* @param {Record<string, any>} input - Object with arrays
* @param {string[]} ignoredKeys - Keys to ignore
* @returns {Record<string, any>[]} Array of objects
*
*/
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,46 +42,43 @@ export function objectWithArraysToArrayOfObjects(
return output; return output;
} }
/*
* Transforms array of objects into object with arrays
*
* @param {Record<string, any>[]} array - Array of objects
* @param {Record<string, any>} mergeObject - Object that is merged to each object in the array
* @returns {object} Merged object with arrays
*
*/
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>( export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
array: T[], array: T[],
mergeObject: Record<string, any> = {}, mergeObject: Record<string, any> = {}
): Record<string, any> { ): Record<string, any> {
return array.reduce( /*
(acc, obj) => { * Transforms array of objects into object with arrays
const mergedObj = { ...obj, ...mergeObject }; *
Object.keys(mergedObj).forEach((key) => { * @param {Record<string, any>[]} array - Array of objects
if (!acc[key]) { * @param {Record<string, any>} mergeObject - Object that is merged to each object in the array
acc[key] = []; * @returns {object} Merged object with arrays
} *
acc[key].push(mergedObj[key]); */
}); return array.reduce((acc, obj) => {
return acc; const mergedObj = { ...obj, ...mergeObject };
}, Object.keys(mergedObj).forEach((key) => {
{} as Record<string, any>, if (!acc[key]) {
); acc[key] = [];
}
acc[key].push(mergedObj[key]);
});
return acc;
}, {} as Record<string, any>);
} }
/*
* Filter certain keys from object
*
* @param {Record<string, any>} obj - Object to filter
* @param {string[]} filter - Keys to filter
* @returns {object} Filtered object
*
*/
export function filterObject( export function filterObject(
obj: Record<string, any>, obj: Record<string, any>,
filter: string[], filter: string[]
): Record<string, any> { ): Record<string, any> {
/*
* Filter certain keys from object
*
* @param {Record<string, any>} obj - Object to filter
* @param {string[]} filter - Keys to filter
* @returns {object} Filtered object
*
*/
return Object.fromEntries( return Object.fromEntries(
Object.entries(obj).filter(([key]) => filter.includes(key)), Object.entries(obj).filter(([key]) => filter.includes(key))
); );
} }