diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.js index 27ec701..197b3fc 100644 --- a/frontend/src/lib/exercises.js +++ b/frontend/src/lib/exercises.js @@ -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)) +} diff --git a/frontend/src/lib/exercises.test.js b/frontend/src/lib/exercises.test.js new file mode 100644 index 0000000..b3c7c1c --- /dev/null +++ b/frontend/src/lib/exercises.test.js @@ -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) + }) +}) diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index d0f6d36..ab3a251 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -1,7 +1,7 @@ import { useEffect, useRef, useState } from 'react' import { useStore } from './store/useStore.js' import { useUI } from './store/useUI.js' -import { EXDB, EXIDX, BODYPARTS, isCardio, isBodyweightEq, allExercises, equipmentOf, smOf } from './lib/exercises.js' +import { EXDB, EXIDX, BODYPARTS, isCardio, isBodyweightEq, allExercises, equipmentOf, smOf, matchExercise } from './lib/exercises.js' import { fmtDate, fmtNum, fmtVol, fmtDur, durPart, todayISO, uid, exCount, DAYN, MONTHS_LONG, ACCENTS } from './lib/format.js' import { lastEntryFor, bestWeightFor, buildSets, effectiveRoutineId, workoutVolume, setsDone, setsDoneActive, lastBW, supersetUnits, unitOf, setLabel, defaultConfig, cleanupSg, modeOf, effortOf, isBw, isPerSide, sideReps, workSetsDone } from './lib/history.js' import { beep, vibrate } from './lib/sound.js' @@ -422,11 +422,10 @@ function ExercisePicker({ onPick, close }) { const [bp, setBp] = useState('') // '' = all, '★' = chosen, else a body part const [eq, setEq] = useState('') // '' = any equipment const [shown, setShown] = useState(50) - const ql = q.toLowerCase().trim() const all = allExercises(st) let base = all.filter(e => (bp === '★' ? usage[e.id] : (!bp || e.bp === bp)) && - (!ql || e.n.toLowerCase().includes(ql) || e.tg.includes(ql) || e.eq.includes(ql) || (e.desc || '').toLowerCase().includes(ql))) + matchExercise(e, q)) if (bp === '★') base = [...base].sort((a, b) => (usage[b.id] - usage[a.id]) || (a.n < b.n ? -1 : 1)) const eqOpts = equipmentOf(base) // Drop the equipment filter if the search narrowed it away, so you never hit a dead end. diff --git a/frontend/src/views/Library.jsx b/frontend/src/views/Library.jsx index a3ba8cb..cb1c70e 100644 --- a/frontend/src/views/Library.jsx +++ b/frontend/src/views/Library.jsx @@ -1,6 +1,6 @@ import { useState } from 'react' import { useStore } from '../store/useStore.js' -import { EXDB, BODYPARTS, allExercises, equipmentOf } from '../lib/exercises.js' +import { EXDB, BODYPARTS, allExercises, equipmentOf, matchExercise } from '../lib/exercises.js' import { bestWeightFor } from '../lib/history.js' import { fmtNum } from '../lib/format.js' import { t } from '../lib/i18n.js' @@ -15,8 +15,7 @@ export default function Library() { const [bp, setBp] = useState('') const [eq, setEq] = useState('') const [shown, setShown] = useState(40) - const ql = q.toLowerCase().trim() - const base = allExercises(S).filter(e => (!bp || e.bp === bp) && (!ql || e.n.toLowerCase().includes(ql) || e.tg.includes(ql) || e.eq.includes(ql) || (e.desc || '').toLowerCase().includes(ql))) + const base = allExercises(S).filter(e => (!bp || e.bp === bp) && matchExercise(e, q)) const eqOpts = equipmentOf(base) // Drop the equipment filter if the search narrowed it away, so you never hit a dead end. const eqOn = eqOpts.includes(eq) ? eq : ''