UNPKG

baseui

Version:

A React Component library implementing the Base design language

62 lines (58 loc) 1.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; /* Copyright (c) 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. */ // @ts-ignore const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // @ts-ignore const isValid = value => { return typeof value !== 'undefined' && value !== null && value !== ''; }; const defaultProps = { filterOption: null, ignoreCase: true, labelKey: 'label', matchPos: 'any', matchProp: 'any', trimFilter: true, valueKey: 'value' }; const filterOptions = (options, filterValue, excludeOptions, newProps) => { const props = { ...defaultProps, ...newProps }; if (props.ignoreCase) { filterValue = filterValue.toLowerCase(); } if (props.trimFilter) { filterValue = filterValue.trim(); } const excludeValues = (excludeOptions || []).reduce((acc, option) => { acc.add(option[props.valueKey]); return acc; }, new Set()); const re = new RegExp(`${props.matchPos === 'start' ? '^' : ''}${escapeRegExp(filterValue)}`, props.ignoreCase ? 'i' : ''); return options.filter(option => { if (excludeValues.has(option[props.valueKey])) return false; if (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); if (!hasValue && !hasLabel) { return false; } const valueTest = hasValue ? String(value) : null; const labelTest = hasLabel ? String(label) : null; return valueTest && props.matchProp !== 'label' && re.test(valueTest) || labelTest && props.matchProp !== 'value' && re.test(labelTest); }); }; var _default = exports.default = filterOptions;