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

http://aipo.googlecode.com/ · JavaScript · 73 lines · 46 code · 5 blank · 22 comment · 3 complexity · efc0d7968d9615bf358a0dfbf3042deb MD5 · raw file

  1. dojo._xdResourceLoaded({
  2. depends: [["provide", "dojo.data.util.filter"]],
  3. defineResource: function(dojo){if(!dojo._hasResource["dojo.data.util.filter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  4. dojo._hasResource["dojo.data.util.filter"] = true;
  5. dojo.provide("dojo.data.util.filter");
  6. dojo.data.util.filter.patternToRegExp = function(/*String*/pattern, /*boolean?*/ ignoreCase){
  7. // summary:
  8. // Helper function to convert a simple pattern to a regular expression for matching.
  9. // description:
  10. // Returns a regular expression object that conforms to the defined conversion rules.
  11. // For example:
  12. // ca* -> /^ca.*$/
  13. // *ca* -> /^.*ca.*$/
  14. // *c\*a* -> /^.*c\*a.*$/
  15. // *c\*a?* -> /^.*c\*a..*$/
  16. // and so on.
  17. //
  18. // pattern: string
  19. // A simple matching pattern to convert that follows basic rules:
  20. // * Means match anything, so ca* means match anything starting with ca
  21. // ? Means match single character. So, b?b will match to bob and bab, and so on.
  22. // \ is an escape character. So for example, \* means do not treat * as a match, but literal character *.
  23. // To use a \ as a character in the string, it must be escaped. So in the pattern it should be
  24. // represented by \\ to be treated as an ordinary \ character instead of an escape.
  25. //
  26. // ignoreCase:
  27. // An optional flag to indicate if the pattern matching should be treated as case-sensitive or not when comparing
  28. // By default, it is assumed case sensitive.
  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. }});