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 ) { 5throw new RangeError ( "gap must be a finite nonnegative number" ); 6} 7for ( const w of windows ) { 8if ( ! w || ! Number . isFinite ( w . start ) || ! Number . isFinite ( w . end ) || w . start > w . end ) { 9throw new RangeError ( "window must have finite numeric start <= end" ); 10} 11} 12const sorted = [ ...windows ]. sort (( a , b ) => a . start - b . start || a . end - b . end ); 13const result :Window [] = []; 14for ( const w of sorted ) { 15const last = result . at ( - 1 ); 16if ( last && w . start - last . end <= gap ) { 17last . end = Math . max ( last . end , w . end ); 18} else { 19result . push ({ start :w . start , end :w . end }); 20} 21} 22return result ; 23}