@samual/match
Version:
A fork of a fork of match-sorter with separated filtering, sorting phases, and match locations.
78 lines (77 loc) • 2.32 kB
TypeScript
/**
* @name match-sorter
* @license MIT license.
* @copyright (c) 2099 Kent C. Dodds
* @author Kent C. Dodds <me@kentcdodds.com> (https://kentcdodds.com)
*/
/***/
export type AccessorAttributes = {
threshold?: Ranking;
maxRanking: Ranking;
minRanking: Ranking;
};
export type RankingInfo = MatchRanking & {
rankedValue: any;
rank: Ranking;
accessorIndex: number;
accessorThreshold: Ranking | undefined;
passed: boolean;
};
export interface AccessorOptions<TItem> {
accessor: AccessorFn<TItem>;
threshold?: Ranking;
maxRanking?: Ranking;
minRanking?: Ranking;
}
export type AccessorFn<TItem> = (item: TItem) => string | Array<string>;
export type Accessor<TItem> = AccessorFn<TItem> | AccessorOptions<TItem>;
export interface RankItemOptions<TItem = unknown> {
accessors?: ReadonlyArray<Accessor<TItem>>;
threshold?: Ranking;
keepDiacritics?: boolean;
}
export declare const rankings: {
readonly CASE_SENSITIVE_EQUAL: 7;
readonly EQUAL: 6;
readonly STARTS_WITH: 5;
readonly WORD_STARTS_WITH: 4;
readonly CONTAINS: 3;
readonly ACRONYM: 2;
readonly MATCHES: 1;
readonly NO_MATCH: 0;
};
export type Ranking = (typeof rankings)[keyof typeof rankings];
/**
* Gets the highest ranking for value for the given item based on its values for the given keys
* @param {*} item - the item to rank
* @param {String} value - the value to rank against
* @param {Object} options - options to control the ranking
* @return {{rank: Number, accessorIndex: Number, accessorThreshold: Number}} - the highest ranking
*/
export declare function rankItem<TItem>(item: TItem, value: string, options?: RankItemOptions<TItem>): RankingInfo;
type MatchRanking = {
rank: 0 | 6 | 7;
} | {
rank: 1;
index: number;
length: number;
closeness: number;
} | {
rank: 2;
indexes: number[];
} | {
rank: 3 | 4;
index: number;
length: number;
} | {
rank: 5;
length: number;
};
/**
* Sorts items that have a rank, index, and accessorIndex
* @param {Object} a - the first item to sort
* @param {Object} b - the second item to sort
* @return {Number} -1 if a should come first, 1 if b should come first, 0 if equal
*/
export declare function compareItems<TItem>(a: RankingInfo, b: RankingInfo): number;
export {};