-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returns Affect Subsequent Rules #35
Comments
Can you clarify what you mean by "returns affect subsequent rules"? It's not clear from your examples. I'm not sure exactly where the error is coming from, but in your second example
|
The following reproduces the error message in v1.4.9: import { ruleFactory } from "@elite-libs/rules-machine";
const rule = 'SPLIT("", foo.bar)';
ruleFactory(rule)({ foo: true }); Closing because there is no bug here. The error message could certainly be more helpful, but that's a separate issue. |
I think there may be a misunderstanding about the context the rules are run. The rules are run in sequence in the same mutable context. Your second example is doing something very much like this: let thing = { foo: { bar: true } };
if (thing.foo.bar) thing = true;
// TypeError: Cannot read properties of undefined (reading 'bar')
if (thing.foo.bar) thing = "Whoops!"; |
Oh yea, @dara-rockwell, there's a |
@justsml @chhatch Yeah, that's what I figured it was doing. Is there a more elegant way to decide that we're removing an item besides adding a flag alongside the item? We'd like to use the rules engine to filter a list of items based on rules, but something like import { ruleFactory } from '@elite-libs/rules-machine';
const things = [
{
name: "ABC",
value: "A Thing to Remove"
},
{
name: "DEF",
value: "A Stuff to Keep"
},
{
name: "GHI",
value: "A Thing to Remove"
},
{
name: "JKL",
value: "A Thing to Keep"
}];
const thingRules = ([
{
if: {
and: [
'CONTAINS(thing.name, ["ABC", "GHI"])',
'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
]
}, then: "keep = false"
},
{
return: "keep"
}
]);
const filterWithRules = ruleFactory(thingRules)
function isFiltered(thing: Record<string, string>): boolean {
const result = filterWithRules({
thing,
keep: true
});
return result;
}
console.dir(things.filter(isFiltered)); |
@dara-rockwell With the work that was done by @chhatch in #47, and the additional work in #51, this adds support to filtering with multiple conditions. This changes your example to be the following: ...
const things = [
{
name: "ABC",
value: "A Thing to Remove"
},
{
name: "DEF",
value: "A Stuff to Keep"
},
{
name: "GHI",
value: "A Thing to Remove"
},
{
name: "JKL",
value: "A Thing to Keep"
}];
const thingRules = ([
{
filter: 'list',
run: { and: [
'CONTAINS(thing.name, ["ABC", "GHI"])',
'CONTAINS("thing", SPLIT(" ", LOWER(thing.value)))'
] },
set: 'results',
},
{ return: 'results' },
]);
...
const filterWithRules = ruleFactory(thingRules)
expect(filterWithRules({ list: things })).toEqual([
{
name: "DEF",
value: "A Stuff to Keep"
},
{
name: "JKL",
value: "A Thing to Keep"
}
]) |
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
Fails
The text was updated successfully, but these errors were encountered: