A small, fast, safe, 0 dependencies, rules engine.
Serialize rules in JSON format.
[operator, value1, value2, ...]
const jr = require("json-rules")();
jr.evaluate([">", 2, 1]); // true (2 > 1)
jr.evaluate(["+", 2, 1]); // 3 (2 + 1)
jr.evaluate(["==", "foo", "bar"]); // false (foo === bar)
jr.evaluate(["!=", "foo", "bar"]); // true (foo !== bar)
jr.evaluate([">",
2,
["+", 2, 1]
]); // false (2 > 3)
jr.evaluate(["and",
["==", 1, 1],
[">", 2, 1]
]); // true (1 == 1 && 2 > 1)
const data = {
"type": "user"
"sub-type": "admin"
}
jr.evaluate(["and",
["==", "$.type", "user"],
["or",
["==", "$.sub-type", "admin"],
["==", "$.sub-type", "super"]
]
], data); // true (type === user && (sub-type === admin || sub-type === super))
const data = {
"foo": "foo",
"bar": "bar"
};
jr.evaluate(["!=", "$.foo", "$.baz"], data); // true (foo != baz)
const data = {
"baz": ["b", "a", "z"]
};
jr.evaluate(["==", "$.baz.1", "a"], data); // true (a == a)
const data = {
"foo": {
"bar": {
"baz": ["b", "a", "z"]
}
}
};
jr.evaluate(["==", "$.foo.bar.baz.1", "a"], data); // true (a == a)
function eq(x, y) {
// ['break;', false] instructs the loop to break and return false, this
// saves testing any further values as they must all be equal to pass
return x === y || ['break;', false];
}
jr.addOperator('equals', eq);
jr.evaluate(['equals', 1, 1]); // true (1 === 1)
jr.addAlias('>=', 'gte');
jr.evaluate('gte', 2, 2); // true (2 >= 2)
Type: Array
The array will be treated as a rule if the first element contains a known operator, otherwise it's treated as a standard array.
Type: Object
This can be referenced within rules using dot notation.
Type: string
Operator name e.g. equals.
Type: function
TODO
MIT