Tidy up code to normalize text, extract text helpers

This commit is contained in:
Jorge Manrubia
2025-07-01 09:42:22 +02:00
parent ffb422ac61
commit 2ab322adfe
2 changed files with 12 additions and 5 deletions
@@ -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, "")
}
}
+10
View File
@@ -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))
}