UNPKG

ssc-refer

Version:
29 lines (21 loc) 750 B
import invariant from 'invariant'; import { find, uniqueId } from 'lodash'; import getOptionLabel from './getOptionLabel'; function addCustomOption(results, text, labelKey) { results = results.slice(); var exactMatchFound = find(results, function (o) { return getOptionLabel(o, labelKey) === text; }); if (!text.trim() || exactMatchFound) { return results; } var newOption = { id: uniqueId('new-id-'), customOption: true }; !(typeof labelKey === 'string') ? process.env.NODE_ENV !== 'production' ? invariant(false, '`labelKey` must be a string when creating new options.') : invariant(false) : void 0; newOption[labelKey] = text; results.push(newOption); return results; } export default addCustomOption;