@@ -5,36 +5,53 @@ namespace collections {
55 // from depending directly on the compiler to speed up compilation time.
66
77 import binarySearch = ts . binarySearch ;
8- import removeAt = ts . orderedRemoveItemAt ;
98
10- export function compareValues < T > ( a : T , b : T ) : number {
9+ function compareValues ( a : string | number , b : string | number ) : number {
1110 if ( a === b ) return 0 ;
1211 if ( a === undefined ) return - 1 ;
1312 if ( b === undefined ) return + 1 ;
1413 return a < b ? - 1 : + 1 ;
1514 }
1615
17- const caseInsensitiveComparisonCollator = typeof Intl === "object" ? new Intl . Collator ( /*locales*/ undefined , { usage : "sort" , sensitivity : "accent" } ) : undefined ;
18- const caseSensitiveComparisonCollator = typeof Intl === "object" ? new Intl . Collator ( /*locales*/ undefined , { usage : "sort" , sensitivity : "variant" } ) : undefined ;
16+ export function compareNumbers ( a : number , b : number ) : number {
17+ return compareValues ( a , b ) ;
18+ }
19+
20+ export function compareStrings ( a : string , b : string , ignoreCase : boolean ) : number {
21+ return ignoreCase
22+ ? compareStringsCaseInsensitive ( a , b )
23+ : compareStringsCaseSensitive ( a , b ) ;
24+ }
1925
20- export function compareStrings ( a : string | undefined , b : string | undefined , ignoreCase : boolean ) {
26+ export function compareStringsCaseSensitive ( a : string , b : string ) : number {
27+ return compareValues ( a , b ) ;
28+ }
29+
30+ export function compareStringsCaseInsensitive ( a : string , b : string ) : number {
2131 if ( a === b ) return 0 ;
2232 if ( a === undefined ) return - 1 ;
2333 if ( b === undefined ) return + 1 ;
24- const collator = ignoreCase ? caseInsensitiveComparisonCollator : caseSensitiveComparisonCollator ;
25- if ( collator ) {
26- return collator . compare ( a , b ) ;
27- }
28- else if ( ignoreCase ) {
29- a = a . toUpperCase ( ) ;
30- b = b . toUpperCase ( ) ;
31- }
34+ a = a . toUpperCase ( ) ;
35+ b = b . toUpperCase ( ) ;
3236 return a < b ? - 1 : a > b ? + 1 : 0 ;
3337 }
3438
35- export namespace compareStrings {
36- export function caseSensitive ( a : string | undefined , b : string | undefined ) { return compareStrings ( a , b , /*ignoreCase*/ false ) ; }
37- export function caseInsensitive ( a : string | undefined , b : string | undefined ) { return compareStrings ( a , b , /*ignoreCase*/ true ) ; }
39+ export function equateStringsCaseSensitive ( a : string , b : string ) : boolean {
40+ return a === b ;
41+ }
42+
43+ export function equateStringsCaseInsensitive ( a : string , b : string ) : boolean {
44+ return a === b
45+ || a !== undefined
46+ && b !== undefined
47+ && a . toUpperCase ( ) === b . toUpperCase ( ) ;
48+ }
49+
50+ function removeAt < T > ( array : T [ ] , index : number ) : void {
51+ for ( let i = index ; i < array . length - 1 ; i ++ ) {
52+ array [ i ] = array [ i + 1 ] ;
53+ }
54+ array . pop ( ) ;
3855 }
3956
4057 function insertAt < T > ( array : T [ ] , index : number , value : T ) {
@@ -63,7 +80,7 @@ namespace collections {
6380 private _version = 0 ;
6481 private _copyOnWrite = false ;
6582
66- constructor ( comparer : ( a : K , b : K ) => number = compareValues ) {
83+ constructor ( comparer : ( a : K , b : K ) => number ) {
6784 this . _comparer = comparer ;
6885 }
6986
@@ -72,16 +89,16 @@ namespace collections {
7289 }
7390
7491 public has ( key : K ) {
75- return binarySearch ( this . _keys , key , this . _comparer ) >= 0 ;
92+ return binarySearch ( this . _keys , key , ts . identity , this . _comparer ) >= 0 ;
7693 }
7794
7895 public get ( key : K ) {
79- const index = binarySearch ( this . _keys , key , this . _comparer ) ;
96+ const index = binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
8097 return index >= 0 ? this . _values [ index ] : undefined ;
8198 }
8299
83100 public set ( key : K , value : V ) {
84- const index = binarySearch ( this . _keys , key , this . _comparer ) ;
101+ const index = binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
85102 if ( index >= 0 ) {
86103 this . _values [ index ] = value ;
87104 }
@@ -96,7 +113,7 @@ namespace collections {
96113 }
97114
98115 public delete ( key : K ) {
99- const index = binarySearch ( this . _keys , key , this . _comparer ) ;
116+ const index = binarySearch ( this . _keys , key , ts . identity , this . _comparer ) ;
100117 if ( index >= 0 ) {
101118 this . writePreamble ( ) ;
102119 removeAt ( this . _keys , index ) ;
@@ -124,8 +141,8 @@ namespace collections {
124141 const order = this . getInsertionOrder ( ) ;
125142 const version = this . _version ;
126143 this . _copyOnWrite = true ;
127- for ( let i = 0 ; i < order . length ; i ++ ) {
128- callback ( values [ order [ i ] ] , keys [ order [ i ] ] , this ) ;
144+ for ( const index of order ) {
145+ callback ( values [ index ] , keys [ index ] , this ) ;
129146 }
130147 if ( version === this . _version ) {
131148 this . _copyOnWrite = false ;
@@ -144,7 +161,7 @@ namespace collections {
144161 private getInsertionOrder ( ) {
145162 return this . _order
146163 . map ( ( _ , i ) => i )
147- . sort ( ( x , y ) => compareValues ( this . _order [ x ] , this . _order [ y ] ) ) ;
164+ . sort ( ( x , y ) => compareNumbers ( this . _order [ x ] , this . _order [ y ] ) ) ;
148165 }
149166 }
150167
0 commit comments