UNPKG

braid-design-system

Version:
62 lines (61 loc) 2.31 kB
"use strict"; const assert = require("assert"); const lib_components_Autosuggest_Autosuggest_cjs = require("./Autosuggest.cjs"); const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert); function matchSuggestion(suggestion, query) { const highlights = lib_components_Autosuggest_Autosuggest_cjs.highlightSuggestions( suggestion.label ?? suggestion.text, query ); return highlights.length ? { ...suggestion, highlights } : null; } function filterSuggestions(...args) { assert__default.default( [1, 2].includes(args.length), `Invalid number of arguments passed to "filterSuggestions". Expected 1 or 2, got ${args.length}` ); if (args.length === 1) { return (inputValue) => filter(args[0], inputValue); } else if (args.length === 2) { return filter(args[0], args[1]); } function filter(suggestions, inputValue) { assert__default.default( typeof inputValue === "string" || typeof inputValue === "object" && inputValue !== null && "text" in inputValue, 'The second argument to "filterSuggestions" must be a string or an Autosuggest value object, e.g. { text: "Hello world" }' ); const query = (typeof inputValue === "string" ? inputValue : inputValue.text).trim(); if (query === "") { return suggestions; } const filteredSuggestions = []; suggestions.forEach((suggestion) => { if ("suggestions" in suggestion) { const filteredGroupSuggestions = []; suggestion.suggestions.forEach((groupSuggestion) => { const filteredSuggestion = matchSuggestion(groupSuggestion, query); if (filteredSuggestion) { filteredGroupSuggestions.push(filteredSuggestion); } }); if (filteredGroupSuggestions.length) { filteredSuggestions.push({ ...suggestion, suggestions: filteredGroupSuggestions }); } } else { const filteredSuggestion = matchSuggestion(suggestion, query); if (filteredSuggestion) { filteredSuggestions.push(filteredSuggestion); } } }); return filteredSuggestions; } } exports.filterSuggestions = filterSuggestions;