Project: typed transformation pipeline
Request
The retail reporting team needs one local transformation function for regional net sales.
Data
Fictional transaction records contain id, region, amount, and optional status/refund fields. Amounts and refunds are fixture values in integer cents. Later valid duplicates replace earlier ones.
Deliverable
A JSON report with a reconciled total and ranked region summaries. Invalid rows are excluded, all other stages run in the prescribed order, and ties use the region name.
A retail reporting service receives transaction rows from several local exports. Region names vary in case, identifiers can repeat, held transactions should not count, and refunds can make a region's net value negative. The final report must reconcile its headline total with its region details.
Build one pipeline through eight continuing steps. Validate the boundary, normalize regions, keep the latest valid row per identifier, select paid rows, subtract refunds, aggregate regions, rank the summaries, and return the reconciled deliverable. Ordering matters. Filtering paid rows before deduplication can accidentally count an outdated row. Grouping before normalization splits one region into several labels. Each new step starts from your previous implementation and adds an observable requirement, including empty input and zero totals.
type InputRow={id:string;region:string;amount:number;status?:string;refund?:number};
function solve(value:string):string {const rows=JSON.parse(value) as InputRow[];const valid=rows.filter(row=>typeof row.id==="string" && typeof row.region==="string" && typeof row.amount==="number" && Number.isFinite(row.amount));const normalized=valid;const unique=normalized;const eligible=unique;const adjusted=eligible;const result=adjusted.map(row=>({region:row.region,amount:row.amount}));return JSON.stringify(result);}
A complete implementation for the first scenario; trace its input before editing.