Comparator

Monster/Util. Comparator

the comparator allows a comparison function to be abstracted.

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.5.0/dist/modules/types/id.js';
console.log(new Monster.Util.Comparator())
console.log(new Monster.Util.Comparator())
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {Comparator} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.5.0/dist/modules/types/id.js';
console.log(new Util())
console.log(new Util())
</script>

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(callback)

Source:
Since:
  • 1.3.0

create new comparator

Parameters:
Name Type Description
callback function

Methods

equal(a, b) → {boolean}

Source:

Checks if two variables are equal.

Parameters:
Name Type Description
a *
b *
Returns:
Type
boolean

greaterThan(a, b) → {boolean}

Source:

Checks if variable a is greater than b

Parameters:
Name Type Description
a *
b *
Returns:
Type
boolean

greaterThanOrEqual(a, b) → {boolean}

Source:

Checks if variable a is greater than or equal to b

Parameters:
Name Type Description
a *
b *
Returns:
Type
boolean

lessThan(a, b) → {boolean}

Source:

Checks if variable a is less than b

Parameters:
Name Type Description
a *
b *
Returns:
Type
boolean

lessThanOrEqual(a, b) → {boolean}

Source:

Checks if variable a is less than or equal to b

Parameters:
Name Type Description
a *
b *
Returns:
Type
boolean

reverse() → {Comparator}

Source:

changes the order of the operators

Returns:
Type
Comparator