]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/util/filter.js.uncompressed.js
update dojo to 1.7.3
[tt-rss.git] / lib / dojo / data / util / filter.js.uncompressed.js
1 define("dojo/data/util/filter", ["dojo/_base/lang"], function(lang) {
2 // module:
3 // dojo/data/util/filter
4 // summary:
5 // TODOC
6
7 var filter = lang.getObject("dojo.data.util.filter", true);
8
9 filter.patternToRegExp = function(/*String*/pattern, /*boolean?*/ ignoreCase){
10 // summary:
11 // Helper function to convert a simple pattern to a regular expression for matching.
12 // description:
13 // Returns a regular expression object that conforms to the defined conversion rules.
14 // For example:
15 // ca* -> /^ca.*$/
16 // *ca* -> /^.*ca.*$/
17 // *c\*a* -> /^.*c\*a.*$/
18 // *c\*a?* -> /^.*c\*a..*$/
19 // and so on.
20 //
21 // pattern: string
22 // A simple matching pattern to convert that follows basic rules:
23 // * Means match anything, so ca* means match anything starting with ca
24 // ? Means match single character. So, b?b will match to bob and bab, and so on.
25 // \ is an escape character. So for example, \* means do not treat * as a match, but literal character *.
26 // To use a \ as a character in the string, it must be escaped. So in the pattern it should be
27 // represented by \\ to be treated as an ordinary \ character instead of an escape.
28 //
29 // ignoreCase:
30 // An optional flag to indicate if the pattern matching should be treated as case-sensitive or not when comparing
31 // By default, it is assumed case sensitive.
32
33 var rxp = "^";
34 var c = null;
35 for(var i = 0; i < pattern.length; i++){
36 c = pattern.charAt(i);
37 switch(c){
38 case '\\':
39 rxp += c;
40 i++;
41 rxp += pattern.charAt(i);
42 break;
43 case '*':
44 rxp += ".*"; break;
45 case '?':
46 rxp += "."; break;
47 case '$':
48 case '^':
49 case '/':
50 case '+':
51 case '.':
52 case '|':
53 case '(':
54 case ')':
55 case '{':
56 case '}':
57 case '[':
58 case ']':
59 rxp += "\\"; //fallthrough
60 default:
61 rxp += c;
62 }
63 }
64 rxp += "$";
65 if(ignoreCase){
66 return new RegExp(rxp,"mi"); //RegExp
67 }else{
68 return new RegExp(rxp,"m"); //RegExp
69 }
70
71 };
72
73 return filter;
74 });