Skip to content

Commit 953d9e0

Browse files
committed
Add Comparator.
1 parent 6f9600a commit 953d9e0

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/utils/comparator/Comparator.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default class Comparator {
2+
/**
3+
* @param {function(a: *, b: *)} [compareFunction]
4+
*/
5+
constructor(compareFunction) {
6+
this.compare = compareFunction || Comparator.defaultCompareFunction;
7+
}
8+
9+
/**
10+
* @param {(string|number)} a
11+
* @param {(string|number)} b
12+
* @returns {number}
13+
*/
14+
static defaultCompareFunction(a, b) {
15+
if (a === b) {
16+
return 0;
17+
}
18+
19+
return a < b ? -1 : 1;
20+
}
21+
22+
equal(a, b) {
23+
return this.compare(a, b) === 0;
24+
}
25+
26+
lessThen(a, b) {
27+
return this.compare(a, b) < 0;
28+
}
29+
30+
greaterThen(a, b) {
31+
return this.compare(a, b) > 0;
32+
}
33+
34+
lessThenOrEqual(a, b) {
35+
return this.lessThen(a, b) || this.equal(a, b);
36+
}
37+
38+
greaterThenOrEqual(a, b) {
39+
return this.greaterThen(a, b) || this.equal(a, b);
40+
}
41+
42+
reverse() {
43+
const compareOriginal = this.compare;
44+
this.compare = (a, b) => compareOriginal(b, a);
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Comparator from '../Comparator';
2+
3+
describe('Comparator', () => {
4+
it('should compare with default comparator function', () => {
5+
const comparator = new Comparator();
6+
7+
expect(comparator.equal(0, 0)).toBeTruthy();
8+
expect(comparator.equal(0, 1)).toBeFalsy();
9+
expect(comparator.equal('a', 'a')).toBeTruthy();
10+
expect(comparator.lessThen(1, 2)).toBeTruthy();
11+
expect(comparator.lessThen(-1, 2)).toBeTruthy();
12+
expect(comparator.lessThen('a', 'b')).toBeTruthy();
13+
expect(comparator.lessThen('a', 'ab')).toBeTruthy();
14+
expect(comparator.lessThen(10, 2)).toBeFalsy();
15+
expect(comparator.lessThenOrEqual(10, 2)).toBeFalsy();
16+
expect(comparator.lessThenOrEqual(1, 1)).toBeTruthy();
17+
expect(comparator.lessThenOrEqual(0, 0)).toBeTruthy();
18+
expect(comparator.greaterThen(0, 0)).toBeFalsy();
19+
expect(comparator.greaterThen(10, 0)).toBeTruthy();
20+
expect(comparator.greaterThenOrEqual(10, 0)).toBeTruthy();
21+
expect(comparator.greaterThenOrEqual(10, 10)).toBeTruthy();
22+
expect(comparator.greaterThenOrEqual(0, 10)).toBeFalsy();
23+
});
24+
25+
it('should compare with custom comparator function', () => {
26+
const comparator = new Comparator((a, b) => {
27+
if (a.length === b.length) {
28+
return 0;
29+
}
30+
31+
return a.length < b.length ? -1 : 1;
32+
});
33+
34+
expect(comparator.equal('a', 'b')).toBeTruthy();
35+
expect(comparator.equal('a', '')).toBeFalsy();
36+
expect(comparator.lessThen('b', 'aa')).toBeTruthy();
37+
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeFalsy();
38+
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeTruthy();
39+
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
40+
41+
comparator.reverse();
42+
43+
expect(comparator.equal('a', 'b')).toBeTruthy();
44+
expect(comparator.equal('a', '')).toBeFalsy();
45+
expect(comparator.lessThen('b', 'aa')).toBeFalsy();
46+
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeTruthy();
47+
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeFalsy();
48+
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
49+
});
50+
});

0 commit comments

Comments
 (0)