mirror of
https://github.com/lynchmarkHQ/lynchmark.com.git
synced 2026-10-14 00:05:21 +00:00
16 lines
677 B
JavaScript
16 lines
677 B
JavaScript
export const processCSV=async(csv,cfg)=>{
|
|
const [{parse},{groupBy,sumBy}]=await Promise.all([
|
|
import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm'),
|
|
import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm')
|
|
]);
|
|
const {filterColumn:fc,filterValue:fv,groupBy:gb,aggregateColumn:ac,operation:op}=cfg;
|
|
const rows=parse(csv,{header:!0,skipEmptyLines:!0}).data.filter(r=>r[fc]==fv);
|
|
const grouped=groupBy(rows,r=>r[gb]);
|
|
const agg=v=>{
|
|
if(op=='count')return v.length;
|
|
const total=sumBy(v,x=>+x[ac]||0);
|
|
return op=='avg'&&v.length?total/v.length:total;
|
|
};
|
|
return Object.entries(grouped).map(([k,v])=>({[gb]:k,result:agg(v)}));
|
|
};
|
|
export default processCSV; |