@zenuinebit/barcode-scan
v0.1.0
Published
Parse and validate scanned barcodes (EAN-13, EAN-8, UPC-A, GS1-style weighted labels) and handle keyboard-wedge scanners.
Readme
@zenuinebit/barcode-scan
Check and read scanned barcodes, and tell scanner input from typing. No dependencies, no DOM required. Pairs with @zenuinebit/barcode, which draws them.
import { validate, parse, toGtin14, parseWeighted, listenForScans } from "@zenuinebit/barcode-scan";
validate("4006381333931"); // true (EAN-8, UPC-A, EAN-13 and GTIN-14 are checked)
parse("036000291452"); // { format: "UPC-A", code, gtin14: "00036000291452", checkDigit: 2 }
toGtin14("0036000291452"); // "00036000291452" - one key for a product, whether it was scanned as UPC-A or EAN-13
// Shop-printed price/weight labels: 2 IIIII C VVVVV C
parseWeighted("2012347012507"); // { item: "01234", value: 1250, amount: 12.5, decimals: 2 }
// USB / Bluetooth scanners that act as a keyboard (browser)
const stop = listenForScans(document, (code) => addToCart(code));How scanner detection works. A keyboard-style scanner types the whole code in a few milliseconds and ends with Enter. ScanBuffer (used by listenForScans) accepts a run of characters only when every gap is at most maxGapMs (default 50) and it ends with Enter, so a person typing slowly is ignored. The Enter that ends a scan is cancelled so it does not submit a form. If you also let people type codes by hand, add a separate input box for that.
Weighted labels. Layouts differ between shops and countries. The default is the common 2 IIIII C VVVVV C (item in digits 1-5, value in digits 7-11). Pass itemStart, itemLength, valueStart, valueLength, decimals and prefix for yours. The inner price check digit is not verified.
Not included: GS1-128 application identifiers (batch, expiry), ISBN, UPC-E expansion, and camera scanning.
