[@types/match-sorter] Add support for string[] to the property callback syntax. (#40850)

* Add support for string[] to the property callback syntax.

* Update header version number

* Fix version header

* Improve types and tests

Use KeyOptions type as suggested by ChrisRu in pull request #40850.
This commit is contained in:
rriski
2019-12-19 22:38:10 -08:00
committed by Daniel Rosenwasser
parent 0afd3fb719
commit 13bafa8b23
2 changed files with 32 additions and 9 deletions
+9 -9
View File
@@ -1,7 +1,8 @@
// Type definitions for match-sorter 2.3
// Type definitions for match-sorter 4.0
// Project: https://github.com/kentcdodds/match-sorter#readme
// Definitions by: Claas Ahlrichs <https://github.com/claasahl>
// Christian Ruigrok <https://github.com/chrisru>
// Timo Riski <https://github.com/rriski>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
@@ -20,13 +21,16 @@ declare namespace matchSorter {
}
}
type ExtendedKeyOptions<T> = { key: string | ((item: T) => string) } & (
type KeyOptions<T> = string | ((item: T) => string | string[]);
type ExtendedKeyOptions<T> = { key: KeyOptions<T> } & (
| { minRanking: number }
| { maxRanking: number }
| { threshold: number });
| { threshold: number }
);
interface Options<T> {
keys?: Array<string | ((item: T) => string) | ExtendedKeyOptions<T>>;
keys?: Array<KeyOptions<T> | ExtendedKeyOptions<T>>;
threshold?: number;
keepDiacritics?: boolean;
}
@@ -38,10 +42,6 @@ interface Options<T> {
* @param options - Some options to configure the sorter
* @return the new sorted array
*/
declare function matchSorter<T>(
items: ReadonlyArray<T>,
value: string,
options?: Options<T>
): T[];
declare function matchSorter<T>(items: ReadonlyArray<T>, value: string, options?: Options<T>): T[];
export = matchSorter;
+23
View File
@@ -70,6 +70,29 @@ import matchSorter = require('match-sorter');
matchSorter(list, 'j', { keys: [item => item.name] });
// [{name: 'Janice'}, {name: 'Jen'}]
}
{
const tea = [
{ tea: 'Earl Grey', alias: 'A' },
{ tea: 'Assam', alias: 'B' },
{ tea: 'Black', alias: 'C' }
];
matchSorter(tea, 'e', { keys: [item => [item.tea, item.alias]] });
// [{tea: 'Earl Grey', alias: 'A'}]
}
{
const tea = [
{ tea: 'Earl Grey', alias: 'A' },
{ tea: 'Assam', alias: 'B' },
{ tea: 'Black', alias: 'C' }
];
matchSorter(tea, 'e', {
keys: [{
key: item => [item.alias, item.tea],
minRanking: matchSorter.rankings.EQUAL
}]
});
// [{tea: 'Earl Grey', alias: 'A'}]
}
// ### Min and Max Ranking
{