-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
This one may be by design, but it makes removal of an item via rules a little clumsy.
Perhaps there's a better example that can be added to the docs for removing an object from an array by rules that check properties of the objects.
Succeeds
import { ruleFactory } from '@elite-libs/rules-machine';
const things = [
{
name: "ABC",
value: "A Thing"
},
{
name: "DEF",
value: "A Stuff"
},
{
name: "GHI",
value: "A Thing"
},
{
name: "JKL",
value: "A Thing"
}];
const thingRules = ([
{
if: {
and: [
'CONTAINS(thing.name, ["ABC", "GHI"])',
'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
]
}, then: "keep = false"
},
{
if: 'LAST(SPLIT("", thing.name)) == "C"', then: 'keep = false'
},
{
return: "keep"
}
]);
const filterWithRules = ruleFactory(thingRules)
function isRateFiltered(thing: Record<string, string>): boolean {
const results = filterWithRules({
thing,
keep: true
});
return results;
}
console.dir(things.filter(isRateFiltered));Fails
PARSER FAIL: Error: Expected string, found: undefined undefinedimport { ruleFactory } from '@elite-libs/rules-machine';
const things = [
{
name: "ABC",
value: "A Thing"
},
{
name: "DEF",
value: "A Stuff"
},
{
name: "GHI",
value: "A Thing"
},
{
name: "JKL",
value: "A Thing"
}];
const thingRules = ([
{
if: {
and: [
'CONTAINS(thing.name, ["ABC", "GHI"])',
'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
]
}, then: "thing = false"
},
{
if: 'LAST(SPLIT("", thing.name)) == "C"', then: 'thing = false'
},
{
return: "thing"
}
]);
const filterWithRules = ruleFactory(thingRules)
function isRateFiltered(thing: Record<string, string>): boolean {
const results = filterWithRules({
thing
});
return results;
}
console.dir(things.filter(isRateFiltered));Metadata
Metadata
Assignees
Labels
No labels