Skip to content

Commit 46c46fa

Browse files
Support extractin fuzzy target/key via function.
For example, useful when a consumer wants to sort on a nested key directly rather than use the extracted text of an option.
1 parent 6201141 commit 46c46fa

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/fuzzy.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,18 @@ const fuzzyHighlight = (
108108
const fuzzySort = (
109109
value: string,
110110
items: any[],
111-
key?: string
111+
key?: string | ((item: any) => any),
112112
): FuzzySortResult => {
113113
const sorted = [];
114114

115115
for (let index = 0; index < items.length; index++) {
116116
const item = items[index];
117-
const target = key ? item[key] : item;
117+
const target = key
118+
? typeof key === "function"
119+
? key(item)
120+
: item[key]
121+
: item;
122+
118123
const result = fuzzySearch(value, target);
119124
if (result.score) {
120125
sorted.push({ ...result, item, index });

0 commit comments

Comments
 (0)