• Check whether the given value is smaller than another value.

    If a value is an object that contains a compareTag, then this function will call that method and return whatever that method returned. This way, you can define your own classes with custom comparison operators. See the compareTag for an example.

    There are some specific rules when it comes to checking built-in JavaScript objects:

    • A string is smaller than another string if and only if each character in the first string is smaller than the corresponding character in the second string. If the second string contains more characters than the first but the overlapping characters are smaller or equal, the first string will also be considered smaller than the first.
    • An array is smaller than another array if each element of the first array is smaller than than each corresponding element of the second array. An exception is made when the second array contains more elements than the first array. In that case, the first array is smaller if and only if each element is less then or equal to the corresponding element in the second array. The remaining elements 'force' the array to be larger.
    • An object is only smaller than another object if the values of the first object is smaller then the corresponding value of the second object. An exception is made when the second object contains more keys than the first object. In that case, the first obejct is smaller if and only if each value in the first object is smaller or equal to the corresponding value in the second object. The extra keys in the second object 'force' the second object to be larger.

    The above rules might seem strange at first, but they ensure that we can perform equality checks on string, arrays and objects by just using lessThan, as demonstrated in the following code:

    import { lessThan } from "scl";

    function equalByLesser(a, b) {
    return !lessThan(a, b) && !lessThan(b, a);
    }

    console.log(equalByLesser(1, 1)) // true
    console.log(equalByLesser(1, 2)) // false
    console.log(equalByLesser('foo', 'foo')) // true
    console.log(equalByLesser('foo', 'bar')) // false
    console.log(equalByLesser([1, 2], [1, 2])) // true
    console.log(equalByLesser([1, 2], [1, 2, 3])) // false
    console.log(equalByLesser({ foo: 42 }, { foo: 42 }) // true
    console.log(equalByLesser({ foo: 42 }, { foo: 42, bar: 33 }) // false

    Parameters

    • a: any
    • b: any

    Returns boolean