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