npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@use-symbology-scanner/vanilla

v1.1.0

Published

A utility for detecting barcode scanner input in the browser.

Downloads

52

Readme

@use-symbology-scanner

Table of contents

Introduction

This package provides a simple way to listen for scanner input in the browser. It works by listening for keydown events and matching the input against a set of symbologies.

Installation

This package comes in two flavours; react and vanilla. The react package is a wrapper around the vanilla package that provides a React hook for listening to scanner input.

Vanilla

npm install @use-symbology-scanner/vanilla
yarn add @use-symbology-scanner/vanilla
pnpm add @use-symbology-scanner/vanilla

React

npm install @use-symbology-scanner/react
yarn add @use-symbology-scanner/react
pnpm add @use-symbology-scanner/react

Usage

import { useSymbologyScanner } from '@use-symbology-scanner/react';

export const App = () => {
    const ref = useRef(null)

    const handleSymbol = (symbol, matchedSymbologies) => {
        console.log(`Scanned ${symbol}`)
    }

    useSymbologyScanner(handleSymbol, { target: ref })

    return (
        <div ref={ref}>
        </div>
    )
}
<!-- index.html -->
<div id="el"></div>
// script.js
const el = document.getElementById('id')

const handleSymbol = (symbol, matchedSymbologies) => {
    console.log(`Scanned ${symbol}`)
}

const scanner = new SymbologyScanner(handleSymbol, {}, el)

scanner.destroy() // clean up listeners

Options

| Property | Type | Description | Default Value | | --- | --- | --- | --- | | target | EventTarget | DOM element to listen for keydown events in. | window.document | | symbologies | Array<Symbology, StandardSymbologyKey> | Symbologies to match against. | STANDARD_SYMBOLOGIES | | enabled | boolean | Whether or not the scanner is enabled. | true | | eventOptions | object | Options to pass to the addEventListener call. | { capture: false, passive: true } | | preventDefault | boolean | Whether or not to call preventDefault on the event. | false | | ignoreRepeats | boolean | Whether or not to ignore repeated keydown events. | true | | scannerOptions | object | Options that describe the behaviour of the hardware scanner. | { prefix: '', suffix: '', maxDelay: 20 } |

Symbologies

A Symbology is a defined method of representing numeric or alphabetic digits using bars and spaces that are easily scanned by computer systems. By default, all of the standard symbologies in the table below are matched against the input.

Standard Symbologies

| Symbology | Allowed Characters | Min Length | Max Length | | --- | --- | --- | --- | | UPC-A | 0-9 | 12 | 12 | | UPC-E | 0-9 | 6 | 6 | | EAN-8 | 0-9 | 8 | 8 | | EAN 13 | 0-9 | 13 | 13 | | Codabar | 0-9, -, ., $, :, /, + | 4 | | | Code 11 | 0-9, - | 4 | | | Code 39 | 0-9, A-Z, , $, %, *, +, -, ., / | 1 | | | Code 93 | 0-9, A-Z, , $, %, +, -, . | 1 | | | Code 128 | 0-9, A-Z, , $, %, *, +, -, ., / | 1 | | | Code 25 Interleaved | 0-9 | 1 | | | Code 25 Industrial | 0-9 | 1 | | | MSI Code | 0-9 | 1 | | | QR Code | 0-9, A-Z, , $, %, *, +, -, ., /, : | 1 | | | PDF417 | 0-9, A-Z, , $, %, *, +, -, ., /, : | 1 | | | Data Matrix | 0-9, A-Z, , $, %, *, +, -, ., /, : | 1 | | | Aztec | 0-9, A-Z, , $, %, *, +, -, ., /, : | 1 | | | Dot Code | 0-9, A-Z, , $, %, *, +, -, ., /, : | 1 | |

In order to only match against a subset of the standard symbologies, you can pass in an array of symbologies to the symbologies option:

import { STANDARD_SYMBOLOGY_KEYS } from '@use-symbology-scanner/core';

const symbologies = [
    STANDARD_SYMBOLOGY_KEYS['EAN-8'],
    STANDARD_SYMBOLOGY_KEYS['EAN-13'],
]

Missing a common symbology or an issue with one of the standard symbologies? Feel free to open an issue or a pull request. Alternatively, you can define your own custom symbologies.

Custom Symbologies

You can also define your own custom symbologies to match against the input.

| Property | Type | Description | | --- | --- | --- | | name | string | Name of the symbology. | | allowedCharacters | RegExp | Allowed characters in the symbology. | | minLength | number | Minimum length of the symbology. | | maxLength | number | Maximum length of the symbology. |

allowedCharacters must only contain a character class. For example, [0-9] is valid, but 0-9 is not. You can define a custom symbology like so:

import { Symbology } from '@use-symbology-scanner/core';

const customSymbology = new Symbology({
    name: 'Custom Symbology',
    allowedCharacters: '[0-9]',
    minLength: 1,
    maxLength: 10
})

Supported hardware

This library has not yet been tested on any hardware. If you have a scanner that you would like to test this library with, please open an issue or a pull request. Generally, this library should work with any hardware that emits keydown events. You can tweak the scannerOptions to match the behaviour of your hardware. For example, if your scanner emits a newline character after each scan, you can configure the scannerOptions like so:

const scannerOptions = {
    prefix: '',
    suffix: '\n',
    maxDelay: 20
}

Bluetooth scanners may require a longer maxDelay value due to latency. If you are using a bluetooth scanner, try increasing the maxDelay value to 100 or 200.

Contributing

Development

git clone
cd @use-symbology-scanner/core
yarn install
yarn build

Testing

yarn test

License

MIT