All files / select/utils default-filter-options.js

85.29% Statements 29/34
55.56% Branches 25/45
80% Functions 4/5
87.1% Lines 27/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                  22x   11x 6x                         11x                   11x           22x   22x         22x 22x     22x 22x     22x       22x 74x         74x   74x   3x 3x 3x 3x   3x       3x 3x   3x 3x 3x     3x                                  
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import type {OptionT, ValueT} from '../types.js';
 
const trim = str => str.replace(/^\s+|\s+$/g, '');
 
const isValid = value => {
  return typeof value !== 'undefined' && value !== null && value !== '';
};
 
type defaultPropsT = {
  filterOption: ?(option: OptionT, filterValue: string) => boolean,
  ignoreCase: boolean,
  labelKey: string,
  matchPos: 'any' | 'start',
  matchProp: 'any' | 'label' | 'value',
  trimFilter: boolean,
  valueKey: string,
};
 
const defaultProps: defaultPropsT = {
  filterOption: null,
  ignoreCase: true,
  labelKey: 'label',
  matchPos: 'any',
  matchProp: 'any',
  trimFilter: true,
  valueKey: 'value',
};
 
const filterOptions = (
  options: ValueT,
  filterValue: string,
  excludeOptions: ?ValueT,
  newProps: ?$Shape<defaultPropsT>,
) => {
  let internalExcludeOptions = excludeOptions;
 
  const props = {
    ...defaultProps,
    ...newProps,
  };
 
  Eif (props.ignoreCase) {
    filterValue = filterValue.toLowerCase();
  }
 
  Eif (props.trimFilter) {
    filterValue = trim(filterValue);
  }
 
  Iif (excludeOptions)
    internalExcludeOptions = excludeOptions.map(i => i[props.valueKey]);
 
  // $FlowFixMe
  return options.filter(option => {
    Iif (
      internalExcludeOptions &&
      internalExcludeOptions.indexOf(option[props.valueKey]) > -1
    )
      return false;
    Iif (props.filterOption)
      return props.filterOption.call(undefined, option, filterValue);
    if (!filterValue) return true;
 
    const value = option[props.valueKey];
    const label = option[props.labelKey];
    const hasValue = isValid(value);
    const hasLabel = isValid(label);
 
    Iif (!hasValue && !hasLabel) {
      return false;
    }
 
    let valueTest = hasValue ? String(value) : null;
    let labelTest = hasLabel ? String(label) : null;
 
    Eif (props.ignoreCase) {
      valueTest = valueTest ? valueTest.toLowerCase() : valueTest;
      labelTest = labelTest ? labelTest.toLowerCase() : labelTest;
    }
 
    return props.matchPos === 'start'
      ? (valueTest &&
          props.matchProp !== 'label' &&
          valueTest.substr(0, filterValue.length) === filterValue) ||
          (labelTest &&
            props.matchProp !== 'value' &&
            labelTest.substr(0, filterValue.length) === filterValue)
      : (valueTest &&
          props.matchProp !== 'label' &&
          valueTest.indexOf(filterValue) >= 0) ||
          (labelTest &&
            props.matchProp !== 'value' &&
            labelTest.indexOf(filterValue) >= 0);
  });
};
 
export default filterOptions;