scorer.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function() {
  2. var PathSeparator, queryIsLastPathSegment;
  3. PathSeparator = require('path').sep;
  4. exports.basenameScore = function(string, query, score) {
  5. var base, depth, index, lastCharacter, segmentCount, 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. if (base === string) {
  33. score *= 2;
  34. } else if (base) {
  35. score += exports.score(base, query);
  36. }
  37. segmentCount = slashCount + 1;
  38. depth = Math.max(1, 10 - segmentCount);
  39. score *= depth * 0.01;
  40. return score;
  41. };
  42. exports.score = function(string, query) {
  43. var character, characterScore, indexInQuery, indexInString, lowerCaseIndex, minIndex, queryLength, queryScore, stringLength, totalCharacterScore, upperCaseIndex, _ref;
  44. if (string === query) {
  45. return 1;
  46. }
  47. if (queryIsLastPathSegment(string, query)) {
  48. return 1;
  49. }
  50. totalCharacterScore = 0;
  51. queryLength = query.length;
  52. stringLength = string.length;
  53. indexInQuery = 0;
  54. indexInString = 0;
  55. while (indexInQuery < queryLength) {
  56. character = query[indexInQuery++];
  57. lowerCaseIndex = string.indexOf(character.toLowerCase());
  58. upperCaseIndex = string.indexOf(character.toUpperCase());
  59. minIndex = Math.min(lowerCaseIndex, upperCaseIndex);
  60. if (minIndex === -1) {
  61. minIndex = Math.max(lowerCaseIndex, upperCaseIndex);
  62. }
  63. indexInString = minIndex;
  64. if (indexInString === -1) {
  65. return 0;
  66. }
  67. characterScore = 0.1;
  68. if (string[indexInString] === character) {
  69. characterScore += 0.1;
  70. }
  71. if (indexInString === 0 || string[indexInString - 1] === PathSeparator) {
  72. characterScore += 0.8;
  73. } else if ((_ref = string[indexInString - 1]) === '-' || _ref === '_' || _ref === ' ') {
  74. characterScore += 0.7;
  75. }
  76. string = string.substring(indexInString + 1, stringLength);
  77. totalCharacterScore += characterScore;
  78. }
  79. queryScore = totalCharacterScore / queryLength;
  80. return ((queryScore * (queryLength / stringLength)) + queryScore) / 2;
  81. };
  82. queryIsLastPathSegment = function(string, query) {
  83. if (string[string.length - query.length - 1] === PathSeparator) {
  84. return string.lastIndexOf(query) === string.length - query.length;
  85. }
  86. };
  87. }).call(this);