okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
407ea8192
main
1export type Window = { start :number ; end :number }; 2 3export function mergeWindows ( windows :Window [], gap = 0 ) :Window [] { 4if ( ! Number . isFinite ( gap ) || gap < 0 ) throw new RangeError ( "gap must be finite and nonnegative" ); 5for ( const w of windows ) { 6if ( ! Number . isFinite ( w . start ) || ! Number . isFinite ( w . end ) || w . start > w . end ) { 7throw new RangeError ( "window start/end must be finite with start <= end" ); 8} 9} 10const sorted = windows . map (( w ) => ({ start :w . start , end :w . end })) 11. sort (( a , b ) => a . start - b . start || a . end - b . end ); 12const result :Window [] = []; 13for ( const w of sorted ) { 14const last = result . at ( - 1 ); 15if ( last && w . start <= last . end + gap ) last . end = Math . max ( last . end , w . end ); 16else result . push ( w ); 17} 18return result ; 19}