@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
26 lines (24 loc) • 820 B
JavaScript
import { utils_exports } from "../../utils/index.js";
import { useMemo } from "react";
//#region src/components/highlight/use-highlight.ts
const escapeRegexp = (term) => term.replace(/[|\\{}()[\]^$+*?.-]/g, (char) => `\\${char}`);
const createRegexp = (query) => {
query = query.filter(Boolean).map((text) => escapeRegexp(text.trim()));
if (query.length) return new RegExp(`(${query.join("|")})`, "ig");
};
const useHighlight = ({ query, text }) => {
return useMemo(() => {
const regexp = createRegexp((0, utils_exports.toArray)(query));
if (!regexp) return [{
match: false,
text
}];
return text.split(regexp).filter(Boolean).map((text$1) => ({
match: regexp.test(text$1),
text: text$1
}));
}, [text, query]);
};
//#endregion
export { useHighlight };
//# sourceMappingURL=use-highlight.js.map