|
| 1 | +export default class Comparator { |
| 2 | + /** |
| 3 | + * Constructor. |
| 4 | + * @param {function(a: *, b: *)} [compareFunction] - It may be custom compare function that, let's |
| 5 | + * say may compare custom objects together. |
| 6 | + */ |
| 7 | + |
| 8 | + constructor(compareFunction) { |
| 9 | + this.compare = compareFunction || Comparator.defaultCompareFunction; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * Default comparison function. It just assumes that 'a' and 'b' are strings or numbers. |
| 14 | + * @param {(string|number)} a |
| 15 | + * @param {(string|number)} b |
| 16 | + * @returns {number} |
| 17 | + */ |
| 18 | + static defaultCompareFunction(a, b) { |
| 19 | + if (a === b) { |
| 20 | + return 0; |
| 21 | + } |
| 22 | + |
| 23 | + return a < b ? -1 : 1; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Checks if two variables are equal. |
| 28 | + * @param {*} a |
| 29 | + * @param {*} b |
| 30 | + * @return {boolean} |
| 31 | + */ |
| 32 | + // Traditional function |
| 33 | + equal(a, b) { |
| 34 | + return this.compare(a, b) === 0; |
| 35 | + } |
| 36 | + // Arrow function |
| 37 | + // equal = (a, b) => this.compare(a, b) === 0; |
| 38 | + |
| 39 | + /** |
| 40 | + * Checks if variable "a" is less than "b" |
| 41 | + * @param {*} a |
| 42 | + * @param {*} b |
| 43 | + * @returns {boolean} |
| 44 | + */ |
| 45 | + // Traditional function |
| 46 | + lessThan(a, b) { |
| 47 | + return this.compare(a, b) < 0; |
| 48 | + } |
| 49 | + // Arrow function |
| 50 | + // lessThan = (a, b) => this.compare(a, b) < 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * Checks if variable "a" is greater than "b". |
| 54 | + * @param {*} a |
| 55 | + * @param {*} b |
| 56 | + * @returns {boolean} |
| 57 | + */ |
| 58 | + // Traditional function |
| 59 | + greaterThan(a, b) { |
| 60 | + return this.compare(a, b) > 0; |
| 61 | + } |
| 62 | + // Arrow function |
| 63 | + // greaterThan = (a, b) => this.compare(a, b) > 0; |
| 64 | + |
| 65 | + /** |
| 66 | + * Checks if variable "a" is less than or equal to "b". |
| 67 | + * @param {*} a |
| 68 | + * @param {*} b |
| 69 | + * @returns {boolean} |
| 70 | + */ |
| 71 | + // Traditional function |
| 72 | + lessThanOrEqual(a, b) { |
| 73 | + return this.lessThan(a, b) || this.equal(a, b); |
| 74 | + } |
| 75 | + // Arrow function |
| 76 | + // lessThanOrEqual = (a, b) => this.lessThan(a, b) || this.equal(a, b); |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks if variable "a" is greater than or equal to "b"l |
| 80 | + * @param {*} a |
| 81 | + * @param {*} b |
| 82 | + * @returns {boolean} |
| 83 | + */ |
| 84 | + // Traditional function |
| 85 | + greaterThanOrEqual(a, b) { |
| 86 | + return this.greaterThan(a, b) || this.equal(a, b); |
| 87 | + } |
| 88 | + |
| 89 | + //Arrow Function |
| 90 | + // greaterThanOrEqual = (a, b) => this.greaterThan(a, b) || this.equal(a, b); |
| 91 | + |
| 92 | + /** |
| 93 | + * Reverses the comparison order. |
| 94 | + */ |
| 95 | + |
| 96 | + reverse() { |
| 97 | + const compareOriginal = this.compare; |
| 98 | + this.compare = (a, b) => compareOriginal(b, a); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +const instanceA = new Comparator(); |
0 commit comments