Tidy up code to normalize text, extract text helpers
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { debounce } from "helpers/timing_helpers"
|
||||
import { filterMatches } from "helpers/text_helpers"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "input", "item" ]
|
||||
@@ -10,7 +11,7 @@ export default class extends Controller {
|
||||
|
||||
filter() {
|
||||
this.itemTargets.forEach(item => {
|
||||
if (this.#convertDiacritics(item.innerText).toLowerCase().includes(this.inputTarget.value.toLowerCase())) {
|
||||
if (filterMatches(item.innerText, this.inputTarget.value)) {
|
||||
item.removeAttribute("hidden")
|
||||
} else {
|
||||
item.toggleAttribute("hidden", true)
|
||||
@@ -19,8 +20,4 @@ export default class extends Controller {
|
||||
|
||||
this.dispatch("changed")
|
||||
}
|
||||
|
||||
#convertDiacritics(text) {
|
||||
return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
export function isMultiLineString(string) {
|
||||
return /\r|\n/.test(string)
|
||||
}
|
||||
|
||||
export function normalizeFilteredText(string) {
|
||||
return string
|
||||
.toLowerCase()
|
||||
.normalize("NFD").replace(/[\u0300-\u036f]/g, "") // Remove diacritics
|
||||
}
|
||||
|
||||
export function filterMatches(text, potentialMatch) {
|
||||
return normalizeFilteredText(text).includes(normalizeFilteredText(potentialMatch))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user