feat(exercises): add multi-token, accent-insensitive and multilingual exercise search

This commit is contained in:
wagenheimer committed 2026-08-24 14:32:00 -03:00
1 parent f8024d344d
commit 4df4406139
4 files changed
+135 -6

No files matched your search

+31
View File
@@ -77,3 +77,34 @@ export const isBodyweightEq = idOrEx =>
// down on the first `ex.n`.
export const exOr = id => EXIDX[id] ||
{ id, n: t('Unknown exercise'), bp: '', tg: '', eq: '', sm: [], st: [], missing: true }
// Normalizes text by lowercasing and stripping diacritics/accents (e.g. "elevação" -> "elevacao")
export const normalizeStr = s => (s || '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
// Multi-token, accent-insensitive and multilingual exercise search.
// Matches when all whitespace-separated words in the query appear anywhere in the exercise's
// name, equipment, target muscle, body part (both in English and translated to active language),
// secondary muscles or description.
export function matchExercise(e, query) {
if (!query) return true
const tokens = normalizeStr(query).split(/\s+/).filter(Boolean)
if (!tokens.length) return true
const corpus = normalizeStr([
e?.n || '',
e?.tg || '',
t(e?.tg || ''),
e?.eq || '',
t(e?.eq || ''),
e?.bp || '',
t(e?.bp || ''),
...(Array.isArray(e?.sm) ? e.sm : []),
...(Array.isArray(e?.sm) ? e.sm.map(m => t(m)) : []),
e?.desc || ''
].join(' '))
return tokens.every(tok => corpus.includes(tok))
}
+100
View File
@@ -0,0 +1,100 @@
import { describe, it, expect } from 'vitest'
import { matchExercise, normalizeStr } from './exercises.js'
import { _setLangState } from './i18n-core.js'
describe('normalizeStr', () => {
it('handles null, undefined and empty strings', () => {
expect(normalizeStr(null)).toBe('')
expect(normalizeStr(undefined)).toBe('')
expect(normalizeStr('')).toBe('')
})
it('lowercases text and removes diacritics / accents', () => {
expect(normalizeStr('Elevação Lateral')).toBe('elevacao lateral')
expect(normalizeStr('SUPINO INCLINADO COM HALTERES')).toBe('supino inclinado com halteres')
expect(normalizeStr('Tríceps & Panturrilhas')).toBe('triceps & panturrilhas')
expect(normalizeStr('Quadríceps / Glúteos')).toBe('quadriceps / gluteos')
})
})
describe('matchExercise', () => {
const benchPress = {
id: '0025',
n: 'barbell bench press',
bp: 'chest',
tg: 'pectorals',
eq: 'barbell',
sm: ['triceps', 'deltoids'],
desc: 'Classic chest exercise using a barbell on a flat bench.'
}
const lateralRaise = {
id: '0283',
n: 'dumbbell lateral raise',
bp: 'shoulders',
tg: 'delts',
eq: 'dumbbell',
sm: ['traps'],
desc: 'Shoulder isolation movement.'
}
it('returns true for empty or whitespace-only query', () => {
expect(matchExercise(benchPress, '')).toBe(true)
expect(matchExercise(benchPress, ' ')).toBe(true)
expect(matchExercise(benchPress, null)).toBe(true)
})
it('matches exact and partial words in exercise name regardless of case', () => {
expect(matchExercise(benchPress, 'bench')).toBe(true)
expect(matchExercise(benchPress, 'BENCH')).toBe(true)
expect(matchExercise(benchPress, 'barbell')).toBe(true)
expect(matchExercise(benchPress, 'press')).toBe(true)
expect(matchExercise(benchPress, 'squat')).toBe(false)
})
it('matches multiple tokens in ANY order (not just sequential)', () => {
// "bench barbell" is reversed order compared to "barbell bench press"
expect(matchExercise(benchPress, 'bench barbell')).toBe(true)
expect(matchExercise(benchPress, 'press bench barbell')).toBe(true)
expect(matchExercise(benchPress, 'barbell press chest')).toBe(true)
expect(matchExercise(benchPress, 'bench squat')).toBe(false)
})
it('matches target muscle, equipment, secondary muscles and description', () => {
expect(matchExercise(benchPress, 'pectorals')).toBe(true)
expect(matchExercise(benchPress, 'triceps barbell')).toBe(true)
expect(matchExercise(benchPress, 'flat bench')).toBe(true)
expect(matchExercise(lateralRaise, 'dumbbell shoulder')).toBe(true)
})
it('matches accent-insensitively', () => {
const customEx = {
id: 'custom-1',
n: 'Elevação de Panturrilha',
bp: 'lower legs',
tg: 'calves',
eq: 'body weight',
desc: 'Exercício para panturrilhas em pé.'
}
expect(matchExercise(customEx, 'elevacao')).toBe(true)
expect(matchExercise(customEx, 'elevação')).toBe(true)
expect(matchExercise(customEx, 'panturrilha elevacao')).toBe(true)
expect(matchExercise(customEx, 'ELEVACAO PE')).toBe(true)
})
it('matches translated UI terms when a language is active', () => {
_setLangState('pt', {
chest: 'peito',
barbell: 'barra',
dumbbell: 'halteres',
shoulders: 'ombros'
}, null)
// "peito" is the translated bp, "barra" is the translated eq
expect(matchExercise(benchPress, 'peito')).toBe(true)
expect(matchExercise(benchPress, 'barra')).toBe(true)
expect(matchExercise(benchPress, 'peito barra bench')).toBe(true)
expect(matchExercise(lateralRaise, 'halteres ombros')).toBe(true)
})
})