UNPKG

3.47 kBMarkdownView Raw
1# highlight-matches-utils
2
3> Utility functions to mark and highlight character matches in text
4
5[![Travis (.org)](https://img.shields.io/travis/reyronald/highlight-matches-utils.svg)](https://travis-ci.org/reyronald/highlight-matches-utils)
6[![Codecov](https://img.shields.io/codecov/c/github/reyronald/highlight-matches-utils.svg)](https://codecov.io/gh/reyronald/highlight-matches-utils)
7[![GitHub](https://img.shields.io/github/license/reyronald/highlight-matches-utils.svg)](https://github.com/reyronald/highlight-matches-utils/blob/master/LICENSE)
8[![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/highlight-matches-utils.svg)](https://unpkg.com/highlight-matches-utils/)
9[![npm bundle size (minified)](https://img.shields.io/bundlephobia/minzip/highlight-matches-utils.svg)](https://unpkg.com/highlight-matches-utils/)
10
11[See an online example in CodeSandbox on how it can be used](https://codesandbox.io/s/71nom6zl21).
12
13## API
14
15**For more in-depth examples, [check the test file](./index.test.js).**
16**For more in-depth documentation, [check the TypeScript definition file](./index.d.ts).**
17
18### highlightChars
19
20Calls the given functions on matching and non-matching characters
21of the given text. Useful when you want to highlight matching characters
22in a UI.
23
24```ts
25export function highlightChars<T>(
26 text: string,
27 chars: string,
28 matchesWrapper: (
29 s: string,
30 index: number,
31 array: {
32 isMatch: boolean
33 str: string
34 }[]
35 ) => T,
36 noMatchesWrapper?: (
37 s: string,
38 index: number,
39 array: {
40 isMatch: boolean
41 str: string
42 }[]
43 ) => T
44): T[]
45```
46
47Example:
48
49```js
50import React from 'react'
51import chalk from 'chalk'
52import { highlightChars } from 'highlight-matches-utils'
53
54highlightChars('How are you?', 'are', s => `(${s})`)
55// => ['How ', '(are)', ' you?']
56
57highlightChars('How are you?', 'are', (s, i) => <mark key={i}>{s}</mark>)
58// => ['How ', <mark>are</mark>, ' you?']
59
60highlightChars('How are you?', 'are', chalk.reset, chalk.gray)
61// =>
62// [
63// "How ",
64// "are",
65// " you?",
66// ]
67// (useful for highlighting CLI output)
68```
69
70### highlightMatches
71
72Calls the given functions on matching and non-matching characters
73of the given text. Useful when you want to highlight matching characters
74in a UI.
75
76> NOTE: You can get the `matches` by calling `fuzzaldrin-plus`'s `.match()` function.
77
78```ts
79export function highlightMatches<T>(
80 text: string,
81 matches: number[],
82 matchesWrapper: (
83 s: string,
84 index: number,
85 array: {
86 isMatch: boolean
87 str: string
88 }[]
89 ) => T,
90 noMatchesWrapper?: (
91 s: string,
92 index: number,
93 array: {
94 isMatch: boolean
95 str: string
96 }[]
97 ) => T
98): T[]
99```
100
101### splitMatches
102
103Splits the given text in separate chunks grouping together
104all the characters that are matches and not matches.
105
106> NOTE: You can get the `matches` by calling `fuzzaldrin-plus`'s `.match()` function.
107
108```ts
109export function splitMatches(
110 text: string,
111 matches: number[]
112): Array<{| isMatch: boolean, str: string |}>
113```
114
115### Prior Art
116
117- https://github.com/bvaughn/react-highlight-words
118- https://github.com/bvaughn/highlight-words-core
119- https://github.com/desktop/desktop/blob/23ddc3aceae0e051a125a934cc322d5ea0db0ebb/app/src/ui/lib/highlight-text.tsx
120- https://markjs.io/