Skip to content

Offline gym log for Android. No account, no ads, no internet.

Match exercise names written as Name (other name)

InlitX613740d3f4ff
diff --git a/lib/services/exercise_match.dart b/lib/services/exercise_match.dart
index 5895324..eb3fa01 100644
--- a/lib/services/exercise_match.dart
+++ b/lib/services/exercise_match.dart
@@ -163,7 +163,23 @@ String? guessMuscle(String name) {
return null;
}
+final _labelled = RegExp(r'^(.+?)\s*\(([^()]+)\)\s*$');
+
Exercise? matchExercise(String name, Iterable<Exercise> pool) {
+ final whole = _matchExact(name, pool);
+ if (whole != null) return whole;
+ final parts = _labelled.firstMatch(name.trim());
+ if (parts != null) {
+ final outer = parts.group(1)!, inner = parts.group(2)!;
+ final hit = _matchExact(outer, pool) ?? _matchExact(inner, pool);
+ if (hit != null) return hit;
+ }
+ final loose = _matchLoose(name, pool);
+ if (loose != null || parts == null) return loose;
+ return matchExercise(parts.group(1)!, pool) ?? matchExercise(parts.group(2)!, pool);
+}
+
+Exercise? _matchExact(String name, Iterable<Exercise> pool) {
final key = searchKey(name);
if (key.isEmpty) return null;
@@ -187,7 +203,12 @@ Exercise? matchExercise(String name, Iterable<Exercise> pool) {
final label = exerciseName(e);
if (label != e.name && _keyOf(label) == key) return e;
}
+ return null;
+}
+Exercise? _matchLoose(String name, Iterable<Exercise> pool) {
+ final key = searchKey(name);
+ if (key.isEmpty) return null;
final q = key.split(' ').toSet();
final qMeaning = q.difference(_equipment);
if (qMeaning.isEmpty) return null;
diff --git a/test/exercise_alias_test.dart b/test/exercise_alias_test.dart
index 0ac45a6..2daaaf6 100644
--- a/test/exercise_alias_test.dart
+++ b/test/exercise_alias_test.dart
@@ -62,6 +62,19 @@ void main() {
}
});
+ test('a name copied with its translation in brackets maps to the same exercise', () {
+ const copied = {
+ 'Dumbbell Fly (Aperturas con mancuernas)': 'Dumbbell Fly',
+ 'Barbell Deadlift (Peso muerto con barra)': 'Barbell Deadlift',
+ 'Cable Pulldown (Jalón al pecho en polea)': 'Cable Pulldown',
+ 'Scapular Pull-up (Dominadas escapulares)': 'Scapular Pull-up',
+ 'Cable Pull Through (With Rope) (Pull through en polea con cuerda)': 'Cable Pull Through (With Rope)',
+ };
+ for (final entry in copied.entries) {
+ expect(matchExercise(entry.key, kExercises)?.name, entry.value, reason: entry.key);
+ }
+ });
+
test('a half-typed name still narrows down to the exercise', () {
expect(_search('lat pulld').map((e) => e.name), contains('Cable Pulldown'));
expect(_search('db bench').map((e) => e.name), contains('Dumbbell Bench Press'));