mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-15 08:37:53 +00:00
16 lines
593 B
JavaScript
16 lines
593 B
JavaScript
const processCSV = async (csvString, config) => {
|
|
const [{ csvParse }, { rollups, sum }] = await Promise.all([
|
|
import('https://esm.sh/d3-dsv'),
|
|
import('https://esm.sh/d3-array')
|
|
]);
|
|
|
|
const { filterColumn: fc, filterValue: fv, groupBy: gb, aggregateColumn: ac, operation: op } = config;
|
|
const getVal = d => +d[ac] || 0;
|
|
|
|
return rollups(
|
|
csvParse(csvString).filter(d => d[fc] == fv),
|
|
g => op === 'count' ? g.length : (op === 'avg' ? sum(g, getVal) / g.length : sum(g, getVal)),
|
|
d => d[gb]
|
|
).map(([k, v]) => ({ [gb]: k, result: v }));
|
|
};
|
|
export default processCSV; |