forked from rob-balfre/svelte-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
38 lines (33 loc) · 928 Bytes
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export default function filter({
loadOptions,
filterText,
items,
multiple,
value,
itemId,
groupBy,
filterSelectedItems,
itemFilter,
convertStringItemsToObjects,
filterGroupedItems,
label,
}) {
if (items && loadOptions) return items;
if (!items) return [];
if (items && items.length > 0 && typeof items[0] !== 'object') {
items = convertStringItemsToObjects(items);
}
let filterResults = items.filter((item) => {
let matchesFilter = itemFilter(item[label], filterText, item);
if (matchesFilter && multiple && value?.length) {
matchesFilter = !value.some((x) => {
return filterSelectedItems ? x[itemId] === item[itemId] : false;
});
}
return matchesFilter;
});
if (groupBy) {
filterResults = filterGroupedItems(filterResults);
}
return filterResults;
}