mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-14 16:17:53 +00:00
19 lines
678 B
JavaScript
19 lines
678 B
JavaScript
export const processCSV = async (csvString, { filterColumn, filterValue, groupBy, aggregateColumn, operation }) => {
|
|
const [{ csvParse }, { rollup, sum, mean }] = await Promise.all([
|
|
import('https://esm.sh/d3-dsv'),
|
|
import('https://esm.sh/d3-array')
|
|
]);
|
|
|
|
const getValue = d => +(d[aggregateColumn]) || 0;
|
|
|
|
const grouped = rollup(
|
|
csvParse(csvString).filter(row => row[filterColumn] == filterValue),
|
|
group => operation === 'count'
|
|
? group.length
|
|
: (operation === 'sum' ? sum : mean)(group, getValue),
|
|
row => row[groupBy]
|
|
);
|
|
|
|
return Array.from(grouped, ([key, result]) => ({ [groupBy]: key, result }));
|
|
};
|
|
export default processCSV; |