feat(items): read quantity, unit and notes from natural-language input

"400 de jamon" adds Jamón · 400, "Queso crema. Solo Tregar" adds a note,
"leche, pan y 2 pascualinas" adds three items and a pasted list adds one per
line. Units are normalized (½ kg, 1,5 l, 2 paquetes), common accents are
restored, and a pending duplicate gets its quantity summed instead of a new
row. A preview under the input shows the reading, with a way to add the text
as typed. quantityLabel now shows any quantity except a plain "1".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-20 19:10:23 -03:00
parent d02ab28fc5
commit 5abdb08204
12 changed files with 1018 additions and 28 deletions

View File

@@ -0,0 +1,61 @@
import type { ListItem } from '@superlist/shared'
import { describe, expect, it } from 'vitest'
import { findDuplicate, mergeBatch, mergeInto } from './merge'
import { parseItemInput } from './parse-item'
function item(overrides: Partial<ListItem>): ListItem {
return {
id: 'item-1',
listId: 'list-1',
name: 'Jamón',
completed: false,
boughtAt: null,
quantity: null,
notes: null,
order: 0,
createdAt: '2026-10-15T10:00:00.000Z',
updatedAt: '2026-10-15T10:00:00.000Z',
...overrides,
}
}
describe('findDuplicate', () => {
it('matches pending items ignoring case, accents and a final "s"', () => {
const jamon = item({})
const pascualinas = item({ id: 'item-2', name: 'Pascualinas' })
expect(findDuplicate([jamon, pascualinas], 'jamon')).toBe(jamon)
expect(findDuplicate([jamon, pascualinas], 'Pascualina')).toBe(pascualinas)
expect(findDuplicate([jamon], 'Queso')).toBeUndefined()
})
it('ignores items already bought', () => {
expect(findDuplicate([item({ completed: true })], 'Jamón')).toBeUndefined()
})
})
describe('mergeInto', () => {
it('sums quantities and appends new notes', () => {
expect(
mergeInto({ quantity: '400 g', notes: 'Feteado' }, { quantity: '200 g', notes: 'Cocido' }),
).toEqual({ quantity: '600 g', notes: 'Feteado · Cocido' })
})
it('leaves out what does not change', () => {
expect(
mergeInto({ quantity: '400 g', notes: 'Feteado' }, { quantity: null, notes: 'feteado' }),
).toEqual({})
})
})
describe('mergeBatch', () => {
it('folds repeated items of one input', () => {
const merged = mergeBatch(parseItemInput('pan, leche, 2 panes'))
expect(merged.map(({ name, quantity }) => ({ name, quantity }))).toEqual([
{ name: 'Pan', quantity: '3' },
{ name: 'Leche', quantity: null },
])
})
})