Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restrict threshold/ranking type to known values #122

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import removeAccents from 'remove-accents'

type KeyAttributes = {
threshold?: number
maxRanking: number
minRanking: number
threshold?: Ranking
maxRanking: Ranking
minRanking: Ranking
}
interface RankingInfo {
rankedValue: string
rank: number
rank: Ranking
keyIndex: number
keyThreshold: number | undefined
keyThreshold: Ranking | undefined
}

interface ValueGetterKey<ItemType> {
Expand All @@ -33,9 +33,9 @@ interface BaseSorter<ItemType> {

interface KeyAttributesOptions<ItemType> {
key?: string | ValueGetterKey<ItemType>
threshold?: number
maxRanking?: number
minRanking?: number
threshold?: Ranking
maxRanking?: Ranking
minRanking?: Ranking
}

type KeyOption<ItemType> =
Expand All @@ -45,7 +45,7 @@ type KeyOption<ItemType> =

interface MatchSorterOptions<ItemType = unknown> {
keys?: Array<KeyOption<ItemType>>
threshold?: number
threshold?: Ranking
baseSort?: BaseSorter<ItemType>
keepDiacritics?: boolean
}
Expand All @@ -60,7 +60,9 @@ const rankings = {
ACRONYM: 2,
MATCHES: 1,
NO_MATCH: 0,
}
} as const

type Ranking = typeof rankings[keyof typeof rankings]

matchSorter.rankings = rankings

Expand Down Expand Up @@ -153,7 +155,7 @@ function getHighestRanking<ItemType>(
},
{
rankedValue: (item as unknown) as string,
rank: rankings.NO_MATCH,
rank: rankings.NO_MATCH as Ranking,
keyIndex: -1,
keyThreshold: options.threshold,
},
Expand All @@ -171,7 +173,7 @@ function getMatchRanking<ItemType>(
testString: string,
stringToRank: string,
options: MatchSorterOptions<ItemType>,
): number {
): Ranking {
testString = prepareValueForComparison(testString, options)
stringToRank = prepareValueForComparison(stringToRank, options)

Expand Down Expand Up @@ -252,7 +254,10 @@ function getAcronym(string: string): string {
* @returns {Number} the number between rankings.MATCHES and
* rankings.MATCHES + 1 for how well stringToRank matches testString
*/
function getClosenessRanking(testString: string, stringToRank: string): number {
function getClosenessRanking(
testString: string,
stringToRank: string,
): Ranking {
let matchingInOrderCharCount = 0
let charNumber = 0
function findMatchingCharacter(
Expand All @@ -273,7 +278,7 @@ function getClosenessRanking(testString: string, stringToRank: string): number {
const spreadPercentage = 1 / spread
const inOrderPercentage = matchingInOrderCharCount / stringToRank.length
const ranking = rankings.MATCHES + inOrderPercentage * spreadPercentage
return ranking
return ranking as Ranking
}
const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0)
if (firstIndex < 0) {
Expand Down Expand Up @@ -437,7 +442,7 @@ function getAllValuesToRank<ItemType>(
item: ItemType,
keys: Array<KeyOption<ItemType>>,
) {
const allValues: Array<{itemValue: string, attributes: KeyAttributes}> = []
const allValues: Array<{itemValue: string; attributes: KeyAttributes}> = []
for (let j = 0, J = keys.length; j < J; j++) {
const key = keys[j]
const attributes = getKeyAttributes(key)
Expand All @@ -453,8 +458,8 @@ function getAllValuesToRank<ItemType>(
}

const defaultKeyAttributes = {
maxRanking: Infinity,
minRanking: -Infinity,
maxRanking: Infinity as Ranking,
minRanking: -Infinity as Ranking,
}
/**
* Gets all the attributes for the given key
Expand Down