]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/atom-space-pen-views/node_modules/fuzzaldrin/lib/scorer.js
c9198ed34aba90f670eeb62b1a1322c9284a3bf2
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / atom-space-pen-views / node_modules / fuzzaldrin / lib / scorer.js
1 (function() {
2 var PathSeparator, queryIsLastPathSegment;
3
4 PathSeparator = require('path').sep;
5
6 exports.basenameScore = function(string, query, score) {
7 var base, depth, index, lastCharacter, segmentCount, slashCount;
8 index = string.length - 1;
9 while (string[index] === PathSeparator) {
10 index--;
11 }
12 slashCount = 0;
13 lastCharacter = index;
14 base = null;
15 while (index >= 0) {
16 if (string[index] === PathSeparator) {
17 slashCount++;
18 if (base == null) {
19 base = string.substring(index + 1, lastCharacter + 1);
20 }
21 } else if (index === 0) {
22 if (lastCharacter < string.length - 1) {
23 if (base == null) {
24 base = string.substring(0, lastCharacter + 1);
25 }
26 } else {
27 if (base == null) {
28 base = string;
29 }
30 }
31 }
32 index--;
33 }
34 if (base === string) {
35 score *= 2;
36 } else if (base) {
37 score += exports.score(base, query);
38 }
39 segmentCount = slashCount + 1;
40 depth = Math.max(1, 10 - segmentCount);
41 score *= depth * 0.01;
42 return score;
43 };
44
45 exports.score = function(string, query) {
46 var character, characterScore, indexInQuery, indexInString, lowerCaseIndex, minIndex, queryLength, queryScore, stringLength, totalCharacterScore, upperCaseIndex, _ref;
47 if (string === query) {
48 return 1;
49 }
50 if (queryIsLastPathSegment(string, query)) {
51 return 1;
52 }
53 totalCharacterScore = 0;
54 queryLength = query.length;
55 stringLength = string.length;
56 indexInQuery = 0;
57 indexInString = 0;
58 while (indexInQuery < queryLength) {
59 character = query[indexInQuery++];
60 lowerCaseIndex = string.indexOf(character.toLowerCase());
61 upperCaseIndex = string.indexOf(character.toUpperCase());
62 minIndex = Math.min(lowerCaseIndex, upperCaseIndex);
63 if (minIndex === -1) {
64 minIndex = Math.max(lowerCaseIndex, upperCaseIndex);
65 }
66 indexInString = minIndex;
67 if (indexInString === -1) {
68 return 0;
69 }
70 characterScore = 0.1;
71 if (string[indexInString] === character) {
72 characterScore += 0.1;
73 }
74 if (indexInString === 0 || string[indexInString - 1] === PathSeparator) {
75 characterScore += 0.8;
76 } else if ((_ref = string[indexInString - 1]) === '-' || _ref === '_' || _ref === ' ') {
77 characterScore += 0.7;
78 }
79 string = string.substring(indexInString + 1, stringLength);
80 totalCharacterScore += characterScore;
81 }
82 queryScore = totalCharacterScore / queryLength;
83 return ((queryScore * (queryLength / stringLength)) + queryScore) / 2;
84 };
85
86 queryIsLastPathSegment = function(string, query) {
87 if (string[string.length - query.length - 1] === PathSeparator) {
88 return string.lastIndexOf(query) === string.length - query.length;
89 }
90 };
91
92 }).call(this);