CSV report generator
Request
Harbor Audio operations needs a CSV export body for a delivery review.
Data
Names and signed units arrive in equal-length arrays. All names and records are fictional.
Deliverable
Return CSV text with a header, escaped accepted name groups in sorted order, and a TOTAL footer that reconciles to the accepted source units. Every line ends with a newline.
Harbor Audio tracks delivery units by recording name and needs a report body it can save as CSV. The browser task receives arrays and returns text; it doesn't write a file. Each step extends the same function, from a header and raw rows to safe quoting, rejected-row filtering, grouping, sorting and a total footer.
CSV formatting is precise. Commas, quotes and newlines inside a name require a quoted field, and quotes inside that field must double. Group and sort the original names before escaping them. A negative row is rejected before it can create a group or change a total. Keep zero-unit groups. The final report reconciles its detail rows with its footer, including repeated source names and an input containing no accepted rows.
names = ["Voice, dry", "Bass", "Voice, dry"]
units = [2, 1, 3]
// Final text:
// name,units
// Bass,1
// "Voice, dry",5
// TOTAL,6
Escape the name for output after grouping its two accepted source rows.