@@ -571,7 +571,7 @@ namespace ts {
571
571
*/
572
572
export function append < T > ( to : T [ ] | undefined , value : T | undefined ) : T [ ] | undefined {
573
573
if ( value === undefined ) return to ;
574
- if ( to === undefined ) to = [ ] ;
574
+ if ( to === undefined ) return [ value ] ;
575
575
to . push ( value ) ;
576
576
return to ;
577
577
}
@@ -592,6 +592,16 @@ namespace ts {
592
592
return to ;
593
593
}
594
594
595
+ /**
596
+ * Stable sort of an array. Elements equal to each other maintain their relative position in the array.
597
+ */
598
+ export function stableSort < T > ( array : T [ ] , comparer : ( x : T , y : T ) => Comparison = compareValues ) {
599
+ return array
600
+ . map ( ( _ , i ) => i ) // create array of indices
601
+ . sort ( ( x , y ) => comparer ( array [ x ] , array [ y ] ) || compareValues ( x , y ) ) // sort indices by value then position
602
+ . map ( i => array [ i ] ) ; // get sorted array
603
+ }
604
+
595
605
export function rangeEquals < T > ( array1 : T [ ] , array2 : T [ ] , pos : number , end : number ) {
596
606
while ( pos < end ) {
597
607
if ( array1 [ pos ] !== array2 [ pos ] ) {
@@ -2099,6 +2109,17 @@ namespace ts {
2099
2109
}
2100
2110
2101
2111
/** Remove an item from an array, moving everything to its right one space left. */
2112
+ export function orderedRemoveItem < T > ( array : T [ ] , item : T ) : boolean {
2113
+ for ( let i = 0 ; i < array . length ; i ++ ) {
2114
+ if ( array [ i ] === item ) {
2115
+ orderedRemoveItemAt ( array , i ) ;
2116
+ return true ;
2117
+ }
2118
+ }
2119
+ return false ;
2120
+ }
2121
+
2122
+ /** Remove an item by index from an array, moving everything to its right one space left. */
2102
2123
export function orderedRemoveItemAt < T > ( array : T [ ] , index : number ) : void {
2103
2124
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
2104
2125
for ( let i = index ; i < array . length - 1 ; i ++ ) {
0 commit comments