Skip to content

Commit fe635d8

Browse files
committed
feat(comparator): added comparator class for comparisons
1 parent dff3394 commit fe635d8

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './math';
2+
export * from './utils';

src/lib/math/greatest-common-divisor/greatest-common-divisor.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// tslint:disable:no-expression-statement
2+
// @ts-ignore
23
import test from 'ava';
34
import { greatestCommonDivisor } from './greatest-common-divisor';
45

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// tslint:disable:no-expression-statement
2+
import test from 'ava';
3+
import { Comparator } from './comparator';
4+
5+
test('comparator equal', t => {
6+
const comparator = new Comparator<number>((a, b) =>
7+
a === b ? 0 : a < b ? -1 : 1
8+
);
9+
t.is(comparator.equal(1, 1), true);
10+
t.is(comparator.equal(1, 2), false);
11+
});
12+
13+
test('comparator equal (default comparer)', t => {
14+
const numberComparator = new Comparator<number>();
15+
t.is(numberComparator.equal(-1, -1), true);
16+
t.is(numberComparator.equal(3, 4), false);
17+
18+
const stringComparator = new Comparator<string>();
19+
t.is(stringComparator.equal('s', 's'), true);
20+
t.is(stringComparator.equal('2019 Year', '2020 Year'), false);
21+
});
22+
23+
test('comparator less', t => {
24+
const comparator = new Comparator<number>();
25+
t.is(comparator.less(3, 4), true);
26+
t.is(comparator.less(-4, -3), true);
27+
t.is(comparator.less(-1, -1), false);
28+
});
29+
30+
test('comparator lessOrEqual', t => {
31+
const comparator = new Comparator<number>();
32+
t.is(comparator.lessOrEqual(3, 4), true);
33+
t.is(comparator.lessOrEqual(-1, -1), true);
34+
t.is(comparator.lessOrEqual(-1, -2), false);
35+
});
36+
37+
test('comparator greater', t => {
38+
const comparator = new Comparator<number>();
39+
t.is(comparator.greater(4, 3), true);
40+
t.is(comparator.greater(-1, -1), false);
41+
});
42+
43+
test('comparator greaterOrEqual', t => {
44+
const comparator = new Comparator<number>();
45+
t.is(comparator.greaterOrEqual(4, 3), true);
46+
t.is(comparator.greaterOrEqual(-1, -1), true);
47+
t.is(comparator.greaterOrEqual(-2, -1), false);
48+
});
49+
50+
test('comparator reverse', t => {
51+
const comparator = new Comparator<number>();
52+
t.is(comparator.less(3, 4), true);
53+
54+
comparator.reverse();
55+
t.is(comparator.less(3, 4), false);
56+
57+
comparator.reverse();
58+
t.is(comparator.less(3, 4), true);
59+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
type TCompareFn = (a, b) => number;
2+
3+
export class Comparator<T> {
4+
public static defaultCompareFn(a, b): number {
5+
return a === b ? 0 : a < b ? -1 : 1;
6+
}
7+
8+
private compareFn: TCompareFn;
9+
10+
constructor(compareFn: TCompareFn = Comparator.defaultCompareFn) {
11+
if (typeof compareFn !== 'function') {
12+
compareFn = Comparator.defaultCompareFn;
13+
}
14+
15+
this.compareFn = compareFn;
16+
}
17+
18+
/**
19+
* Checks if two values are equal
20+
*/
21+
public equal(a: T, b: T): boolean {
22+
return this.compareFn(a, b) === 0;
23+
}
24+
25+
/**
26+
* Checks if variable "a" is less than "b"
27+
*/
28+
public less(a: T, b: T): boolean {
29+
return this.compareFn(a, b) < 0;
30+
}
31+
32+
/**
33+
* Checks if variable "a" is greater than "b"
34+
*/
35+
public greater(a: T, b: T): boolean {
36+
return this.compareFn(a, b) > 0;
37+
}
38+
39+
/**
40+
* Checks if variable "a" is less than or equal to "b"
41+
*/
42+
public lessOrEqual(a: T, b: T): boolean {
43+
return this.less(a, b) || this.equal(a, b);
44+
}
45+
46+
/**
47+
* Checks if variable "a" is greater than or equal to "b"
48+
*/
49+
public greaterOrEqual(a: T, b: T): boolean {
50+
return this.greater(a, b) || this.equal(a, b);
51+
}
52+
53+
/**
54+
* Reverses the comparison order
55+
*/
56+
public reverse(): void {
57+
const compareFn = this.compareFn;
58+
this.compareFn = (a, b) => compareFn(b, a);
59+
}
60+
}

src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './comparator/comparator';

0 commit comments

Comments
 (0)