( arr: ArrType, )
| 39 | } |
| 40 | |
| 41 | export function deduplicateArray<ArrType extends any[]>( |
| 42 | arr: ArrType, |
| 43 | ): Partial<ArrType | { items: Partial<ArrType>[] }> { |
| 44 | if (arr.length === 0) return {}; |
| 45 | // Check for null, undefined, string, number, boolean or array |
| 46 | if ( |
| 47 | arr.some((ele) => !ele || typeof ele !== "object" || Array.isArray(ele)) |
| 48 | ) { |
| 49 | return arr; |
| 50 | } |
| 51 | const firstEle = arr[0]; |
| 52 | let output: Record<string, any> = { items: [] }; |
| 53 | |
| 54 | Object.keys(firstEle).forEach((key) => { |
| 55 | const allValues = arr.map((ele) => ele[key]); |
| 56 | if (allValues.every((val) => val === allValues[0])) { |
| 57 | output[key] = allValues[0]; |
| 58 | } else if (output.items.length === 0) { |
| 59 | output.items = arr.map((ele) => ({ [key]: ele[key] })); |
| 60 | } else { |
| 61 | output.items.forEach((item: any, i: number) => { |
| 62 | item[key] = arr[i][key]; |
| 63 | }); |
| 64 | } |
| 65 | }); |
| 66 | if (output.items.length === 0) { |
| 67 | delete output.items; |
| 68 | } else { |
| 69 | output.items = output.items.map((item: any) => { |
| 70 | return deleteUndefined(item); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | // If there's no duplication (everything in 'items') then keep it as an array |
| 75 | if (Object.keys(output).length === 1 && "items" in output) { |
| 76 | return output.items; |
| 77 | } |
| 78 | |
| 79 | return output; |
| 80 | } |
| 81 | |
| 82 | export function filterKeys<InputObject extends any>( |
| 83 | obj: InputObject, |
nothing calls this directly
no test coverage detected