All files / src/utils defaultFilterOptions.js

3.57% Statements 1/28
0% Branches 0/36
0% Functions 0/3
5.26% Lines 1/19
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                                                                          4x  
import stripDiacritics from './stripDiacritics';
 
function filterOptions (options, filterValue, excludeOptions, props) {
	if (props.ignoreAccents) {
		filterValue = stripDiacritics(filterValue);
	}
 
	if (props.ignoreCase) {
		filterValue = filterValue.toLowerCase();
	}
 
	if (excludeOptions) excludeOptions = excludeOptions.map(i => i[props.valueKey]);
 
	return options.filter(option => {
		if (excludeOptions && excludeOptions.indexOf(option[props.valueKey]) > -1) return false;
		if (props.filterOption) return props.filterOption.call(this, option, filterValue);
		if (!filterValue) return true;
		var valueTest = String(option[props.valueKey]);
		var labelTest = String(option[props.labelKey]);
		if (props.ignoreAccents) {
			if (props.matchProp !== 'label') valueTest = stripDiacritics(valueTest);
			if (props.matchProp !== 'value') labelTest = stripDiacritics(labelTest);
		}
		if (props.ignoreCase) {
			if (props.matchProp !== 'label') valueTest = valueTest.toLowerCase();
			if (props.matchProp !== 'value') labelTest = labelTest.toLowerCase();
		}
		return props.matchPos === 'start' ? (
			(props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) ||
			(props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue)
		) : (
			(props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) ||
			(props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
		);
	});
}
 
module.exports = filterOptions;