UNPKG

2.53 kBTypeScriptView Raw
1export type SplitMatchesResult = Array<{
2 isMatch: boolean
3 str: string
4}>
5
6/**
7 * Calls the given functions on matching and non-matching characters
8 * of the given text. Useful when you want to highlight matching characters
9 * in a UI.
10 *
11 * @param text The text that contains the matches
12 * @param matches Array of numbers indicating the indexes of `text` where there is a match
13 * @param matchesWrapper Function to execute on matching substrings
14 * @param noMatchesWrapper Function to execute on non-matching substrings
15 *
16 * @example
17 * highlightMatches('How are you?', 'are', s => `[${s}]`)
18 * // => ['How ', '[are]', ' you?']
19 */
20export function highlightChars<T>(
21 text: string,
22 chars: string,
23 matchesWrapper: (s: string, index: number, array: SplitMatchesResult) => T,
24 noMatchesWrapper?: (s: string, index: number, array: SplitMatchesResult) => T
25): T[]
26
27/**
28 * Calls the given functions on matching and non-matching characters
29 * of the given text. Useful when you want to highlight matching characters
30 * in a UI.
31 *
32 * @remarks
33 * You can get the `matches` by calling `fuzzaldrin-plus`'s `.match()` function
34 *
35 * @param text The text that contains the matches
36 * @param matches Array of numbers indicating the indexes of `text` where there is a match
37 * @param matchesWrapper Function to execute on matching substrings
38 * @param noMatchesWrapper Function to execute on non-matching substrings
39 *
40 * @example
41 * highlightMatches('How are you?', [4, 5, 6], s => `[${s}]`)
42 * // => ['How ', '[are]', ' you?']
43 */
44export function highlightMatches<T>(
45 text: string,
46 matches: number[],
47 matchesWrapper: (s: string, index: number, array: SplitMatchesResult) => T,
48 noMatchesWrapper?: (s: string, index: number, array: SplitMatchesResult) => T
49): T[]
50
51/**
52 * Splits the given text in separate chunks grouping together
53 * all the characters that are matches and not matches.
54 *
55 * @remarks
56 * You can get the `matches` by calling `fuzzaldrin-plus`'s `.match()` function
57 *
58 * @param text The text that contains the matches
59 * @param matches Array of numbers indicating the indexes of `text` where there is a match
60 *
61 * @example
62 * splitMatches('How are you?', [4, 5, 6])
63 * // [
64 * // { isMatch: false, str: 'How ' },
65 * // { isMatch: true, str: 'are' },
66 * // { isMatch: false, str: ' you?' },
67 * // ]
68 */
69export function splitMatches(
70 text: string,
71 matches: number[]
72): SplitMatchesResult
73
74export as namespace highlightMatchesUtils
75
\No newline at end of file