Comments (3)
I think you just have a JavaScript problem, at least trying to break this down in a Node REPL.
Just the match
part of the rule:
jsonLogic.apply(
{"method":[{"var":"a"},"match",["(.)(.)(.)?(.)"]]},
{"a": "abc"}
);
Returns:
[ 'abc', 'a', 'b', undefined, 'c', index: 0, input: 'abc' ]
So the slice
returns [ undefined ]
. So you need a solution where undefined is not in an array (because ==
and ===
don't work on arrays unless they're literally references to the same memory location), and where it's probably not strictly compared to null. (I assume you're using null
in the rule because it's valid JSON while undefined
isn't.)
This rule returns true:
jsonLogic.apply(
{"==":[
null,
{"method":[
{"method":[
{"method":[
{"var":"a"},
"match",
["(.)(.)(.)?(.)"]
]},
"slice",
[3,4]
]},
"shift"
]}
]},
{"a": "abc"}
);
But that's kind of janky, it's like "abc".match().slice().shift()
when what you really want is "abc".match()[3]
I don't think JavaScript has a cleaner method to get data by index, and JsonLogic doesn't have one either, as you note, except on data, but you could add one:
jsonLogic.add_operation("index", function(a, b){
return a[b];
});
jsonLogic.apply(
{"==":[
null,
{"index":[
{"method":[
{"var":"a"},
"match",
["(.)(.)(.)?(.)"]
]},
3
]}
]},
{"a": "abc"}
);
You'll need to think through how your index function works, that one throws an exception if a
is null, which is to say if the match fails. But my next attempt returned something falsy if a is null or undefined, made the rule as a whole return true if the match fails.
from json-logic-js.
Really, what I'd want to do with that regex in a real language is assign the result to a temp variable and then run a few tests on it.
This adds two methods that let your rule set temp data in a namespace that can't overwrite your other data or otherwise escape.
JsonLogic isn't really set up to run rules sequentially, so we store the value in the first clause of theand
rule then retrieve it in the second (and potentially 3rd...Nth).
jsonLogic.add_operation("set_temp", function(key, value){
if( ! this.temp_variables){ this.temp_variables = {}; }
this.temp_variables[key] = value;
return value;
});
jsonLogic.add_operation("get_temp", function(key){
if( ! this.temp_variables){ this.temp_variables = {}; }
return this.temp_variables[key];
});
jsonLogic.add_operation("index", function(a, b){
if(typeof a === 'undefined' || a === null){
return undefined;
}
return a[b];
});
var rule = {"and":[
{'!=':[
null,
{"set_temp":[
"match_result",
{"method":[
{"var":"a"},
"match",
["(.)(.)(.)?(.)"]
]}
]}
]},
{"==":[
null,
{"index":[
{"get_temp":"match_result"},
3
]}
]}
]};
jsonLogic.apply(rule, {"a": "ab"}); //false, match fails
jsonLogic.apply(rule, {"a": "abc"}); //true, match succeeds, optional capture missing
jsonLogic.apply(rule, {"a": "abcd"}); //false, match succeeds, optional capture present
from json-logic-js.
Wow! Thank you very much for the follow-up, I had not thought about setting and getting variables within the logic tree like that.
I did add an operation for getting an array element by index and a custom match function which made things a lot easier than all the splice shift madness. :)
I'll definitely keep this trick handy too though.
from json-logic-js.
Related Issues (20)
- Inconsistent numeric var behaviour HOT 2
- Access to index in "reduce" operation
- Is it possible to return "Undefined"? HOT 1
- [Question] Using var operator at nested level of array operators HOT 2
- Addition function adding unwanted precision HOT 4
- Feature Request: ability to add custom operators which are not automatically traversed for evaluation HOT 9
- Would you be interested in a code merge? (json-logic-engine)
- How to know if logic is valid? HOT 1
- Referencing data values within a map HOT 1
- Handle undefined HOT 4
- Support friendly error messages? HOT 2
- Reference a key with a dot in it HOT 1
- Subtract breaks when there is a 0 between two values HOT 3
- Example on how to persist in a relational database HOT 1
- Global variable in the iteration HOT 2
- In-place data update
- [IDEA] Callable data
- Is There a way to split String by space or Comma HOT 2
- Can't seem to manipulate data inside an 'in' statement
- "IN" Operator Fails When Accessed Array via "VAR" Operator HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-logic-js.