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 |
|
20 | Calls the given functions on matching and non-matching characters
|
21 | of the given text. Useful when you want to highlight matching characters
|
22 | in a UI.
|
23 |
|
24 | ```ts
|
25 | export 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 |
|
47 | Example:
|
48 |
|
49 | ```js
|
50 | import React from 'react'
|
51 | import chalk from 'chalk'
|
52 | import { highlightChars } from 'highlight-matches-utils'
|
53 |
|
54 | highlightChars('How are you?', 'are', s => `(${s})`)
|
55 | // => ['How ', '(are)', ' you?']
|
56 |
|
57 | highlightChars('How are you?', 'are', (s, i) => <mark key={i}>{s}</mark>)
|
58 | // => ['How ', <mark>are</mark>, ' you?']
|
59 |
|
60 | highlightChars('How are you?', 'are', chalk.reset, chalk.gray)
|
61 | // =>
|
62 | // [
|
63 | // "[90mHow [39m",
|
64 | // "[0mare[0m",
|
65 | // "[90m you?[39m",
|
66 | // ]
|
67 | // (useful for highlighting CLI output)
|
68 | ```
|
69 |
|
70 | ### highlightMatches
|
71 |
|
72 | Calls the given functions on matching and non-matching characters
|
73 | of the given text. Useful when you want to highlight matching characters
|
74 | in a UI.
|
75 |
|
76 | > NOTE: You can get the `matches` by calling `fuzzaldrin-plus`'s `.match()` function.
|
77 |
|
78 | ```ts
|
79 | export 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 |
|
103 | Splits the given text in separate chunks grouping together
|
104 | all 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
|
109 | export 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/
|