mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-16 08:57:52 +00:00
18 lines
645 B
JavaScript
18 lines
645 B
JavaScript
export const processCSV = async (csv, { filterColumn: fc, filterValue: fv, groupBy: gb, aggregateColumn: ac, operation: op }) => {
|
|
const [{ parse }, { groupBy, map }] = await Promise.all([
|
|
import('https://esm.sh/papaparse@5.4.1'),
|
|
import('https://esm.sh/lodash-es@4.17.21')
|
|
]);
|
|
|
|
return map(groupBy(
|
|
parse(csv, { header: true, skipEmptyLines: true }).data.filter(r => r[fc] == fv),
|
|
gb
|
|
), (rows, key) => {
|
|
const sum = rows.reduce((a, r) => a + (+r[ac] || 0), 0);
|
|
return {
|
|
[gb]: key,
|
|
result: op === 'count' ? rows.length : op === 'avg' ? sum / rows.length : sum
|
|
};
|
|
});
|
|
};
|
|
export default processCSV; |