You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sims-hq/packages/scanning/src/entry.ts

33 lines
1.1 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* The smart scan box grammar (09-UX §1.2): one input, no modes. Typed and
* scanned input both land here; the caller resolves codes against the item
* cache and scale profile.
*/
export type EntryParse =
| { kind: 'empty' }
| { kind: 'multiplier'; qty: number; rest: string }
| { kind: 'barcode'; code: string }
| { kind: 'code'; code: string }
| { kind: 'search'; text: string }
const MULTIPLIER = /^(\d{1,4}(?:\.\d{1,3})?)\*(.*)$/
export function parseEntry(raw: string): EntryParse {
const input = raw.trim()
if (input === '') return { kind: 'empty' }
const mult = MULTIPLIER.exec(input)
if (mult !== null) {
const qty = Number(mult[1])
if (qty > 0) return { kind: 'multiplier', qty, rest: mult[2]!.trim() }
}
if (/^\d+$/.test(input)) {
// 814 digits reads as a barcode/item code; shorter digits are still codes
// (quick-key PLUs); resolution order is the caller's concern.
return input.length >= 8 ? { kind: 'barcode', code: input } : { kind: 'code', code: input }
}
return { kind: 'search', text: input }
}