UNPKG

@gitlab/ui

Version:
29 lines (23 loc) 1.26 kB
import { isString, isNumber } from 'lodash-es'; const isOption = item => Boolean(item) && (isString(item.value) || isNumber(item.value) || item.value === null); // eslint-disable-next-line unicorn/no-array-callback-reference const isGroup = function () { let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, options = _ref.options; return Array.isArray(options) && options.every(isOption); }; const hasNoDuplicates = array => array.length === new Set(array).size; const flattenedOptions = items => items.flatMap(item => isOption(item) ? item : item.options); // eslint-disable-next-line unicorn/no-array-callback-reference const isAllOptionsOrAllGroups = items => items.every(isOption) || items.every(isGroup); const hasUniqueValues = items => hasNoDuplicates(flattenedOptions(items).map(_ref2 => { let value = _ref2.value; return value; })); // eslint-disable-next-line unicorn/no-array-callback-reference const hasUniqueGroups = items => hasNoDuplicates(items.filter(isGroup).map(_ref3 => { let text = _ref3.text; return text; })); const itemsValidator = items => isAllOptionsOrAllGroups(items) && hasUniqueValues(items) && hasUniqueGroups(items); export { flattenedOptions, isOption, itemsValidator };