fuzzaldrin.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. (function() {
  2. var PathSeparator, SpaceRegex, filter, matcher, scorer;
  3. scorer = require('./scorer');
  4. filter = require('./filter');
  5. matcher = require('./matcher');
  6. PathSeparator = require('path').sep;
  7. SpaceRegex = /\ /g;
  8. module.exports = {
  9. filter: function(candidates, query, options) {
  10. var queryHasSlashes;
  11. if (query) {
  12. queryHasSlashes = query.indexOf(PathSeparator) !== -1;
  13. query = query.replace(SpaceRegex, '');
  14. }
  15. return filter(candidates, query, queryHasSlashes, options);
  16. },
  17. score: function(string, query) {
  18. var queryHasSlashes, score;
  19. if (!string) {
  20. return 0;
  21. }
  22. if (!query) {
  23. return 0;
  24. }
  25. if (string === query) {
  26. return 2;
  27. }
  28. queryHasSlashes = query.indexOf(PathSeparator) !== -1;
  29. query = query.replace(SpaceRegex, '');
  30. score = scorer.score(string, query);
  31. if (!queryHasSlashes) {
  32. score = scorer.basenameScore(string, query, score);
  33. }
  34. return score;
  35. },
  36. match: function(string, query) {
  37. var baseMatches, index, matches, queryHasSlashes, seen, _i, _ref, _results;
  38. if (!string) {
  39. return [];
  40. }
  41. if (!query) {
  42. return [];
  43. }
  44. if (string === query) {
  45. return (function() {
  46. _results = [];
  47. for (var _i = 0, _ref = string.length; 0 <= _ref ? _i < _ref : _i > _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
  48. return _results;
  49. }).apply(this);
  50. }
  51. queryHasSlashes = query.indexOf(PathSeparator) !== -1;
  52. query = query.replace(SpaceRegex, '');
  53. matches = matcher.match(string, query);
  54. if (!queryHasSlashes) {
  55. baseMatches = matcher.basenameMatch(string, query);
  56. matches = matches.concat(baseMatches).sort(function(a, b) {
  57. return a - b;
  58. });
  59. seen = null;
  60. index = 0;
  61. while (index < matches.length) {
  62. if (index && seen === matches[index]) {
  63. matches.splice(index, 1);
  64. } else {
  65. seen = matches[index];
  66. index++;
  67. }
  68. }
  69. }
  70. return matches;
  71. }
  72. };
  73. }).call(this);