Monster.Util. Comparator

The comparator allows a comparison function to be abstracted.

The following are some examples of the application of the class.

new Comparator().lessThanOrEqual(2, 5) // ↦ true
new Comparator().greaterThan(4, 2) // ↦ true
new Comparator().equal(4, 4) // ↦ true
new Comparator().equal(4, 5) // ↦ false

You can also pass your own comparison function, and thus define the comparison function.

new Comparator(function (a, b) {
     if (a.v === b.v) return 0;
        return a.v < b.v ? -1 : 1;
     }).equal({v: 2}, {v: 2});  // ↦ true

Constructor

new Comparator(callbackopt)

create new comparator

.

create new comparator

Parameters:
NameTypeAttributesDescription
callbackMonster.Util~exampleCallback<optional>

Comparator callback

Since
  • 1.3.0
License
  • AGPLv3
Example
import {Comparator} from '@schukai/monster/source/util/comparator.mjs';

console.log(new Comparator().lessThanOrEqual(2, 5))
// ↦ true
console.log(new Comparator().greaterThan(4, 2))
// ↦ true
console.log(new Comparator().equal(4, 4))
// ↦ true
console.log(new Comparator().equal(4, 5))
// ↦ false

Methods

compare(a, b) → {integer}

Parameters:
NameTypeDescription
a*
b*
Returns:

-1, 0 or 1

Type: 
integer

equal(a, b) → {boolean}

Checks if two variables are equal.

Checks if two variables are equal.

Parameters:
NameTypeDescription
a*
b*
Returns:
Type: 
boolean

greaterThan(a, b) → {boolean}

Checks if variable a is greater than b

.

Checks if variable a is greater than b

Parameters:
NameTypeDescription
a*
b*
Returns:
Type: 
boolean

greaterThanOrEqual(a, b) → {boolean}

Checks if variable a is greater than or equal to b

.

Checks if variable a is greater than or equal to b

Parameters:
NameTypeDescription
a*
b*
Returns:
Type: 
boolean

lessThan(a, b) → {boolean}

Checks if variable a is less than b

.

Checks if variable a is less than b

Parameters:
NameTypeDescription
a*
b*
Returns:
Type: 
boolean

lessThanOrEqual(a, b) → {boolean}

Checks if variable a is less than or equal to b

.

Checks if variable a is less than or equal to b

Parameters:
NameTypeDescription
a*
b*
Returns:
Type: 
boolean

reverse() → {Comparator}

changes the order of the operators

.

changes the order of the operators

Returns:
Type: 
Comparator