@@ -13,10 +13,19 @@ export class SortedArray<T> {
13
13
protected key : string ;
14
14
protected cmp : ( left : T , right : T ) => number ;
15
15
16
- constructor ( key : string = null , cmp ?: ( left : T , right : T ) => number ) {
16
+ constructor ( arg ? : string | ( ( left : T , right : T ) => number ) ) {
17
17
this . items = [ ] ;
18
- this . key = key ;
19
- this . cmp = cmp || ( this . key ? this . objectComparison : this . defaultComparison ) ;
18
+ const type = typeof arg ;
19
+ if ( type === 'string' ) {
20
+ this . key = arg as string ;
21
+ this . cmp = this . objectComparison ;
22
+ } else if ( type === 'function' ) {
23
+ this . key = null ;
24
+ this . cmp = arg as ( ( left : T , right : T ) => number ) ;
25
+ } else {
26
+ this . key = null ;
27
+ this . cmp = this . defaultComparison ;
28
+ }
20
29
}
21
30
22
31
get length ( ) : number {
@@ -91,35 +100,35 @@ export class SortedArray<T> {
91
100
92
101
public eq ( value : any ) : SortedArray < T > {
93
102
const found : IEqualElements = this . getEqual ( value ) ;
94
- const result = new SortedArray ( this . key , this . cmp ) ;
103
+ const result = new SortedArray ( this . key || this . cmp ) ;
95
104
result . insert ( ( found . length === 0 ) ? [ ] : this . items . slice ( found . first , found . first + found . length ) ) ;
96
105
return result ;
97
106
}
98
107
99
108
public gt ( value : any ) : SortedArray < T > {
100
109
const index = this . greaterThan ( value ) ;
101
- const result = new SortedArray ( this . key , this . cmp ) ;
110
+ const result = new SortedArray ( this . key || this . cmp ) ;
102
111
result . insert ( ( index < 0 ) ? [ ] : this . items . slice ( index ) ) ;
103
112
return result ;
104
113
}
105
114
106
115
public lt ( value : any ) : SortedArray < T > {
107
116
const index = this . lessThan ( value ) ;
108
- const result = new SortedArray ( this . key , this . cmp ) ;
117
+ const result = new SortedArray ( this . key || this . cmp ) ;
109
118
result . insert ( ( index < 0 ) ? [ ] : this . items . slice ( 0 , index + 1 ) ) ;
110
119
return result ;
111
120
}
112
121
113
122
public gte ( value : any ) : SortedArray < T > {
114
123
const index = this . greaterThan ( value , true ) ;
115
- const result = new SortedArray ( this . key , this . cmp ) ;
124
+ const result = new SortedArray ( this . key || this . cmp ) ;
116
125
result . insert ( ( index < 0 ) ? [ ] : this . items . slice ( index ) ) ;
117
126
return result ;
118
127
}
119
128
120
129
public lte ( value : any ) : SortedArray < T > {
121
130
const index = this . lessThan ( value , true ) ;
122
- const result = new SortedArray ( this . key , this . cmp ) ;
131
+ const result = new SortedArray ( this . key || this . cmp ) ;
123
132
result . insert ( ( index < 0 ) ? [ ] : this . items . slice ( 0 , index + 1 ) ) ;
124
133
return result ;
125
134
}
0 commit comments