|
(function() { |
|
var AcronymResult, PathSeparator, Query, basenameScore, coreChars, countDir, doScore, emptyAcronymResult, isMatch, isSeparator, isWordEnd, isWordStart, miss_coeff, opt_char_re, pos_bonus, scoreAcronyms, scoreCharacter, scoreConsecutives, scoreExact, scoreExactMatch, scorePattern, scorePosition, scoreSize, tau_depth, tau_size, wm; |
|
|
|
PathSeparator = require('path').sep; |
|
|
|
wm = 150; |
|
|
|
pos_bonus = 20; |
|
|
|
tau_size = 50; |
|
|
|
tau_depth = 13; |
|
|
|
miss_coeff = 0.75; |
|
|
|
opt_char_re = /[ _\-:\/\\]/g; |
|
|
|
exports.coreChars = coreChars = function(query) { |
|
return query.replace(opt_char_re, ''); |
|
}; |
|
|
|
exports.score = function(string, query, prepQuery, allowErrors) { |
|
var score, string_lw; |
|
if (prepQuery == null) { |
|
prepQuery = new Query(query); |
|
} |
|
if (allowErrors == null) { |
|
allowErrors = false; |
|
} |
|
if (!(allowErrors || isMatch(string, prepQuery.core_lw, prepQuery.core_up))) { |
|
return 0; |
|
} |
|
string_lw = string.toLowerCase(); |
|
|
|
|
|
// --------- EDITS -------------------- |
|
// if we're dealing with an index file. we don't want to artificially |
|
// bump all indexes above good matches that aren't to index files |
|
var regexp = new RegExp("/index.(j|t)sx?$") |
|
var isIndexFile = string.match(regexp) |
|
if (isIndexFile) { |
|
// we're going to score using a string without index at the end |
|
// to artificially increase the score |
|
var withoutIndex = string.replace(regexp, "") |
|
|
|
// further, (only in this index case) we're scoring on just the last |
|
// directory name. "/path/to/thing/index.tsx" only gets matched to "thing" |
|
// TODO: this is probably too extreme but it seems to work alright so far |
|
// without totally breaking existing useful matches |
|
var lastPart = withoutIndex.match(/\/(?:.(?!\/))+$/g)[0] |
|
var lastPart_lw = lastPart.toLowerCase() |
|
|
|
score = doScore( |
|
lastPart, |
|
lastPart_lw, |
|
prepQuery |
|
); |
|
adjustScore = Math.ceil(basenameScore(lastPart, lastPart_lw, prepQuery, score)); |
|
} else { |
|
score = doScore(string, string_lw, prepQuery); |
|
adjustScore = Math.ceil(basenameScore(string, string_lw, prepQuery, score)); |
|
} |
|
console.log(adjustScore, string) |
|
|
|
return adjustScore |
|
// --------- EDITS -------------------- |
|
}; |
|
|
|
Query = (function() { |
|
function Query(query) { |
|
if (!(query != null ? query.length : void 0)) { |
|
return null; |
|
} |
|
this.query = query; |
|
this.query_lw = query.toLowerCase(); |
|
this.core = coreChars(query); |
|
this.core_lw = this.core.toLowerCase(); |
|
this.core_up = this.core.toUpperCase(); |
|
this.depth = countDir(query, query.length); |
|
} |
|
|
|
return Query; |
|
|
|
})(); |
|
|
|
exports.prepQuery = function(query) { |
|
return new Query(query); |
|
}; |
|
|
|
exports.isMatch = isMatch = function(subject, query_lw, query_up) { |
|
var i, j, m, n, qj_lw, qj_up, si; |
|
m = subject.length; |
|
n = query_lw.length; |
|
if (!m || !n || n > m) { |
|
return false; |
|
} |
|
i = -1; |
|
j = -1; |
|
while (++j < n) { |
|
qj_lw = query_lw[j]; |
|
qj_up = query_up[j]; |
|
while (++i < m) { |
|
si = subject[i]; |
|
if (si === qj_lw || si === qj_up) { |
|
break; |
|
} |
|
} |
|
if (i === m) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
}; |
|
|
|
doScore = function(subject, subject_lw, prepQuery) { |
|
var acro, acro_score, align, csc_diag, csc_row, csc_score, i, j, m, miss_budget, miss_left, mm, n, pos, query, query_lw, record_miss, score, score_diag, score_row, score_up, si_lw, start, sz; |
|
query = prepQuery.query; |
|
query_lw = prepQuery.query_lw; |
|
m = subject.length; |
|
n = query.length; |
|
acro = scoreAcronyms(subject, subject_lw, query, query_lw); |
|
acro_score = acro.score; |
|
if (acro.count === n) { |
|
return scoreExact(n, m, acro_score, acro.pos); |
|
} |
|
pos = subject_lw.indexOf(query_lw); |
|
if (pos > -1) { |
|
return scoreExactMatch(subject, subject_lw, query, query_lw, pos, n, m); |
|
} |
|
score_row = new Array(n); |
|
csc_row = new Array(n); |
|
sz = scoreSize(n, m); |
|
miss_budget = Math.ceil(miss_coeff * n) + 5; |
|
miss_left = miss_budget; |
|
j = -1; |
|
while (++j < n) { |
|
score_row[j] = 0; |
|
csc_row[j] = 0; |
|
} |
|
i = subject_lw.indexOf(query_lw[0]); |
|
if (i > -1) { |
|
i--; |
|
} |
|
mm = subject_lw.lastIndexOf(query_lw[n - 1], m); |
|
if (mm > i) { |
|
m = mm + 1; |
|
} |
|
while (++i < m) { |
|
score = 0; |
|
score_diag = 0; |
|
csc_diag = 0; |
|
si_lw = subject_lw[i]; |
|
record_miss = true; |
|
j = -1; |
|
while (++j < n) { |
|
score_up = score_row[j]; |
|
if (score_up > score) { |
|
score = score_up; |
|
} |
|
csc_score = 0; |
|
if (query_lw[j] === si_lw) { |
|
start = isWordStart(i, subject, subject_lw); |
|
csc_score = csc_diag > 0 ? csc_diag : scoreConsecutives(subject, subject_lw, query, query_lw, i, j, start); |
|
align = score_diag + scoreCharacter(i, j, start, acro_score, csc_score); |
|
if (align > score) { |
|
score = align; |
|
miss_left = miss_budget; |
|
} else { |
|
if (record_miss && --miss_left <= 0) { |
|
return score_row[n - 1] * sz; |
|
} |
|
record_miss = false; |
|
} |
|
} |
|
score_diag = score_up; |
|
csc_diag = csc_row[j]; |
|
csc_row[j] = csc_score; |
|
score_row[j] = score; |
|
} |
|
} |
|
return score * sz; |
|
}; |
|
|
|
exports.isWordStart = isWordStart = function(pos, subject, subject_lw) { |
|
var curr_s, prev_s; |
|
if (pos < 0) { |
|
return false; |
|
} |
|
if (pos === 0) { |
|
return true; |
|
} |
|
curr_s = subject[pos]; |
|
prev_s = subject[pos - 1]; |
|
return isSeparator(curr_s) || isSeparator(prev_s) || (curr_s !== subject_lw[pos] && prev_s === subject_lw[pos - 1]); |
|
}; |
|
|
|
exports.isWordEnd = isWordEnd = function(pos, subject, subject_lw, len) { |
|
var next_s; |
|
if (pos > len - 1) { |
|
return false; |
|
} |
|
if (pos === len - 1) { |
|
return true; |
|
} |
|
next_s = subject[pos + 1]; |
|
return isSeparator(next_s) || (subject[pos] === subject_lw[pos] && next_s !== subject_lw[pos + 1]); |
|
}; |
|
|
|
isSeparator = function(c) { |
|
return c === ' ' || c === '.' || c === '-' || c === '_' || c === '/' || c === '\\'; |
|
}; |
|
|
|
scorePosition = function(pos) { |
|
var sc; |
|
if (pos < pos_bonus) { |
|
sc = pos_bonus - pos; |
|
return 100 + sc * sc; |
|
} else { |
|
return 100 + pos_bonus - pos; |
|
} |
|
}; |
|
|
|
scoreSize = function(n, m) { |
|
return tau_size / (tau_size + Math.abs(m - n)); |
|
}; |
|
|
|
scoreExact = function(n, m, quality, pos) { |
|
return 2 * n * (wm * quality + scorePosition(pos)) * scoreSize(n, m); |
|
}; |
|
|
|
exports.scorePattern = scorePattern = function(count, len, sameCase, start, end) { |
|
var bonus, sz; |
|
sz = count; |
|
bonus = 6; |
|
if (sameCase === count) { |
|
bonus += 2; |
|
} |
|
if (start) { |
|
bonus += 3; |
|
} |
|
if (end) { |
|
bonus += 1; |
|
} |
|
if (count === len) { |
|
if (start) { |
|
if (sameCase === len) { |
|
sz += 2; |
|
} else { |
|
sz += 1; |
|
} |
|
} |
|
if (end) { |
|
bonus += 1; |
|
} |
|
} |
|
return sameCase + sz * (sz + bonus); |
|
}; |
|
|
|
exports.scoreCharacter = scoreCharacter = function(i, j, start, acro_score, csc_score) { |
|
var posBonus; |
|
posBonus = scorePosition(i); |
|
if (start) { |
|
return posBonus + wm * ((acro_score > csc_score ? acro_score : csc_score) + 10); |
|
} |
|
return posBonus + wm * csc_score; |
|
}; |
|
|
|
exports.scoreConsecutives = scoreConsecutives = function(subject, subject_lw, query, query_lw, i, j, start) { |
|
var k, m, mi, n, nj, sameCase, startPos, sz; |
|
m = subject.length; |
|
n = query.length; |
|
mi = m - i; |
|
nj = n - j; |
|
k = mi < nj ? mi : nj; |
|
startPos = i; |
|
sameCase = 0; |
|
sz = 0; |
|
if (query[j] === subject[i]) { |
|
sameCase++; |
|
} |
|
while (++sz < k && query_lw[++j] === subject_lw[++i]) { |
|
if (query[j] === subject[i]) { |
|
sameCase++; |
|
} |
|
} |
|
if (sz === 1) { |
|
return 1 + 2 * sameCase; |
|
} |
|
return scorePattern(sz, n, sameCase, start, isWordEnd(i, subject, subject_lw, m)); |
|
}; |
|
|
|
exports.scoreExactMatch = scoreExactMatch = function(subject, subject_lw, query, query_lw, pos, n, m) { |
|
var end, i, pos2, sameCase, start; |
|
start = isWordStart(pos, subject, subject_lw); |
|
if (!start) { |
|
pos2 = subject_lw.indexOf(query_lw, pos + 1); |
|
if (pos2 > -1) { |
|
start = isWordStart(pos2, subject, subject_lw); |
|
if (start) { |
|
pos = pos2; |
|
} |
|
} |
|
} |
|
i = -1; |
|
sameCase = 0; |
|
while (++i < n) { |
|
if (query[pos + i] === subject[i]) { |
|
sameCase++; |
|
} |
|
} |
|
end = isWordEnd(pos + n - 1, subject, subject_lw, m); |
|
return scoreExact(n, m, scorePattern(n, n, sameCase, start, end), pos); |
|
}; |
|
|
|
AcronymResult = (function() { |
|
function AcronymResult(score, pos, count) { |
|
this.score = score; |
|
this.pos = pos; |
|
this.count = count; |
|
} |
|
|
|
return AcronymResult; |
|
|
|
})(); |
|
|
|
emptyAcronymResult = new AcronymResult(0, 0.1, 0); |
|
|
|
exports.scoreAcronyms = scoreAcronyms = function(subject, subject_lw, query, query_lw) { |
|
var count, i, j, m, n, pos, qj_lw, sameCase, score; |
|
m = subject.length; |
|
n = query.length; |
|
if (!(m > 1 && n > 1)) { |
|
return emptyAcronymResult; |
|
} |
|
count = 0; |
|
pos = 0; |
|
sameCase = 0; |
|
i = -1; |
|
j = -1; |
|
while (++j < n) { |
|
qj_lw = query_lw[j]; |
|
while (++i < m) { |
|
if (qj_lw === subject_lw[i] && isWordStart(i, subject, subject_lw)) { |
|
if (query[j] === subject[i]) { |
|
sameCase++; |
|
} |
|
pos += i; |
|
count++; |
|
break; |
|
} |
|
} |
|
if (i === m) { |
|
break; |
|
} |
|
} |
|
if (count < 2) { |
|
return emptyAcronymResult; |
|
} |
|
score = scorePattern(count, n, sameCase, true, false); |
|
return new AcronymResult(score, pos / count, count); |
|
}; |
|
|
|
basenameScore = function(subject, subject_lw, prepQuery, fullPathScore) { |
|
var alpha, basePathScore, basePos, depth, end; |
|
if (fullPathScore === 0) { |
|
return 0; |
|
} |
|
end = subject.length - 1; |
|
while (subject[end] === PathSeparator) { |
|
end--; |
|
} |
|
basePos = subject.lastIndexOf(PathSeparator, end); |
|
if (basePos === -1) { |
|
return fullPathScore; |
|
} |
|
depth = prepQuery.depth; |
|
while (depth-- > 0) { |
|
basePos = subject.lastIndexOf(PathSeparator, basePos - 1); |
|
if (basePos === -1) { |
|
return fullPathScore; |
|
} |
|
} |
|
basePos++; |
|
end++; |
|
basePathScore = doScore(subject.slice(basePos, end), subject_lw.slice(basePos, end), prepQuery); |
|
alpha = 0.5 * tau_depth / (tau_depth + countDir(subject, end + 1)); |
|
return alpha * basePathScore + (1 - alpha) * fullPathScore * scoreSize(0, 0.5 * (end - basePos)); |
|
}; |
|
|
|
exports.countDir = countDir = function(path, end) { |
|
var count, i; |
|
if (end < 1) { |
|
return 0; |
|
} |
|
count = 0; |
|
i = -1; |
|
while (++i < end) { |
|
if (path[i] === PathSeparator) { |
|
++count; |
|
while (++i < end && path[i] === PathSeparator) { |
|
continue; |
|
} |
|
} |
|
} |
|
return count; |
|
}; |
|
|
|
}).call(this); |