Skip to content

Commit

Permalink
hasstrongterminal
Browse files Browse the repository at this point in the history
  • Loading branch information
callionica committed Aug 7, 2024
1 parent e4f79ee commit 2d7f5e2
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion colors/try-hard.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,33 @@ function pathToValues(path, result) {
*/
function compareValueMatch(a, b) {

/**
* @param { ValueMatch } a
* @returns
*/
function hasStrongTerminal(a) {
return a.matches.some(m => m.isTerminal && m.path.length >= match.word.length - 1);
}

/**
* @param { (match: Match) => number } fn
* @returns
*/
function score(fn) {
/** @type { (previous: number, match: Match) => number } */
const fn2 = (previous, match) => previous + fn(match);
const scoreA = a.matches.reduce(fn2, 0);
const scoreB = b.matches.reduce(fn2, 0);
return scoreB - scoreA; // higher score ordered first by default
}

if (hasStrongTerminal(a) !== hasStrongTerminal(b)) {
return hasStrongTerminal(a) ? -1 : +1;
}

// The primary score is how many letters across all words were matched
// with a boost for a terminal match
const lengthScore = score(match => match.path.length + (match.isTerminal ? 1 : 0));
const lengthScore = score(match => match.path.length + (match.isTerminal && (match.path.length >= match.word.length - 1) ? 1 : 0));
if (lengthScore !== 0) {
return lengthScore;
}
Expand Down

0 comments on commit 2d7f5e2

Please sign in to comment.