Search is still hard

This commit is contained in:
dirwin31
2026-08-27 18:11:03 -07:00
parent 695573c057
commit c02bb05113
2 changed files with 30 additions and 10 deletions
@@ -151,18 +151,20 @@ export function routeMatchesSearch(route, searchQuery) {
compactQuery.length >= 8 || (compactQuery.length >= 4 && /\d/.test(compactQuery) && /[a-f]/.test(compactQuery))
))
const valuesFor = key => Array.isArray(searchIndex[key]) ? searchIndex[key] : []
const searchValues = [
...valuesFor("titles"),
...(isDateQuery ? valuesFor("dates") : []),
...(isTimeQuery ? valuesFor("times") : []),
...(isIdQuery ? valuesFor("ids") : []),
]
return searchValues.some(value => {
const matchesValue = (value, dateValue = false) => {
if (value.includes(normalizedQuery)) return true
const searchTokens = value.split(" ").filter(Boolean)
return queryTokens.every(queryToken => searchTokens.some(searchToken => searchToken.startsWith(queryToken)))
})
return queryTokens.every(queryToken => searchTokens.some(searchToken => {
// In a date query, "20" is a day prefix, not a match for the year "2026".
if (dateValue && /^\d{1,2}$/.test(queryToken) && /^\d{4}$/.test(searchToken)) return false
return searchToken.startsWith(queryToken)
}))
}
return valuesFor("titles").some(value => matchesValue(value))
|| (isDateQuery && valuesFor("dates").some(value => matchesValue(value, true)))
|| (isTimeQuery && valuesFor("times").some(value => matchesValue(value)))
|| (isIdQuery && valuesFor("ids").some(value => matchesValue(value)))
}
export function buildRouteView(routes, options = {}) {
@@ -173,6 +173,24 @@ def test_search_matches_partial_title_tokens_and_friendly_dates_as_the_user_type
]
def test_partial_day_does_not_match_the_four_digit_year():
result = evaluate('''
const routes = Array.from({ length: 9 }, (_, offset) => {
const day = 19 + offset
return route(`aug-${day}`, `2026-08-${day}T12:00:00Z`)
})
return {
partial: buildRouteView(routes, { searchQuery: "aug 2" }).matching.map(item => item.name),
complete: buildRouteView(routes, { searchQuery: "aug 20" }).matching.map(item => item.name),
}
''')
assert result == {
"partial": ["aug-27", "aug-26", "aug-25", "aug-24", "aug-23", "aug-22", "aug-21", "aug-20"],
"complete": ["aug-20"],
}
def test_route_search_input_updates_on_every_keystroke():
source = COMPONENT_PATH.read_text(encoding="utf-8")