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