fix: match filenames against both animal_id_string and animal_name

Multi-word names ("Rat A") now allow any word separator in the
filename — space, underscore, or dash — so "Rat_A_session.csv"
and "Rat-A-data.bin" both match. Single-word names and numeric IDs
are unchanged. Partial overlaps still rejected ("Rat" won't hit
"Rationale"). Tests: 5 new name-based match cases (24 total).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Experiments DB Dev
2026-04-23 13:33:10 -04:00
parent daf36908ef
commit 66e79c1780
2 changed files with 70 additions and 7 deletions
+21 -7
View File
@@ -9,15 +9,29 @@ function escapeRegex(str) {
/**
* Build a regex that matches an animal's identifier in a filename.
* Matches only when the ID is not surrounded by other alphanumeric chars,
* so "2852" matches "rat_2852_session" but not "28520" or "X2852Y".
* Tries both animal_id_string and animal_name (OR logic).
*
* Rules:
* - Match must not be preceded or followed by an alphanumeric char,
* so "2852" matches "rat_2852_session" but not "28520" or "X2852Y".
* - Multi-word names ("Rat A") allow any separator (space, dash, underscore)
* between words, so "Rat A" matches "rat_a_session" and "Rat-A-data".
*/
function buildAnimalRegex(animal) {
const ids = [];
if (animal.animal_id_string) ids.push(escapeRegex(animal.animal_id_string));
if (animal.animal_name) ids.push(escapeRegex(animal.animal_name));
if (ids.length === 0) return null;
return new RegExp(`(?<![a-zA-Z0-9])(${ids.join('|')})(?![a-zA-Z0-9])`, 'i');
const patterns = [];
if (animal.animal_id_string) {
patterns.push(escapeRegex(animal.animal_id_string));
}
if (animal.animal_name) {
const words = animal.animal_name.trim().split(/\s+/).map(escapeRegex);
// Single-word name: use as-is. Multi-word: allow space/dash/underscore between words.
patterns.push(words.length === 1 ? words[0] : words.join('[\\s_\\-]+'));
}
if (patterns.length === 0) return null;
return new RegExp(`(?<![a-zA-Z0-9])(${patterns.join('|')})(?![a-zA-Z0-9])`, 'i');
}
function classifyFile(filename) {