As for the description of filter conditions, pattern matching is a very common and useful way. In JavaScript, it is quite convenient to use JSON to describe patterns, so let's make 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:
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, local identical matching is not enough, we also need some other operators.
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, 10], /* not in */
"value9$all": [1, 2, 3, 4, 5], /* all */
"value10$ex": true, /* 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"
})
Writing down a bunch of operators, it seems that the implementation will be complicated? In fact, in the next article, we will discuss how to design an operator interface and then implement these operators one by one.