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