As for the description of the filter condition, pattern matching is a very common and useful way. In JavaScript, it's quite handy to use JSON to describe patterns, so let's do a JSON pattern matching tool.
Use case design
As a dispatcher, we only need two methods: Notify and capture. One of the simplest use cases is this:
Copy Code code as follows:
Dispatcher.capture ({
"Status": 200,
"Command": "Message"
}, function (JSON) {/* Display message */});
Dispatcher.notify ({
"Status": 200,
"Command": "Message",
"Content": {
"From": "User1",
"To": "User2",
"Text": "Hello"
}
});
Of course, only local congruent matching is not enough, we need some other operators.
Copy Code code as follows:
Dispatcher.capture ({
"value1$eq": "Hello", * * equal * *
"Value2$ne" : TRUE,/* Not equal * *
"value3$lt": 0,/* Less than/
"value4$lte:1,/* less than or equal/
" Value5 $gt ": 2,/* greater than * * *
" Value6$gte ": 3,/* greater than or equal */
" value7$in ": [1, 3, 5, 7, 9],/* in * /
"Value8$nin": [2, 4, 6, 8, ten],/* not in */
"Value9$all": [1, 2, 3, 4, 5],/* all */
"Value10$ex": TR UE,/* exists * *
"value11$re":/^a.*/,/* Regular expression */
"VALUE12$LD": function (JSON) {return true;} /* lambda */
}, Function (JSON) {});
Dispatcher.notify ({
"value1": "Hello",
"value2": false,
"Value3":-1,
"value4": 1,
"Value5": 3,
"Value6": 3,
"Value7": 5,
"Value8": 5,
"Value9": [1, 3, 5, 2, 4],
"Value10": " Hello ",
Value11": "A13579",
"value12": "Anything"
})
Write down a bunch of operators, it looks like the implementation will be very complex? It's not really that complicated. In the next article, we'll discuss how to design an operator interface, and then implement the operators.