Add duplicate detection when adding items

- normalizeName() strips punctuation and normalizes whitespace/case
- findDuplicates() checks for matching normalized names
- Shows confirmation dialog if similar item exists

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Eric Wagoner
2025-12-23 23:06:06 -05:00
parent d04d6e58e2
commit 6b7858aabe

View File

@@ -993,6 +993,22 @@
return div.innerHTML;
}
function normalizeName(name) {
return name
.toLowerCase()
.replace(/[^\w\s]/g, '') // Remove punctuation
.replace(/\s+/g, ' ') // Normalize whitespace
.trim();
}
function findDuplicates(name, excludeId = null) {
const normalized = normalizeName(name);
return inventory.filter(item => {
if (excludeId && item.id === excludeId) return false;
return normalizeName(item.name) === normalized;
});
}
function updateStats() {
const total = inventory.length;
const inStock = inventory.filter(i => !i.outOfStock).length;
@@ -1225,9 +1241,20 @@
showToast('Please enter an item name');
return;
}
// Check for duplicates when adding new items
if (!editingId) {
const duplicates = findDuplicates(name);
if (duplicates.length > 0) {
const dupNames = duplicates.map(d => d.name).join(', ');
if (!confirm(`Similar item exists: "${dupNames}"\n\nAdd anyway?`)) {
return;
}
}
}
showLoading();
try {
if (editingId) {
// Update existing