UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 1.66 kB
{"version":3,"file":"use-highlight.cjs","names":["text"],"sources":["../../../../src/components/highlight/use-highlight.ts"],"sourcesContent":["import { useMemo } from \"react\"\nimport { toArray } from \"../../utils\"\n\nexport interface Chunk {\n match: boolean\n text: string\n}\n\nconst escapeRegexp = (term: string): string =>\n term.replace(/[|\\\\{}()[\\]^$+*?.-]/g, (char: string) => `\\\\${char}`)\n\nconst createRegexp = (query: string[]): RegExp | undefined => {\n query = query.filter(Boolean).map((text) => escapeRegexp(text.trim()))\n\n if (query.length) return new RegExp(`(${query.join(\"|\")})`, \"ig\")\n}\n\nexport interface UseHighlightProps {\n query: string | string[]\n text: string\n}\n\nexport const useHighlight = ({ query, text }: UseHighlightProps) => {\n const chunks = useMemo(() => {\n const regexp = createRegexp(toArray(query))\n\n if (!regexp) return [{ match: false, text }]\n\n return text\n .split(regexp)\n .filter(Boolean)\n .map((text) => ({ match: regexp.test(text), text }))\n }, [text, query])\n\n return chunks\n}\n\nexport type UseHighlightReturn = ReturnType<typeof useHighlight>\n"],"mappings":";;;;;;AAQA,MAAM,gBAAgB,SACpB,KAAK,QAAQ,yBAAyB,SAAiB,KAAK,OAAO;AAErE,MAAM,gBAAgB,UAAwC;AAC5D,SAAQ,MAAM,OAAO,QAAQ,CAAC,KAAK,SAAS,aAAa,KAAK,MAAM,CAAC,CAAC;AAEtE,KAAI,MAAM,OAAQ,QAAO,IAAI,OAAO,IAAI,MAAM,KAAK,IAAI,CAAC,IAAI,KAAK;;AAQnE,MAAa,gBAAgB,EAAE,OAAO,WAA8B;AAYlE,iCAX6B;EAC3B,MAAM,SAAS,4DAAqB,MAAM,CAAC;AAE3C,MAAI,CAAC,OAAQ,QAAO,CAAC;GAAE,OAAO;GAAO;GAAM,CAAC;AAE5C,SAAO,KACJ,MAAM,OAAO,CACb,OAAO,QAAQ,CACf,KAAK,YAAU;GAAE,OAAO,OAAO,KAAKA,OAAK;GAAE;GAAM,EAAE;IACrD,CAAC,MAAM,MAAM,CAAC"}