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