/tags/release/7000/aipo/war/src/main/webapp/javascript/dojo/data/util/filter.js

http://aipo.googlecode.com/ · JavaScript · 69 lines · 43 code · 4 blank · 22 comment · 4 complexity · f8fcc43a1f69ee1efd0c05ed98d1ab18 MD5 · raw file

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