index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* jshint node: true */
  2. 'use strict';
  3. var REGEXP_PARTS = /(\*|\?)/g;
  4. /**
  5. # wildcard
  6. Very simple wildcard matching, which is designed to provide the same
  7. functionality that is found in the
  8. [eve](https://github.com/adobe-webplatform/eve) eventing library.
  9. ## Usage
  10. It works with strings:
  11. <<< examples/strings.js
  12. Arrays:
  13. <<< examples/arrays.js
  14. Objects (matching against keys):
  15. <<< examples/objects.js
  16. ## Alternative Implementations
  17. - <https://github.com/isaacs/node-glob>
  18. Great for full file-based wildcard matching.
  19. - <https://github.com/sindresorhus/matcher>
  20. A well cared for and loved JS wildcard matcher.
  21. **/
  22. function WildcardMatcher(text, separator) {
  23. this.text = text = text || '';
  24. this.hasWild = text.indexOf('*') >= 0;
  25. this.separator = separator;
  26. this.parts = text.split(separator).map(this.classifyPart.bind(this));
  27. }
  28. WildcardMatcher.prototype.match = function(input) {
  29. var matches = true;
  30. var parts = this.parts;
  31. var ii;
  32. var partsCount = parts.length;
  33. var testParts;
  34. if (typeof input == 'string' || input instanceof String) {
  35. if (!this.hasWild && this.text != input) {
  36. matches = false;
  37. } else {
  38. testParts = (input || '').split(this.separator);
  39. for (ii = 0; matches && ii < partsCount; ii++) {
  40. if (parts[ii] === '*') {
  41. continue;
  42. } else if (ii < testParts.length) {
  43. matches = parts[ii] instanceof RegExp
  44. ? parts[ii].test(testParts[ii])
  45. : parts[ii] === testParts[ii];
  46. } else {
  47. matches = false;
  48. }
  49. }
  50. // If matches, then return the component parts
  51. matches = matches && testParts;
  52. }
  53. }
  54. else if (typeof input.splice == 'function') {
  55. matches = [];
  56. for (ii = input.length; ii--; ) {
  57. if (this.match(input[ii])) {
  58. matches[matches.length] = input[ii];
  59. }
  60. }
  61. }
  62. else if (typeof input == 'object') {
  63. matches = {};
  64. for (var key in input) {
  65. if (this.match(key)) {
  66. matches[key] = input[key];
  67. }
  68. }
  69. }
  70. return matches;
  71. };
  72. WildcardMatcher.prototype.classifyPart = function(part) {
  73. // in the event that we have been provided a part that is not just a wildcard
  74. // then turn this into a regular expression for matching purposes
  75. if (part === '*') {
  76. return part;
  77. } else if (part.indexOf('*') >= 0 || part.indexOf('?') >= 0) {
  78. return new RegExp(part.replace(REGEXP_PARTS, '\.$1'));
  79. }
  80. return part;
  81. };
  82. module.exports = function(text, test, separator) {
  83. var matcher = new WildcardMatcher(text, separator || /[\/\.]/);
  84. if (typeof test != 'undefined') {
  85. return matcher.match(test);
  86. }
  87. return matcher;
  88. };