-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from DenizUgur/main
Specification coverage component enhancements
- Loading branch information
Showing
20 changed files
with
22,261 additions
and
12,461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { FuseSearchWithScore } from "@/types"; | ||
|
||
/** | ||
* Normalize the scores of the given results. | ||
* @param results - the results to normalize | ||
*/ | ||
function normalizeResultScores<T>(results: FuseSearchWithScore<T>[], threshold = 0.2) { | ||
let normalizedResults = results; | ||
if (results.length === 1) { | ||
normalizedResults = results.map((result) => ({ | ||
...result, | ||
actualScore: result.score, | ||
score: 0 | ||
})); | ||
} else if (results.length > 1) { | ||
const minScore = results[0].score; | ||
const maxScore = results[results.length - 1].score; | ||
|
||
if (minScore === maxScore) { | ||
normalizedResults = results.map((result) => ({ | ||
...result, | ||
actualScore: result.score, | ||
score: minScore < threshold ? 0 : 1 | ||
})); | ||
} else { | ||
normalizedResults = results.map((result) => ({ | ||
...result, | ||
actualScore: result.score, | ||
score: ((result.score || 0) - minScore) / (maxScore - minScore) | ||
})); | ||
} | ||
} | ||
|
||
return normalizedResults; | ||
} | ||
|
||
/** | ||
* Add ordinal suffix to the given number. | ||
* @param num - the number to add the suffix to | ||
* @link https://stackoverflow.com/a/15397495 | ||
*/ | ||
function addOrdinalSuffix(num: number) { | ||
const dString = String(num); | ||
const last = +dString.slice(-2); | ||
if (last > 3 && last < 21) return `${num}th`; | ||
switch (last % 10) { | ||
case 1: | ||
return `${num}st`; | ||
case 2: | ||
return `${num}nd`; | ||
case 3: | ||
return `${num}rd`; | ||
default: | ||
return `${num}th`; | ||
} | ||
} | ||
|
||
export { normalizeResultScores, addOrdinalSuffix }; |
Oops, something went wrong.