upsetjs param arr export function compressIndicesArray ( arr ) { if ( arr . length === 0 ) { return '' ; } const encoded = [ ] ; let startIndex = 0 ; const push = ( i ) => { if ( i === startIndex + 1 ) { encoded . push ( arr [ startIndex ] . toString ( ) ) ; } else if ( i === startIndex + 2 && i < 10 ) { encoded . push ( `${ arr [ startIndex ] },${ arr [ startIndex + 1 ] }` ) ; } else { encoded . push ( `${ arr [ startIndex ] }+${ i - startIndex - 1 }` ) ; } return i ; } ; for ( let i = 1 ; i < arr . length ; i ++ ) { const expected = arr [ i - 1 ] + 1 ; const v = arr [ i ] ; if ( v !== expected ) { startIndex = push ( i ) ; startIndex = i ; } } push ( arr . length ) ; return encoded . join ( ',' ) ; } param arr param toIndex export function toIndicesArray < T > ( arr , toIndex , { sortAble , compress = 'auto' } = { } ) { if ( arr . length === 0 ) { return [ ] ; } const base = arr . map ( ( v ) => toIndex ( v ) ) ; if ( compress === 'no' ) { return base ; } if ( sortAble ) { base . sort ( ( a , b ) => a - b ) ; } const encoded = compressIndicesArray ( base ) ; const baseLength = JSON . stringify ( base ) . length ; const encodedLength = encoded . length + 2 ; if ( encodedLength < baseLength * 0.6 || baseLength - encodedLength > 50 || ( compress === 'yes' && encodedLength < baseLength ) ) { return encoded ; } return base ; } param indices param elements export function fromIndicesArray < T > ( indices , elements ) { if ( typeof indices === 'string' ) { if ( indices . length === 0 ) { return [ ] ; } return indices . split ( ',' ) . map ( ( s ) => { if ( s . includes ( '+' ) ) { const [ start , length ] = s . split ( '+' ) . map ( ( si ) => Number . parseInt ( si , 10 ) ) ; return elements . slice ( start , start + length + 1 ) ; } return elements [ Number . parseInt ( s , 10 ) ] ! ; } ) . flat ( ) as T [ ] ; } return indices . map ( ( i ) => elements [ i ] ) ; }