Tofu/app/tools/text/normalizeSearchText.js

14 lines
463 B
JavaScript

// Canonical form used by every text comparison of the app: lowercase, no
// diacritics, punctuation turned into single spaces. "Gnocchis a la sauge".
const DIACRITICS = /\p{Diacritic}/gu
const NON_ALPHANUMERIC = /[^\p{Letter}\p{Number}]+/gu
export function normalizeSearchText(text) {
if (typeof text !== 'string') return ''
return text
.normalize('NFD')
.replace(DIACRITICS, '')
.toLowerCase()
.replace(NON_ALPHANUMERIC, ' ')
.trim()
}