93 lines
2.8 KiB
JavaScript
93 lines
2.8 KiB
JavaScript
(function() {
|
|
var PathSeparator, queryIsLastPathSegment;
|
|
|
|
PathSeparator = require('path').sep;
|
|
|
|
exports.basenameScore = function(string, query, score) {
|
|
var base, depth, index, lastCharacter, segmentCount, slashCount;
|
|
index = string.length - 1;
|
|
while (string[index] === PathSeparator) {
|
|
index--;
|
|
}
|
|
slashCount = 0;
|
|
lastCharacter = index;
|
|
base = null;
|
|
while (index >= 0) {
|
|
if (string[index] === PathSeparator) {
|
|
slashCount++;
|
|
if (base == null) {
|
|
base = string.substring(index + 1, lastCharacter + 1);
|
|
}
|
|
} else if (index === 0) {
|
|
if (lastCharacter < string.length - 1) {
|
|
if (base == null) {
|
|
base = string.substring(0, lastCharacter + 1);
|
|
}
|
|
} else {
|
|
if (base == null) {
|
|
base = string;
|
|
}
|
|
}
|
|
}
|
|
index--;
|
|
}
|
|
if (base === string) {
|
|
score *= 2;
|
|
} else if (base) {
|
|
score += exports.score(base, query);
|
|
}
|
|
segmentCount = slashCount + 1;
|
|
depth = Math.max(1, 10 - segmentCount);
|
|
score *= depth * 0.01;
|
|
return score;
|
|
};
|
|
|
|
exports.score = function(string, query) {
|
|
var character, characterScore, indexInQuery, indexInString, lowerCaseIndex, minIndex, queryLength, queryScore, stringLength, totalCharacterScore, upperCaseIndex, _ref;
|
|
if (string === query) {
|
|
return 1;
|
|
}
|
|
if (queryIsLastPathSegment(string, query)) {
|
|
return 1;
|
|
}
|
|
totalCharacterScore = 0;
|
|
queryLength = query.length;
|
|
stringLength = string.length;
|
|
indexInQuery = 0;
|
|
indexInString = 0;
|
|
while (indexInQuery < queryLength) {
|
|
character = query[indexInQuery++];
|
|
lowerCaseIndex = string.indexOf(character.toLowerCase());
|
|
upperCaseIndex = string.indexOf(character.toUpperCase());
|
|
minIndex = Math.min(lowerCaseIndex, upperCaseIndex);
|
|
if (minIndex === -1) {
|
|
minIndex = Math.max(lowerCaseIndex, upperCaseIndex);
|
|
}
|
|
indexInString = minIndex;
|
|
if (indexInString === -1) {
|
|
return 0;
|
|
}
|
|
characterScore = 0.1;
|
|
if (string[indexInString] === character) {
|
|
characterScore += 0.1;
|
|
}
|
|
if (indexInString === 0 || string[indexInString - 1] === PathSeparator) {
|
|
characterScore += 0.8;
|
|
} else if ((_ref = string[indexInString - 1]) === '-' || _ref === '_' || _ref === ' ') {
|
|
characterScore += 0.7;
|
|
}
|
|
string = string.substring(indexInString + 1, stringLength);
|
|
totalCharacterScore += characterScore;
|
|
}
|
|
queryScore = totalCharacterScore / queryLength;
|
|
return ((queryScore * (queryLength / stringLength)) + queryScore) / 2;
|
|
};
|
|
|
|
queryIsLastPathSegment = function(string, query) {
|
|
if (string[string.length - query.length - 1] === PathSeparator) {
|
|
return string.lastIndexOf(query) === string.length - query.length;
|
|
}
|
|
};
|
|
|
|
}).call(this);
|