UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = defaultFilterBy;
9
10var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
11
12var _getOptionProperty = _interopRequireDefault(require("./getOptionProperty"));
13
14var _nodash = require("./nodash");
15
16var _stripDiacritics = _interopRequireDefault(require("./stripDiacritics"));
17
18var _warn = _interopRequireDefault(require("./warn"));
19
20function isMatch(input, string, props) {
21 var searchStr = input;
22 var str = string;
23
24 if (!props.caseSensitive) {
25 searchStr = searchStr.toLowerCase();
26 str = str.toLowerCase();
27 }
28
29 if (props.ignoreDiacritics) {
30 searchStr = (0, _stripDiacritics["default"])(searchStr);
31 str = (0, _stripDiacritics["default"])(str);
32 }
33
34 return str.indexOf(searchStr) !== -1;
35}
36/**
37 * Default algorithm for filtering results.
38 */
39
40
41function defaultFilterBy(option, props) {
42 var filterBy = props.filterBy,
43 labelKey = props.labelKey,
44 multiple = props.multiple,
45 selected = props.selected,
46 text = props.text; // Don't show selected options in the menu for the multi-select case.
47
48 if (multiple && selected.some(function (o) {
49 return (0, _fastDeepEqual["default"])(o, option);
50 })) {
51 return false;
52 }
53
54 if ((0, _nodash.isFunction)(labelKey)) {
55 return isMatch(text, labelKey(option), props);
56 }
57
58 var fields = filterBy.slice();
59
60 if ((0, _nodash.isString)(labelKey)) {
61 // Add the `labelKey` field to the list of fields if it isn't already there.
62 if (fields.indexOf(labelKey) === -1) {
63 fields.unshift(labelKey);
64 }
65 }
66
67 if ((0, _nodash.isString)(option)) {
68 (0, _warn["default"])(fields.length <= 1, 'You cannot filter by properties when `option` is a string.');
69 return isMatch(text, option, props);
70 }
71
72 return fields.some(function (field) {
73 var value = (0, _getOptionProperty["default"])(option, field);
74
75 if (!(0, _nodash.isString)(value)) {
76 (0, _warn["default"])(false, 'Fields passed to `filterBy` should have string values. Value will ' + 'be converted to a string; results may be unexpected.');
77 value = String(value);
78 }
79
80 return isMatch(text, value, props);
81 });
82}
\No newline at end of file