Skip to content

Commit

Permalink
fix: Make sure suggestions are within limits (#2428)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Feb 9, 2022
1 parent 6b06f92 commit d9f7943
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const defaultAdjustments: PenaltyAdjustment[] = [
},
{
id: 'short-compounds-1',
regexp: /∙.{1,2}(?=∙|$)/gu,
regexp: /^[^∙]{0,2}(?=∙)|∙[^∙]{0,2}(?=∙|$)/gm,
penalty: 100,
},
{
id: 'short-compounds-3',
regexp: /∙.{3}(?=∙|$)/gu,
regexp: /^[^∙]{3}(?=∙)|∙[^∙]{3}(?=∙|$)/gm,
penalty: 50,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Validate suggestCollector', () => {
${'aple'} | ${[s('apple', 55), s('apples', 155)]}
${'cafe'} | ${[s('cafe', 0), s('café', 1), s('cafés', 101)]}
${'tim'} | ${[s('time', 75)]}
${'runtime'} | ${[s('runtime', 50, 'run|time'), s('time', 200 + 75 + 4)]}
${'runtime'} | ${[s('runtime', 100, 'run|time'), s('time', 200 + 75 + 4)]}
`('collect weighted suggestions for "$word"', ({ word, expected }) => {
const collector = suggestionCollector(
word,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ export function suggestionCollector(wordToMatch: string, options: SuggestionColl
const sorted = values.sort(compSuggestionResults).map(cleanCompoundResult);
let i = Math.min(sorted.length, numSuggestions) - 1;
const limit = includeTies ? sorted.length : Math.min(sorted.length, numSuggestions);
const maxCost = sorted[i].cost;
for (++i; i < limit && sorted[i].cost === maxCost; ++i) {
const iCost = sorted[i].cost;
const maxCost = Math.min(iCost, weightMap ? changeLimit * BASE_COST - 1 : iCost);
for (i = 1; i < limit && sorted[i].cost <= maxCost; ++i) {
// loop
}
sorted.length = i;
Expand Down

0 comments on commit d9f7943

Please sign in to comment.