okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
407ea8192
main
1import type { Order , Summary } from './orders.ts' ; 2 3function compareCodePointOrder ( a :string , b :string ) :number { 4const pa = [ ...a ]; 5const pb = [ ...b ]; 6for ( let i = 0 ; i < Math . max ( pa . length , pb . length ); i ++ ){ 7const x = pa [ i ]; 8const y = pb [ i ]; 9if ( x === undefined ) return - 1 ; 10if ( y === undefined ) return 1 ; 11if ( x !== y ) return x < y ?- 1 :1 ; 12} 13return 0 ; 14} 15 16export function aggregateOrders ( orders :Order []) :Summary [] { 17const groups = new Map < string , Summary >(); 18for ( const o of orders ){ 19if ( o . cancelled ) continue ; 20let s = groups . get ( o . customer ); 21if ( ! s ){ s = { customer :o . customer , totalCents :0 , units :0 }; groups . set ( o . customer , s );} 22s . totalCents += o . priceCents * o . quantity ; 23s . units += o . quantity ; 24} 25return [ ...groups . values ()]. sort (( a , b ) => compareCodePointOrder ( a . customer , b . customer )); 26}