Trying to optimize generics
This commit is contained in:
@ -6,36 +6,41 @@
|
|||||||
* @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>[] {
|
export function objectWithArraysToArrayOfObjects(
|
||||||
arraysToArrays.forEach((k) => {
|
input: Record<string, any>,
|
||||||
// Transform single array to array of arrays and keep array of arrays as is
|
arraysToArrays: string[]
|
||||||
if (Array.isArray(input[k]) && !Array.isArray(input[k][0])) {
|
): Record<string, any>[] {
|
||||||
input[k] = [input[k]];
|
const inputCopy = { ...input };
|
||||||
}
|
arraysToArrays.forEach((k) => {
|
||||||
});
|
if (Array.isArray(inputCopy[k]) && !Array.isArray(inputCopy[k][0])) {
|
||||||
const keys = Object.keys(input);
|
inputCopy[k] = [inputCopy[k]];
|
||||||
|
|
||||||
const maxLength = Math.max(
|
|
||||||
...keys.map((k) =>
|
|
||||||
Array.isArray(input[k]) ? (input[k] as any[]).length : 1
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const output: Record<string, any>[] = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < maxLength; i++) {
|
|
||||||
const event: Record<string, any> = {};
|
|
||||||
for (const k of keys) {
|
|
||||||
if (Array.isArray(input[k])) {
|
|
||||||
event[k] = (input[k] as any[])[i % (input[k] as any[]).length];
|
|
||||||
} else {
|
|
||||||
event[k] = input[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output.push(event);
|
|
||||||
}
|
}
|
||||||
return output;
|
});
|
||||||
};
|
|
||||||
|
const keysAndLengths = Object.entries(inputCopy).reduce(
|
||||||
|
(acc, [key, value]) => {
|
||||||
|
const length = Array.isArray(value) ? (value as any[]).length : 1;
|
||||||
|
acc.maxLength = Math.max(acc.maxLength, length);
|
||||||
|
acc.keys.push(key);
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{ keys: [] as string[], maxLength: 0 }
|
||||||
|
);
|
||||||
|
|
||||||
|
const output: Record<string, any>[] = [];
|
||||||
|
for (let i = 0; i < keysAndLengths.maxLength; i++) {
|
||||||
|
const event: Record<string, any> = {};
|
||||||
|
for (const k of keysAndLengths.keys) {
|
||||||
|
if (Array.isArray(inputCopy[k])) {
|
||||||
|
event[k] = (inputCopy[k] as any[])[i % (inputCopy[k] as any[]).length];
|
||||||
|
} else {
|
||||||
|
event[k] = inputCopy[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.push(event);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Transforms array of objects into object with arrays
|
* Transforms array of objects into object with arrays
|
||||||
@ -45,29 +50,35 @@ export function objectWithArraysToArrayOfObjects(input: Record<string, any>, arr
|
|||||||
* @returns {object} Merged object with arrays
|
* @returns {object} Merged object with arrays
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(array: T[], mergeObject: Record<string, any> = {}): Record<string, any> {
|
export function arrayOfObjectsToObjectWithArrays<T extends Record<string, any>>(
|
||||||
return array.reduce((acc, obj) => {
|
array: T[],
|
||||||
Object.keys(mergeObject).forEach((key) => {
|
mergeObject: Record<string, any> = {}
|
||||||
obj[key as keyof T] = mergeObject[key];
|
): Record<string, any> {
|
||||||
});
|
return array.reduce((acc, obj) => {
|
||||||
Object.keys(obj).forEach((key) => {
|
const mergedObj = { ...obj, ...mergeObject };
|
||||||
if (!acc[key as keyof T]) {
|
Object.keys(mergedObj).forEach((key) => {
|
||||||
acc[key as keyof T] = [];
|
if (!acc[key]) {
|
||||||
}
|
acc[key] = [];
|
||||||
(acc[key as keyof T] as unknown[]).push(obj[key]);
|
}
|
||||||
});
|
acc[key].push(mergedObj[key]);
|
||||||
return acc;
|
});
|
||||||
}, {} as Record<keyof T, any[]>);
|
return acc;
|
||||||
}
|
}, {} as 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
|
||||||
* @param {string[]} filter - Keys to filter
|
* @param {string[]} filter - Keys to filter
|
||||||
* @returns {object} Filtered object
|
* @returns {object} Filtered object
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function filterObject(obj: Record<string, any>, filter: string[]): Record<string, any> {
|
export function filterObject(
|
||||||
return Object.fromEntries(Object.entries(obj).filter(([key]) => filter.includes(key)));
|
obj: Record<string, any>,
|
||||||
}
|
filter: string[]
|
||||||
|
): Record<string, any> {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(obj).filter(([key]) => filter.includes(key))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user