1
1

matcher.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. (function() {
  2. var PathSeparator;
  3. PathSeparator = require('path').sep;
  4. exports.basenameMatch = function(string, query) {
  5. var base, index, lastCharacter, slashCount;
  6. index = string.length - 1;
  7. while (string[index] === PathSeparator) {
  8. index--;
  9. }
  10. slashCount = 0;
  11. lastCharacter = index;
  12. base = null;
  13. while (index >= 0) {
  14. if (string[index] === PathSeparator) {
  15. slashCount++;
  16. if (base == null) {
  17. base = string.substring(index + 1, lastCharacter + 1);
  18. }
  19. } else if (index === 0) {
  20. if (lastCharacter < string.length - 1) {
  21. if (base == null) {
  22. base = string.substring(0, lastCharacter + 1);
  23. }
  24. } else {
  25. if (base == null) {
  26. base = string;
  27. }
  28. }
  29. }
  30. index--;
  31. }
  32. return exports.match(base, query, string.length - base.length);
  33. };
  34. exports.match = function(string, query, stringOffset) {
  35. var character, indexInQuery, indexInString, lowerCaseIndex, matches, minIndex, queryLength, stringLength, upperCaseIndex, _i, _ref, _results;
  36. if (stringOffset == null) {
  37. stringOffset = 0;
  38. }
  39. if (string === query) {
  40. return (function() {
  41. _results = [];
  42. for (var _i = stringOffset, _ref = stringOffset + string.length; stringOffset <= _ref ? _i < _ref : _i > _ref; stringOffset <= _ref ? _i++ : _i--){ _results.push(_i); }
  43. return _results;
  44. }).apply(this);
  45. }
  46. queryLength = query.length;
  47. stringLength = string.length;
  48. indexInQuery = 0;
  49. indexInString = 0;
  50. matches = [];
  51. while (indexInQuery < queryLength) {
  52. character = query[indexInQuery++];
  53. lowerCaseIndex = string.indexOf(character.toLowerCase());
  54. upperCaseIndex = string.indexOf(character.toUpperCase());
  55. minIndex = Math.min(lowerCaseIndex, upperCaseIndex);
  56. if (minIndex === -1) {
  57. minIndex = Math.max(lowerCaseIndex, upperCaseIndex);
  58. }
  59. indexInString = minIndex;
  60. if (indexInString === -1) {
  61. return [];
  62. }
  63. matches.push(stringOffset + indexInString);
  64. stringOffset += indexInString + 1;
  65. string = string.substring(indexInString + 1, stringLength);
  66. }
  67. return matches;
  68. };
  69. }).call(this);