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