forked from JedWatson/react-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.js
41 lines (37 loc) · 1.11 KB
/
filters.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
39
40
41
// @flow
type Config = {
ignoreCase?: boolean,
ignoreAccents?: boolean,
stringify?: Object => string,
trim?: boolean,
matchFrom?: 'any' | 'start',
};
import { stripDiacritics } from './diacritics';
const trimString = str => str.replace(/^\s+|\s+$/g, '');
const defaultStringify = option => `${option.label} ${option.value}`;
export const createFilter = (config: ?Config) => (
option: { label: string, value: string, data: any },
rawInput: string
) => {
const { ignoreCase, ignoreAccents, stringify, trim, matchFrom } = {
ignoreCase: true,
ignoreAccents: true,
stringify: defaultStringify,
trim: true,
matchFrom: 'any',
...config,
};
let input = trim ? trimString(rawInput) : rawInput;
let candidate = trim ? trimString(stringify(option)) : stringify(option);
if (ignoreCase) {
input = input.toLowerCase();
candidate = candidate.toLowerCase();
}
if (ignoreAccents) {
input = stripDiacritics(input);
candidate = stripDiacritics(candidate);
}
return matchFrom === 'start'
? candidate.substr(0, input.length) === input
: candidate.indexOf(input) > -1;
};