mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Add comparator annotations.
This commit is contained in:
parent
f7ebddc82d
commit
1ecb5b4236
@ -1,12 +1,14 @@
|
|||||||
export default class Comparator {
|
export default class Comparator {
|
||||||
/**
|
/**
|
||||||
* @param {function(a: *, b: *)} [compareFunction]
|
* @param {function(a: *, b: *)} [compareFunction] - It may be custom compare function that, let's
|
||||||
|
* say may compare custom objects together.
|
||||||
*/
|
*/
|
||||||
constructor(compareFunction) {
|
constructor(compareFunction) {
|
||||||
this.compare = compareFunction || Comparator.defaultCompareFunction;
|
this.compare = compareFunction || Comparator.defaultCompareFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Default comparison function. It just assumes that "a" and "b" are strings or numbers.
|
||||||
* @param {(string|number)} a
|
* @param {(string|number)} a
|
||||||
* @param {(string|number)} b
|
* @param {(string|number)} b
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
@ -19,26 +21,59 @@ export default class Comparator {
|
|||||||
return a < b ? -1 : 1;
|
return a < b ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if two variables are equal.
|
||||||
|
* @param {*} a
|
||||||
|
* @param {*} b
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
equal(a, b) {
|
equal(a, b) {
|
||||||
return this.compare(a, b) === 0;
|
return this.compare(a, b) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if variable "a" is less than "b".
|
||||||
|
* @param {*} a
|
||||||
|
* @param {*} b
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
lessThan(a, b) {
|
lessThan(a, b) {
|
||||||
return this.compare(a, b) < 0;
|
return this.compare(a, b) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if variable "a" is greater than "b".
|
||||||
|
* @param {*} a
|
||||||
|
* @param {*} b
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
greaterThan(a, b) {
|
greaterThan(a, b) {
|
||||||
return this.compare(a, b) > 0;
|
return this.compare(a, b) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if variable "a" is less than or equal to "b".
|
||||||
|
* @param {*} a
|
||||||
|
* @param {*} b
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
lessThanOrEqual(a, b) {
|
lessThanOrEqual(a, b) {
|
||||||
return this.lessThan(a, b) || this.equal(a, b);
|
return this.lessThan(a, b) || this.equal(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if variable "a" is greater than or equal to "b".
|
||||||
|
* @param {*} a
|
||||||
|
* @param {*} b
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
greaterThanOrEqual(a, b) {
|
greaterThanOrEqual(a, b) {
|
||||||
return this.greaterThan(a, b) || this.equal(a, b);
|
return this.greaterThan(a, b) || this.equal(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverses the comparison order.
|
||||||
|
*/
|
||||||
reverse() {
|
reverse() {
|
||||||
const compareOriginal = this.compare;
|
const compareOriginal = this.compare;
|
||||||
this.compare = (a, b) => compareOriginal(b, a);
|
this.compare = (a, b) => compareOriginal(b, a);
|
||||||
|
Loading…
Reference in New Issue
Block a user