Function condition

Evaluates a set of conditions over an array of numbers, returning an array of booleans indicating whether each element in the input array satisfies all specified conditions.

The condition string should have the format {value/x} {expression} {x/value}, where value should to be number value, and expression like token: ">" | ">=" | "<" | "<=" | "=".

The conditions are parsed using the parseCondition function and each parsed condition is evaluated using the corresponding function in expr.

It supports conditions where 'x' denotes the variable and the expression determines the comparison to either side.

> const out = condition([0, 1, 2, 3], "1 <= x, x < 3")
> console.log(out)
[false, true, true, false]

Throws an error if any condition string does not match the required format.

  • Parameters

    • array: number[]

      The array of numbers to evaluate against the conditions.

    • conditions: string

      A comma-separated string containing conditions to evaluate.

    Returns boolean[]

    An array of boolean values indicating if each element in the array satisfies all the conditions. True means all conditions are met for that element, otherwise false.