Overview
Numeric operators perform numerical comparisons between values. Rule Engine JS provides four numeric comparison operators:
gte
Greater than or equal (≥)
lte
Less than or equal (≤)
Architecture
Source: src/operators/numeric.js | Tests: tests/unit/operators/numeric.test.js
Key Features
- Automatic type coercion - Converts string numbers to numeric values
- Dynamic comparison - Compare two field values or field to literal
- Strict mode - Prevent coercion when needed
- NaN handling - Proper error handling for invalid numbers
Syntax
All numeric operators follow the same pattern:
{ operator: [left, right] }
{ operator: [left, right, options] }
Parameters
Left operand - field path or literal value
Right operand - field path or literal value
Enable strict type checking - prevents string to number coercion
Operators
gt - Greater Than
{ gt: ['age', 18] } // age > 18
{ gt: ['score', 'threshold'] } // Dynamic comparison
Examples:
const data = { age: 25, price: 99.99, minPrice: 50 };
engine.evaluateExpr({ gt: ['age', 18] }, data);
// Result: { success: true } - 25 > 18
engine.evaluateExpr({ gt: ['price', 'minPrice'] }, data);
// Result: { success: true } - 99.99 > 50
engine.evaluateExpr({ gt: ['age', 30] }, data);
// Result: { success: false } - 25 not > 30
gte - Greater Than or Equal
{ gte: ['age', 18] } // age >= 18
{ gte: ['score', 'passScore'] } // Dynamic comparison
Examples:
const data = { age: 18, score: 85, passScore: 85 };
engine.evaluateExpr({ gte: ['age', 18] }, data);
// Result: { success: true } - 18 >= 18
engine.evaluateExpr({ gte: ['score', 'passScore'] }, data);
// Result: { success: true } - 85 >= 85
lt - Less Than
{ lt: ['price', 100] } // price < 100
{ lt: ['temp', 'maxTemp'] } // Dynamic comparison
Examples:
const data = { price: 49.99, temperature: 25, maxTemp: 30 };
engine.evaluateExpr({ lt: ['price', 100] }, data);
// Result: { success: true } - 49.99 < 100
engine.evaluateExpr({ lt: ['temperature', 'maxTemp'] }, data);
// Result: { success: true } - 25 < 30
lte - Less Than or Equal
{ lte: ['quantity', 100] } // quantity <= 100
{ lte: ['value', 'maxVal'] } // Dynamic comparison
Examples:
const data = { quantity: 100, discount: 20, maxDiscount: 25 };
engine.evaluateExpr({ lte: ['quantity', 100] }, data);
// Result: { success: true } - 100 <= 100
engine.evaluateExpr({ lte: ['discount', 'maxDiscount'] }, data);
// Result: { success: true } - 20 <= 25
Common Use Cases
Age Verification
Price Range
Score Validation
Inventory Check
const user = { age: 25 };
// Must be adult
const isAdult = { gte: ['age', 18] };
// Age range
const validAge = {
and: [
{ gte: ['age', 18] },
{ lte: ['age', 65] }
]
};
Type Coercion
Loose Mode (Default)
const data = { age: '25', limit: 18 };
// String "25" coerced to number 25
engine.evaluateExpr({ gt: ['age', 'limit'] }, data);
// Result: { success: true } - 25 > 18
Strict Mode
const strictEngine = createRuleEngine({ strict: true });
// No coercion - type mismatch error
strictEngine.evaluateExpr({ gt: ['age', 'limit'] }, data);
// Result: { success: false, error: "requires numeric operands" }
Coercion Table
| Input | Loose Mode | Strict Mode |
"25" | 25 ✅ | Error ❌ |
25 | 25 ✅ | 25 ✅ |
true | 1 ✅ | Error ❌ |
false | 0 ✅ | Error ❌ |
null | Error ❌ | Error ❌ |
"abc" | Error ❌ | Error ❌ |
Error Handling
const data = { name: 'John', age: 25 };
const result = engine.evaluateExpr({ gt: ['name', 10] }, data);
// Error: "GT operator requires numeric operands"
const result = engine.evaluateExpr({ gt: ['age'] }, data);
// Error: "GT operator requires 2-3 arguments, got 1"
const data = { age: 25 };
const result = engine.evaluateExpr({ gt: ['score', 10] }, data);
// Error: "GT operator requires numeric operands"
// (undefined cannot be coerced to number)
Quick Reference
| Operator | Symbol | True When | Example |
gt | > | left > right | 25 > 18 → true |
gte | ≥ | left ≥ right | 18 ≥ 18 → true |
lt | < | left < right | 10 < 20 → true |
lte | ≤ | left ≤ right | 20 ≤ 20 → true |
API Reference