Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit b70571f

Browse files
committed
Changed constructor parameters
1 parent f5c6ba3 commit b70571f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ npm install @gurisko/sorted-array
1111

1212
## API
1313

14-
#### `new SortedArray()`
15-
16-
#### `new SortedArray([key], [compare])`
14+
#### `new SortedArray([key|compareFn])`
1715

1816
#### `arr.insert([element1[, ...[, elementN]]])`
1917

src/sorted-array.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ export class SortedArray<T> {
1313
protected key: string;
1414
protected cmp: (left: T, right: T) => number;
1515

16-
constructor(key: string = null, cmp?: (left: T, right: T) => number) {
16+
constructor(arg?: string | ((left: T, right: T) => number)) {
1717
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+
}
2029
}
2130

2231
get length(): number {
@@ -91,35 +100,35 @@ export class SortedArray<T> {
91100

92101
public eq(value: any): SortedArray<T> {
93102
const found: IEqualElements = this.getEqual(value);
94-
const result = new SortedArray(this.key, this.cmp);
103+
const result = new SortedArray(this.key || this.cmp);
95104
result.insert((found.length === 0) ? [] : this.items.slice(found.first, found.first + found.length));
96105
return result;
97106
}
98107

99108
public gt(value: any): SortedArray<T> {
100109
const index = this.greaterThan(value);
101-
const result = new SortedArray(this.key, this.cmp);
110+
const result = new SortedArray(this.key || this.cmp);
102111
result.insert((index < 0) ? [] : this.items.slice(index));
103112
return result;
104113
}
105114

106115
public lt(value: any): SortedArray<T> {
107116
const index = this.lessThan(value);
108-
const result = new SortedArray(this.key, this.cmp);
117+
const result = new SortedArray(this.key || this.cmp);
109118
result.insert((index < 0) ? [] : this.items.slice(0, index + 1));
110119
return result;
111120
}
112121

113122
public gte(value: any): SortedArray<T> {
114123
const index = this.greaterThan(value, true);
115-
const result = new SortedArray(this.key, this.cmp);
124+
const result = new SortedArray(this.key || this.cmp);
116125
result.insert((index < 0) ? [] : this.items.slice(index));
117126
return result;
118127
}
119128

120129
public lte(value: any): SortedArray<T> {
121130
const index = this.lessThan(value, true);
122-
const result = new SortedArray(this.key, this.cmp);
131+
const result = new SortedArray(this.key || this.cmp);
123132
result.insert((index < 0) ? [] : this.items.slice(0, index + 1));
124133
return result;
125134
}

0 commit comments

Comments
 (0)