2010-11-15 08:39:52 +01:00
|
|
|
/*
|
2011-11-08 17:40:44 +01:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 08:39:52 +01:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
if(!dojo._hasResource["dojo.data.util.filter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.data.util.filter"] = true;
|
2010-11-15 08:39:52 +01:00
|
|
|
dojo.provide("dojo.data.util.filter");
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2011-11-08 17:40:44 +01:00
|
|
|
dojo.getObject("data.util.filter", true, dojo);
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
dojo.data.util.filter.patternToRegExp = function(/*String*/pattern, /*boolean?*/ ignoreCase){
|
2011-11-08 17:40:44 +01:00
|
|
|
// summary:
|
2011-03-04 17:02:28 +01:00
|
|
|
// Helper function to convert a simple pattern to a regular expression for matching.
|
|
|
|
// description:
|
|
|
|
// Returns a regular expression object that conforms to the defined conversion rules.
|
2011-11-08 17:40:44 +01:00
|
|
|
// For example:
|
2011-03-04 17:02:28 +01:00
|
|
|
// ca* -> /^ca.*$/
|
|
|
|
// *ca* -> /^.*ca.*$/
|
|
|
|
// *c\*a* -> /^.*c\*a.*$/
|
|
|
|
// *c\*a?* -> /^.*c\*a..*$/
|
|
|
|
// and so on.
|
|
|
|
//
|
|
|
|
// pattern: string
|
|
|
|
// A simple matching pattern to convert that follows basic rules:
|
|
|
|
// * Means match anything, so ca* means match anything starting with ca
|
|
|
|
// ? Means match single character. So, b?b will match to bob and bab, and so on.
|
|
|
|
// \ is an escape character. So for example, \* means do not treat * as a match, but literal character *.
|
2011-11-08 17:40:44 +01:00
|
|
|
// To use a \ as a character in the string, it must be escaped. So in the pattern it should be
|
2011-03-04 17:02:28 +01:00
|
|
|
// represented by \\ to be treated as an ordinary \ character instead of an escape.
|
|
|
|
//
|
|
|
|
// ignoreCase:
|
|
|
|
// An optional flag to indicate if the pattern matching should be treated as case-sensitive or not when comparing
|
|
|
|
// By default, it is assumed case sensitive.
|
|
|
|
|
|
|
|
var rxp = "^";
|
|
|
|
var c = null;
|
|
|
|
for(var i = 0; i < pattern.length; i++){
|
|
|
|
c = pattern.charAt(i);
|
|
|
|
switch(c){
|
|
|
|
case '\\':
|
|
|
|
rxp += c;
|
|
|
|
i++;
|
|
|
|
rxp += pattern.charAt(i);
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
rxp += ".*"; break;
|
|
|
|
case '?':
|
|
|
|
rxp += "."; break;
|
|
|
|
case '$':
|
|
|
|
case '^':
|
|
|
|
case '/':
|
|
|
|
case '+':
|
|
|
|
case '.':
|
|
|
|
case '|':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
rxp += "\\"; //fallthrough
|
|
|
|
default:
|
|
|
|
rxp += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rxp += "$";
|
|
|
|
if(ignoreCase){
|
|
|
|
return new RegExp(rxp,"mi"); //RegExp
|
|
|
|
}else{
|
|
|
|
return new RegExp(rxp,"m"); //RegExp
|
|
|
|
}
|
|
|
|
|
2010-11-15 08:39:52 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2010-11-15 08:39:52 +01:00
|
|
|
}
|