JSON pattern matching using JavaScript (Part 2-Implementation)

Source: Internet
Author: User

Policy & Capture
It is too easy to implement policy and capture. We only need to save the handler passed by capture, and then find the matched handler in policy.
Copy codeThe Code is as follows:
Var filterHandlerBundles = [];
Dispatch. capture = function (pattern, handler ){
Var filter = createFilter (pattern );
FilterHandlerBundles. push ({
"Filter": filter,
"Handler": handler
});
};
Dispatcher. Policy = function (json ){
For (var I = 0; I <filterHandlerBundles. length; I ++ ){
If (filterHandlerBundles [I]. filter. apply (this, arguments )){
FilterHandlerBundles [I]. handler (json );
}
}
};

The logic of this code is very clear. The key lies in the createFilter section. This function converts json in the description mode into a function that determines whether JSON matches.
Operators
We have designed a lot of computational methods. How can we implement them? Remember, do not switch case. Therefore, we use an associated array to save the ing between operators and implementations.
Copy codeThe Code is as follows:
Var operators = {};
Operators ["lt"] = function (testValue, value ){
Return arguments. length = 2 & value <testValue;
};
Operators ["lte"] = function (testValue, value ){
Return arguments. length = 2 & value <= testValue;
};
Operators ["gt"] = function (testValue, value ){
Return arguments. length = 2 & value> testValue;
};
Operators ["gte"] = function (testValue, value ){
Return arguments. length = 2 & value> = testValue;
};

In this way, we only need to extract the operators after "$", and then we can immediately find the corresponding judgment function. The above four are comparison operators. The implementation is easier, so here is an example.
A difficult function is eq, because it needs to select a specific judgment method based on the data type. For String, Number, and Boolean, eq means =. For Array, eq means that each element in it is eq, and the order is consistent. For Object, the meaning of eq is that every sub-condition meets, so we need to extract the operator string of each sub-condition and then call the corresponding operator. For details, refer to the complete code.
Other operators will be simpler. Here I will just give you a prompt that you can use the subset or superset of these operators as needed:

In-traversal array to see if at least one eq can be found.
All-traverse the array to check whether each of them has an eq.
Ex-if there is an input value, the child element exists.
Re-use a regular expression to determine whether the string matches.
Ld-directly call the function for determination.
Have you finished writing? Are you not sure you have written correctly? This is what we will discuss in the next article. Let's add a default operator first.
Copy codeThe Code is as follows:
Operators [""] = function (testValue, value ){
If (testValue instanceof Array ){
Return operators ["in"]. apply (this, arguments );
} Else if (testValue instanceof RegExp ){
Return operators ["re"]. apply (this, arguments );
} Else if (testValue instanceof Function ){
Return operators ["ld"]. apply (this, arguments );
} Else {
Return operators ["eq"]. apply (this, arguments );
}
};

Why is a default operator required? This is actually a shortcut. In most cases, all we need is eq operations. If operators are written in every place, the code will become very complicated and not beautiful. Which one do you think is more natural?
Copy codeThe Code is as follows:
Dispatcher. capture ({
"Status": 200,
"Command": "message"
}, Function (json) {/* display message */});
Dispatcher. capture ({
"Status $ eq": 200,
"Command $ eq": "message"
}, Function (json) {/* display message */});

Obviously, the first one is more intuitive. Therefore, we need a default operator. When the operator string is "", we select an operator through the default operator.
Pattern to Filter
Finally, we need to connect operators and createFilter. This part of work is actually not difficult, as long as you call the default operator.
Copy codeThe Code is as follows:
Var createFilter = function (condition ){
Return function (json ){
If (arguments. length> 0 ){
Return operators [""] (condition, json );
} Else {
Return operators [""] (condition );
}
};
};

Why should we consider the absence of json parameters? I will tell you more in the next article. You can do it without having to do so, but there are some minor problems.
Write operators require rigor. Because Dispatcher is an encapsulated component and its operators are a little less rigorous, the defects are buried deep and difficult to find out. Therefore, we will discuss unit testing in the next article. Through unit testing, we can greatly improve the robustness of Dispatcher.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.