GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add comparator annotations. · BrandonWeb/javascript-algorithms@1ecb5b4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/utils/comparator
    • Comparator.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/utils/comparator/Comparator.js‎

Copy file name to clipboardExpand all lines: src/utils/comparator/Comparator.js
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export default class Comparator {
22
/**
3-
* @param {function(a: *, b: *)} [compareFunction]
3+
* @param {function(a: *, b: *)} [compareFunction] - It may be custom compare function that, let's
4+
* say may compare custom objects together.
45
*/
56
constructor(compareFunction) {
67
this.compare = compareFunction || Comparator.defaultCompareFunction;
78
}
89

910
/**
11+
* Default comparison function. It just assumes that "a" and "b" are strings or numbers.
1012
* @param {(string|number)} a
1113
* @param {(string|number)} b
1214
* @returns {number}
@@ -19,26 +21,59 @@ export default class Comparator {
1921
return a < b ? -1 : 1;
2022
}
2123

24+
/**
25+
* Checks if two variables are equal.
26+
* @param {*} a
27+
* @param {*} b
28+
* @return {boolean}
29+
*/
2230
equal(a, b) {
2331
return this.compare(a, b) === 0;
2432
}
2533

34+
/**
35+
* Checks if variable "a" is less than "b".
36+
* @param {*} a
37+
* @param {*} b
38+
* @return {boolean}
39+
*/
2640
lessThan(a, b) {
2741
return this.compare(a, b) < 0;
2842
}
2943

44+
/**
45+
* Checks if variable "a" is greater than "b".
46+
* @param {*} a
47+
* @param {*} b
48+
* @return {boolean}
49+
*/
3050
greaterThan(a, b) {
3151
return this.compare(a, b) > 0;
3252
}
3353

54+
/**
55+
* Checks if variable "a" is less than or equal to "b".
56+
* @param {*} a
57+
* @param {*} b
58+
* @return {boolean}
59+
*/
3460
lessThanOrEqual(a, b) {
3561
return this.lessThan(a, b) || this.equal(a, b);
3662
}
3763

64+
/**
65+
* Checks if variable "a" is greater than or equal to "b".
66+
* @param {*} a
67+
* @param {*} b
68+
* @return {boolean}
69+
*/
3870
greaterThanOrEqual(a, b) {
3971
return this.greaterThan(a, b) || this.equal(a, b);
4072
}
4173

74+
/**
75+
* Reverses the comparison order.
76+
*/
4277
reverse() {
4378
const compareOriginal = this.compare;
4479
this.compare = (a, b) => compareOriginal(b, a);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.