@splendidlabz/svelte 4.15.3 → 4.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/components/Form/Combobox/Autocomplete.svelte +208 -0
- package/components/Form/Combobox/AutocompleteOptions.svelte +99 -14
- package/components/Form/Combobox/Chips.svelte +58 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +44 -1
- package/components/Form/Combobox/Select.svelte +195 -0
- package/components/Form/Combobox/SelectOptions.svelte +57 -0
- package/components/Form/Combobox/autocomplete.svelte.js +517 -96
- package/components/Form/Combobox/combobox.svelte.js +395 -49
- package/components/Form/Combobox/index.js +6 -2
- package/components/Form/Combobox/listbox.js +119 -22
- package/components/Form/Combobox/match.js +405 -0
- package/components/Form/Combobox/matching.js +83 -0
- package/components/Form/Combobox/select.svelte.js +533 -0
- package/components/Form/FieldMeta.svelte +50 -0
- package/components/Form/Honeypot.svelte +3 -1
- package/components/Form/{Select.svelte → NativeSelect.svelte} +20 -15
- package/components/Form/SearchInput.svelte +6 -0
- package/components/Form/TextInput.svelte +42 -22
- package/components/Form/Textarea.svelte +22 -34
- package/components/Form/index.js +1 -1
- package/package.json +3 -4
- package/components/Form/Combobox/AutocompleteInput.svelte +0 -26
- package/components/Form/Combobox/ComboboxInput.svelte +0 -37
- package/components/Form/Combobox/autocomplete-matching.js +0 -69
|
@@ -1,28 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file DOM helpers for the options inside a listbox.
|
|
3
3
|
*
|
|
4
|
-
* Two states sit on an option and they are not the same thing. `data-highlighted` is the
|
|
4
|
+
* Two states sit on an option and they are not the same thing. `data-highlighted` is the highlighted option — where the keyboard or pointer is, one option at a time. `aria-selected` is the selection — what the person chose, which can be more than one.
|
|
5
5
|
*/
|
|
6
6
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
7
|
+
import { removeAccents } from './match.js'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* prepareOptions
|
|
10
|
-
* Gives options written as plain `li` their role, id and
|
|
11
|
+
* Gives options written as plain `li` their role, id, label and value, and gives every option its check.
|
|
11
12
|
*
|
|
12
|
-
* Options rendered by Svelte already carry
|
|
13
|
+
* Options rendered by Svelte already carry their role, id, label and value in their markup, so only `li` without a role are touched for those.
|
|
14
|
+
*
|
|
15
|
+
* The check is copied in here rather than rendered by Svelte because slot options are markup the person wrote, and Svelte can't render a snippet inside it. Copying it into every option keeps slot options, item options and created options the same.
|
|
13
16
|
*
|
|
14
17
|
* @param {object} opts
|
|
15
18
|
* @param {HTMLElement} opts.listbox
|
|
16
19
|
* @param {boolean} [opts.multiple] Let more than one option be selected
|
|
20
|
+
* @param {HTMLElement | null} [opts.check] Check the options copy
|
|
17
21
|
* @returns {void}
|
|
18
22
|
*/
|
|
19
|
-
export function prepareOptions({ listbox, multiple = false }) {
|
|
20
|
-
|
|
23
|
+
export function prepareOptions({ listbox, multiple = false, check }) {
|
|
24
|
+
listbox.setAttribute('aria-multiselectable', String(multiple))
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
/** @type {NodeListOf<HTMLLIElement>} */
|
|
27
|
+
const plainOptions = listbox.querySelectorAll('li:not([role])')
|
|
28
|
+
for (const option of plainOptions) {
|
|
23
29
|
option.setAttribute('role', 'option')
|
|
24
30
|
option.id ||= randomString(8)
|
|
25
|
-
option.dataset.
|
|
31
|
+
option.dataset.label = option.textContent.trim()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// An option submits its value, and the label is the value when the markup names no other.
|
|
35
|
+
for (const option of getAllOptions(listbox)) {
|
|
36
|
+
option.dataset.value ??= option.dataset.label
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The order the markup wrote, so filtering can put the list back after ranking it
|
|
40
|
+
getAllOptions(listbox).forEach((option, index) => {
|
|
41
|
+
option.dataset.position ??= String(index)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Comes after the label is read, so the check's own markup never becomes part of it
|
|
45
|
+
if (check) {
|
|
46
|
+
for (const option of getAllOptions(listbox)) {
|
|
47
|
+
if (!option.querySelector('.option-check'))
|
|
48
|
+
option.append(check.cloneNode(true))
|
|
49
|
+
}
|
|
26
50
|
}
|
|
27
51
|
|
|
28
52
|
// A multiselectable listbox states every option's selected state, not only the chosen ones.
|
|
@@ -34,6 +58,55 @@ export function prepareOptions({ listbox, multiple = false }) {
|
|
|
34
58
|
}
|
|
35
59
|
}
|
|
36
60
|
|
|
61
|
+
/**
|
|
62
|
+
* showOptionCount
|
|
63
|
+
* Caps the listbox at the height of its first `size` options, so that many show before it scrolls.
|
|
64
|
+
*
|
|
65
|
+
* The heights are summed rather than multiplied, so an option that wraps onto a second line still counts as one option.
|
|
66
|
+
*
|
|
67
|
+
* @param {object} opts
|
|
68
|
+
* @param {HTMLElement} opts.listbox
|
|
69
|
+
* @param {number} [opts.size] How many options to show
|
|
70
|
+
* @returns {void}
|
|
71
|
+
*/
|
|
72
|
+
export function showOptionCount({ listbox, size }) {
|
|
73
|
+
if (!size) return
|
|
74
|
+
|
|
75
|
+
const options = getVisibleOptions(listbox).slice(0, size)
|
|
76
|
+
if (!options.length) return
|
|
77
|
+
|
|
78
|
+
const height = options.reduce(
|
|
79
|
+
(total, option) => total + option.getBoundingClientRect().height,
|
|
80
|
+
0,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
// Options measure 0 inside a closed popover, and a cap of 0 would hide every one
|
|
84
|
+
if (!height) return
|
|
85
|
+
listbox.style.setProperty('--options-max-height', `${height}px`)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* sortOptions
|
|
90
|
+
* Puts the options into the DOM in the order they read, because a screen reader reads the DOM.
|
|
91
|
+
*
|
|
92
|
+
* Svelte renders a created value and the create option from their own blocks below the list, and each block inserts against an anchor node. An option moved past one of those anchors sends the block's next render above the list, so everything stays ahead of the first anchor.
|
|
93
|
+
*
|
|
94
|
+
* @param {object} opts
|
|
95
|
+
* @param {HTMLElement} opts.listbox
|
|
96
|
+
* @param {HTMLElement[]} opts.options Options in the order they read
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
99
|
+
export function sortOptions({ listbox, options }) {
|
|
100
|
+
if (!options.length) return
|
|
101
|
+
|
|
102
|
+
const group = new Set(options)
|
|
103
|
+
const anchor = /** @type {HTMLElement} */ (
|
|
104
|
+
getAllOptions(listbox).findLast(option => group.has(option))
|
|
105
|
+
).nextSibling
|
|
106
|
+
|
|
107
|
+
for (const option of options) listbox.insertBefore(option, anchor)
|
|
108
|
+
}
|
|
109
|
+
|
|
37
110
|
/**
|
|
38
111
|
* getAllOptions
|
|
39
112
|
* Gets every option, including the hidden ones.
|
|
@@ -49,21 +122,16 @@ export function getAllOptions(listbox) {
|
|
|
49
122
|
* getVisibleOptions
|
|
50
123
|
* Gets the options that aren't filtered out, in the order they appear on screen.
|
|
51
124
|
*
|
|
52
|
-
* Filtering ranks matches with the CSS `order` property, so screen order and DOM order differ. Arrow keys follow this list, so it has to be the one the eye follows.
|
|
53
|
-
*
|
|
54
125
|
* @param {HTMLElement} listbox
|
|
55
126
|
* @returns {HTMLElement[]}
|
|
56
127
|
*/
|
|
57
128
|
export function getVisibleOptions(listbox) {
|
|
58
|
-
|
|
59
|
-
listbox.querySelectorAll('[role="option"]:not([hidden])'),
|
|
60
|
-
)
|
|
61
|
-
return options.sort((a, b) => getOrder(a) - getOrder(b))
|
|
129
|
+
return getAllOptions(listbox).filter(option => !option.hidden)
|
|
62
130
|
}
|
|
63
131
|
|
|
64
132
|
/**
|
|
65
133
|
* getHighlightedOption
|
|
66
|
-
* Gets the option
|
|
134
|
+
* Gets the highlighted option.
|
|
67
135
|
*
|
|
68
136
|
* @param {HTMLElement} listbox
|
|
69
137
|
* @returns {HTMLElement | null}
|
|
@@ -74,23 +142,28 @@ export function getHighlightedOption(listbox) {
|
|
|
74
142
|
|
|
75
143
|
/**
|
|
76
144
|
* highlightOption
|
|
77
|
-
*
|
|
145
|
+
* Highlights one option.
|
|
78
146
|
*
|
|
79
147
|
* @param {object} opts
|
|
80
148
|
* @param {HTMLElement} opts.listbox
|
|
81
149
|
* @param {HTMLElement} opts.option
|
|
82
150
|
* @param {boolean} [opts.shouldScroll] Scroll option into view
|
|
151
|
+
* @param {ScrollBehavior} [opts.behaviour] How the scroll runs
|
|
83
152
|
* @returns {void}
|
|
84
153
|
*/
|
|
85
|
-
export function highlightOption(
|
|
154
|
+
export function highlightOption(opts) {
|
|
155
|
+
const { listbox, option, shouldScroll = true, behaviour } = opts
|
|
156
|
+
|
|
86
157
|
clearHighlight(listbox)
|
|
87
158
|
option.dataset.highlighted = ''
|
|
88
|
-
if (shouldScroll)
|
|
159
|
+
if (shouldScroll) {
|
|
160
|
+
option.scrollIntoView({ block: 'nearest', behavior: behaviour })
|
|
161
|
+
}
|
|
89
162
|
}
|
|
90
163
|
|
|
91
164
|
/**
|
|
92
165
|
* clearHighlight
|
|
93
|
-
*
|
|
166
|
+
* Removes the highlight from every option.
|
|
94
167
|
*
|
|
95
168
|
* @param {HTMLElement} listbox
|
|
96
169
|
* @returns {void}
|
|
@@ -155,7 +228,10 @@ export function getAdjacentOption({ listbox, direction, loop }) {
|
|
|
155
228
|
const options = getVisibleOptions(listbox)
|
|
156
229
|
if (!options.length) return null
|
|
157
230
|
|
|
158
|
-
const
|
|
231
|
+
const highlightedOption = getHighlightedOption(listbox)
|
|
232
|
+
const currentIndex = highlightedOption
|
|
233
|
+
? options.indexOf(highlightedOption)
|
|
234
|
+
: -1
|
|
159
235
|
const lastIndex = options.length - 1
|
|
160
236
|
|
|
161
237
|
if (currentIndex === -1)
|
|
@@ -167,7 +243,28 @@ export function getAdjacentOption({ listbox, direction, loop }) {
|
|
|
167
243
|
return options[index]
|
|
168
244
|
}
|
|
169
245
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
246
|
+
/**
|
|
247
|
+
* getOptionStartingWith
|
|
248
|
+
* Gets the first visible option whose label starts with typed text, searching from an option and wrapping past the end.
|
|
249
|
+
*
|
|
250
|
+
* Case and accents are ignored, so "e" reaches "Éire".
|
|
251
|
+
*
|
|
252
|
+
* @param {object} opts
|
|
253
|
+
* @param {HTMLElement} opts.listbox
|
|
254
|
+
* @param {string} opts.text Typed text
|
|
255
|
+
* @param {HTMLElement | null} [opts.from] Option the search starts at
|
|
256
|
+
* @returns {HTMLElement | null}
|
|
257
|
+
*/
|
|
258
|
+
export function getOptionStartingWith({ listbox, text, from }) {
|
|
259
|
+
const options = getVisibleOptions(listbox)
|
|
260
|
+
const start = from ? options.indexOf(from) : 0
|
|
261
|
+
const typed = removeAccents(text).toLowerCase()
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
[...options.slice(start), ...options.slice(0, start)].find(option =>
|
|
265
|
+
removeAccents(/** @type {string} */ (option.dataset.label))
|
|
266
|
+
.toLowerCase()
|
|
267
|
+
.startsWith(typed),
|
|
268
|
+
) ?? null
|
|
269
|
+
)
|
|
173
270
|
}
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Matches query words inside text, ignoring accents.
|
|
3
|
+
*
|
|
4
|
+
* Copied from autosuggest-highlight 3.3.4 (match) and its bundled remove-accents, both MIT. autosuggest-highlight ships CommonJS only, so importing it from this package broke hydration in `astro dev`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* match
|
|
9
|
+
* Finds where every word of a query matches inside a text.
|
|
10
|
+
*
|
|
11
|
+
* @param {object} opts
|
|
12
|
+
* @param {string} opts.text Text to search
|
|
13
|
+
* @param {string} opts.query Typed query
|
|
14
|
+
* @param {boolean} opts.insideWords Match inside words too
|
|
15
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
16
|
+
* @returns {[number, number][]} Sorted ranges, or empty when any word misses.
|
|
17
|
+
*/
|
|
18
|
+
export function match({ text, query, insideWords, caseSensitive = false }) {
|
|
19
|
+
const removeAccents = AccentRemover()
|
|
20
|
+
const characters = Array.from(text).map(removeAccents)
|
|
21
|
+
let remainingText = characters.join('')
|
|
22
|
+
const words = removeAccents(query)
|
|
23
|
+
.trim()
|
|
24
|
+
.split(/\s+/)
|
|
25
|
+
.filter(word => word.length > 0)
|
|
26
|
+
|
|
27
|
+
/** @type {[number, number][]} */
|
|
28
|
+
const ranges = []
|
|
29
|
+
for (const word of words) {
|
|
30
|
+
const boundary = !insideWords && /[a-z0-9_]/i.test(word[0]) ? '\\b' : ''
|
|
31
|
+
const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
32
|
+
const occurrence = new RegExp(
|
|
33
|
+
boundary + escapedWord,
|
|
34
|
+
caseSensitive ? '' : 'i',
|
|
35
|
+
).exec(remainingText)
|
|
36
|
+
if (!occurrence) return []
|
|
37
|
+
|
|
38
|
+
// Maps the index back to the original text, where "Æ" became "AE"
|
|
39
|
+
const { index } = occurrence
|
|
40
|
+
const startOffset = index - characters.slice(0, index).join('').length
|
|
41
|
+
const endOffset =
|
|
42
|
+
word.length - characters.slice(index, index + word.length).join('').length
|
|
43
|
+
const start = index + startOffset
|
|
44
|
+
const end = index + word.length + startOffset + endOffset
|
|
45
|
+
if (start !== end) ranges.push([start, end])
|
|
46
|
+
|
|
47
|
+
// Blanks the matched part so later words skip it
|
|
48
|
+
remainingText =
|
|
49
|
+
remainingText.slice(0, index) +
|
|
50
|
+
' '.repeat(word.length) +
|
|
51
|
+
remainingText.slice(index + word.length)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return ranges.sort((a, b) => a[0] - b[0])
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* removeAccents
|
|
59
|
+
* Writes a string with plain letters, so "Café" reads as "Cafe".
|
|
60
|
+
*
|
|
61
|
+
* @param {string} string
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
export function removeAccents(string) {
|
|
65
|
+
return Array.from(string).map(AccentRemover()).join('')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* AccentRemover
|
|
70
|
+
* Replaces accented characters with their plain letters.
|
|
71
|
+
*
|
|
72
|
+
* @returns {(string: string) => string}
|
|
73
|
+
*/
|
|
74
|
+
function AccentRemover() {
|
|
75
|
+
/** @type {Record<string, string>} */
|
|
76
|
+
const accentMap = {
|
|
77
|
+
À: 'A',
|
|
78
|
+
Á: 'A',
|
|
79
|
+
Â: 'A',
|
|
80
|
+
Ã: 'A',
|
|
81
|
+
Ä: 'A',
|
|
82
|
+
Å: 'A',
|
|
83
|
+
Ấ: 'A',
|
|
84
|
+
Ắ: 'A',
|
|
85
|
+
Ẳ: 'A',
|
|
86
|
+
Ẵ: 'A',
|
|
87
|
+
Ặ: 'A',
|
|
88
|
+
Æ: 'AE',
|
|
89
|
+
Ầ: 'A',
|
|
90
|
+
Ằ: 'A',
|
|
91
|
+
Ȃ: 'A',
|
|
92
|
+
Ç: 'C',
|
|
93
|
+
Ḉ: 'C',
|
|
94
|
+
È: 'E',
|
|
95
|
+
É: 'E',
|
|
96
|
+
Ê: 'E',
|
|
97
|
+
Ë: 'E',
|
|
98
|
+
Ế: 'E',
|
|
99
|
+
Ḗ: 'E',
|
|
100
|
+
Ề: 'E',
|
|
101
|
+
Ḕ: 'E',
|
|
102
|
+
Ḝ: 'E',
|
|
103
|
+
Ȇ: 'E',
|
|
104
|
+
Ì: 'I',
|
|
105
|
+
Í: 'I',
|
|
106
|
+
Î: 'I',
|
|
107
|
+
Ï: 'I',
|
|
108
|
+
Ḯ: 'I',
|
|
109
|
+
Ȋ: 'I',
|
|
110
|
+
Ð: 'D',
|
|
111
|
+
Ñ: 'N',
|
|
112
|
+
Ò: 'O',
|
|
113
|
+
Ó: 'O',
|
|
114
|
+
Ô: 'O',
|
|
115
|
+
Õ: 'O',
|
|
116
|
+
Ö: 'O',
|
|
117
|
+
Ø: 'O',
|
|
118
|
+
Ố: 'O',
|
|
119
|
+
Ṍ: 'O',
|
|
120
|
+
Ṓ: 'O',
|
|
121
|
+
Ȏ: 'O',
|
|
122
|
+
Ù: 'U',
|
|
123
|
+
Ú: 'U',
|
|
124
|
+
Û: 'U',
|
|
125
|
+
Ü: 'U',
|
|
126
|
+
Ý: 'Y',
|
|
127
|
+
à: 'a',
|
|
128
|
+
á: 'a',
|
|
129
|
+
â: 'a',
|
|
130
|
+
ã: 'a',
|
|
131
|
+
ä: 'a',
|
|
132
|
+
å: 'a',
|
|
133
|
+
ấ: 'a',
|
|
134
|
+
ắ: 'a',
|
|
135
|
+
ẳ: 'a',
|
|
136
|
+
ẵ: 'a',
|
|
137
|
+
ặ: 'a',
|
|
138
|
+
æ: 'ae',
|
|
139
|
+
ầ: 'a',
|
|
140
|
+
ằ: 'a',
|
|
141
|
+
ȃ: 'a',
|
|
142
|
+
ç: 'c',
|
|
143
|
+
ḉ: 'c',
|
|
144
|
+
è: 'e',
|
|
145
|
+
é: 'e',
|
|
146
|
+
ê: 'e',
|
|
147
|
+
ë: 'e',
|
|
148
|
+
ế: 'e',
|
|
149
|
+
ḗ: 'e',
|
|
150
|
+
ề: 'e',
|
|
151
|
+
ḕ: 'e',
|
|
152
|
+
ḝ: 'e',
|
|
153
|
+
ȇ: 'e',
|
|
154
|
+
ì: 'i',
|
|
155
|
+
í: 'i',
|
|
156
|
+
î: 'i',
|
|
157
|
+
ï: 'i',
|
|
158
|
+
ḯ: 'i',
|
|
159
|
+
ȋ: 'i',
|
|
160
|
+
ð: 'd',
|
|
161
|
+
ñ: 'n',
|
|
162
|
+
ò: 'o',
|
|
163
|
+
ó: 'o',
|
|
164
|
+
ô: 'o',
|
|
165
|
+
õ: 'o',
|
|
166
|
+
ö: 'o',
|
|
167
|
+
ø: 'o',
|
|
168
|
+
ố: 'o',
|
|
169
|
+
ṍ: 'o',
|
|
170
|
+
ṓ: 'o',
|
|
171
|
+
ȏ: 'o',
|
|
172
|
+
ù: 'u',
|
|
173
|
+
ú: 'u',
|
|
174
|
+
û: 'u',
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|
384
|
+
Ȕ: 'U',
|
|
385
|
+
ȕ: 'u',
|
|
386
|
+
Ǧ: 'G',
|
|
387
|
+
ǧ: 'g',
|
|
388
|
+
Ȟ: 'H',
|
|
389
|
+
ȟ: 'h',
|
|
390
|
+
ǰ: 'j',
|
|
391
|
+
Ǩ: 'K',
|
|
392
|
+
ǩ: 'k',
|
|
393
|
+
Ṧ: 'S',
|
|
394
|
+
ṧ: 's',
|
|
395
|
+
Ḑ: 'D',
|
|
396
|
+
ḑ: 'd',
|
|
397
|
+
Ȩ: 'E',
|
|
398
|
+
ȩ: 'e',
|
|
399
|
+
Ḩ: 'H',
|
|
400
|
+
ḩ: 'h',
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return string =>
|
|
404
|
+
Array.from(string, character => accentMap[character] ?? character).join('')
|
|
405
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Matches option texts against a typed query.
|
|
3
|
+
*
|
|
4
|
+
* match strips diacritics before matching, so "cafe" matches "Café". Case is ignored the same way, until `caseSensitive` says otherwise.
|
|
5
|
+
*/
|
|
6
|
+
import { match } from './match.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* getMatchedIndexes
|
|
10
|
+
* Finds which options a query matches, best first. Each option has one text per key it matches by.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} opts
|
|
13
|
+
* @param {string[][]} opts.texts Each option's texts
|
|
14
|
+
* @param {string} opts.query Typed query
|
|
15
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
16
|
+
* @returns {number[]} Word-start matches on any text first, then inside-word matches.
|
|
17
|
+
*/
|
|
18
|
+
export function getMatchedIndexes({ texts, query, caseSensitive }) {
|
|
19
|
+
const wordStartIndexes = getMatchingIndexes({
|
|
20
|
+
texts,
|
|
21
|
+
query,
|
|
22
|
+
insideWords: false,
|
|
23
|
+
caseSensitive,
|
|
24
|
+
})
|
|
25
|
+
const insideWordIndexes = getMatchingIndexes({
|
|
26
|
+
texts,
|
|
27
|
+
query,
|
|
28
|
+
insideWords: true,
|
|
29
|
+
caseSensitive,
|
|
30
|
+
})
|
|
31
|
+
return Array.from(new Set([...wordStartIndexes, ...insideWordIndexes]))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* hasPerfectMatch
|
|
36
|
+
* Checks whether the query equals one of the texts, ignoring diacritics.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} opts
|
|
39
|
+
* @param {string[]} opts.texts Texts to search
|
|
40
|
+
* @param {string} opts.query Typed query
|
|
41
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
42
|
+
* @returns {boolean}
|
|
43
|
+
*/
|
|
44
|
+
export function hasPerfectMatch({ texts, query, caseSensitive }) {
|
|
45
|
+
return texts.some(text => isPerfectMatch({ text, query, caseSensitive }))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* getMatchingIndexes
|
|
50
|
+
* Gets the indexes of the options where any text matches the query.
|
|
51
|
+
*
|
|
52
|
+
* @param {object} opts
|
|
53
|
+
* @param {string[][]} opts.texts Each option's texts
|
|
54
|
+
* @param {string} opts.query Typed query
|
|
55
|
+
* @param {boolean} opts.insideWords Match inside words too
|
|
56
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
57
|
+
* @returns {number[]}
|
|
58
|
+
*/
|
|
59
|
+
function getMatchingIndexes({ texts, query, insideWords, caseSensitive }) {
|
|
60
|
+
return texts.flatMap((optionTexts, index) => {
|
|
61
|
+
const isMatch = optionTexts.some(
|
|
62
|
+
text => match({ text, query, insideWords, caseSensitive }).length,
|
|
63
|
+
)
|
|
64
|
+
return isMatch ? [index] : []
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* isPerfectMatch
|
|
70
|
+
* Checks whether the query covers the whole text.
|
|
71
|
+
*
|
|
72
|
+
* @param {object} opts
|
|
73
|
+
* @param {string} opts.text Text to compare
|
|
74
|
+
* @param {string} opts.query Typed query
|
|
75
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
76
|
+
*/
|
|
77
|
+
function isPerfectMatch({ text, query, caseSensitive }) {
|
|
78
|
+
if (text.length !== query.length) return false
|
|
79
|
+
|
|
80
|
+
const ranges = match({ text, query, insideWords: true, caseSensitive })
|
|
81
|
+
if (!ranges.length) return false
|
|
82
|
+
return ranges[0][0] === 0 && ranges[ranges.length - 1][1] === text.length
|
|
83
|
+
}
|