okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142c-small into f-142407ea8192

main
1017 B12 linesraw
1export type Order = {customer: string; priceCents: number; quantity: number; cancelled?: boolean};
2export type Summary = {customer: string; totalCents: number; units: number};
3export function summarizeOrders(orders: Order[]): Summary[] {
4  if(!Array.isArray(orders))throw new RangeError('orders');
5  const normalized=orders.map(o=>{
6    if(!o || typeof o.customer!=='string' || !o.customer.trim() || !Number.isInteger(o.priceCents) || o.priceCents<0 || !Number.isInteger(o.quantity) || o.quantity<=0 || (o.cancelled!==undefined && typeof o.cancelled!=='boolean'))throw new RangeError('order');
7    return {...o,customer:o.customer.trim()};
8  });
9  const groups=new Map<string,Summary>();
10  for(const o of normalized){if(o.cancelled)continue;let s=groups.get(o.customer);if(!s){s={customer:o.customer,totalCents:0,units:0};groups.set(o.customer,s);}s.totalCents+=o.priceCents*o.quantity;s.units+=o.quantity;}
11  return [...groups.values()].sort((a,b)=>a.customer<b.customer?-1:a.customer>b.customer?1:0);
12}