JSON pattern matching using JavaScript (Part 2-Implementation)

Source: Internet
Author: User
Tags switch case

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. CopyCode The 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 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 many algorithms . 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 Code the code is as follows: vaR operators ={};
operators ["LT"] = function (testvalue, value) {
return arguments. length = 2 & value };
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. You can set the subset or superset of these operators according to your actual needs:

In-traversal array to check whether at least one EQ can be found.
All-traverse the array to check whether each of them has an EQ.
ex-if a value is input, the child element exists.
re-use a regular expression to determine whether the string matches.
LD-directly calls the function for determination.
are you sure you want to write it? 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 Code the 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);
}< BR >};

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 do we need to 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.

Related Article

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.