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

babel-scanner

v1.0.3

Published

Babel Scanner is a library that encodes and decodes strings into a custom numeric format, solving keyboard layout compatibility issues (AZERTY, QWERTY, etc.) when processing barcode scans across different operating systems.

Downloads

174

Readme

Babel Scanner

Babel Scanner is a lightweight yet powerful library designed to encode strings into a numeric format using the TextEncoder and TextDecoder APIs and decode the generated codes back into their original strings. This library ensures compatibility across different keyboard layouts, such as AZERTY, QWERTY, and QWERTZ, when handling barcode scans, providing a consistent encoding and decoding mechanism.

Latest Stable Version NPM Downloads NPM Downloads Bundlephobia Size License

Found it useful? Want more updates?

Show your support by giving a :star:

Problem Solved

Barcodes, especially Code-128, are interpreted by operating systems as if they were typed from a keyboard using the system's current layout. Issues arise when a barcode generated using one layout (e.g., AZERTY) is interpreted using a different layout (e.g., QWERTY), resulting in incorrect mappings.

Babel Scanner eliminates this problem by encoding and decoding barcodes in a consistent format that is not affected by the operating system's keyboard layout. This prevents the need for complex device-specific logic in web and client applications.

Features

  • Encode strings into a numeric format using the TextEncoder API.
  • Decode numeric codes back into the original string using the TextDecoder API.
  • Ensures compatibility across different keyboard layouts (AZERTY, QWERTY, QWERTZ).
  • Lightweight and easy to integrate into web and client applications.

Installation

You can install Babel Scanner via npm:

npm install babel-scanner

Usage

Babel Scanner exposes two main functions: readCode and writeCode. These functions encode and decode strings, ensuring compatibility across different keyboard layouts. The input for readCode should be a string that was generated by writeCode. This is the only format that guarantees the functions will work correctly.

readCode

Decodes a string that was previously encoded using Babel Scanner's format based on TextDecoder.

Parameters

  • str (string): The encoded string to decode. The string should consist of numeric values separated by a specific separator and should contain the expected prefix.

Returns

  • object: An object with two properties:
    • decodedStr (string): The decoded original string.
    • isRes (boolean): A flag indicating whether the string contains the 'res' prefix.

Throws

  • Error: If the string does not contain the required scanner prefix, an error is thrown.

Example

import { readCode } from "babel-scanner";

// RES prefixed code
const resDecoded = readCode("66-83-67-65-78-82-83-80-73-78-90-73-54-54");

/*
returns {
    decodedStr: "PINZI66"
    isRes: true
}
*/

// Normal prefixed code
const decoded = readCode("66-83-67-65-78-80-73-78-90-73-54-54");

/*
returns {
    decodedStr: "PINZI66"
    isRes: false
}
*/

writeCode

Encodes a string into a numeric format using TextEncoder with an optional prefix.

Parameters

  • str (string): The string to encode.
  • prefix (object, optional): An optional object to define a specific prefix.
    • isRes (boolean): If true, the string will be encoded with a reserved-specific prefix.

Returns

  • string: The encoded string, consisting of numeric values separated by a defined delimiter.

Example


import { Prefixes, writeCode } from "babel-scanner";

const prefixPayload: Prefixes = {
    isRes: true; // If you need a reserved prefix for custom logic
}

const encoded = writeCode("PINZI66", prefixPayload);
// Returns "66-83-67-65-78-82-83-80-73-78-90-73-54-54"
import { writeCode } from "babel-scanner";

const encodedWithResPrefix = writeCode("PINZI66", { isRes: true });
// Returns "66-83-67-65-78-82-83-80-73-78-90-73-54-54"

const encodedWithoutResPrefix = writeCode("PINZI66");
// Returns "66-83-67-65-78-80-73-78-90-73-54-54"

How It Works

Encoding

When you use writeCode, the function:

  1. Sanitizes the input string.
  2. Adds a scanner-specific prefix (or reserved-specific prefix, if provided).
  3. Converts the string into a series of numeric values using TextEncoder.
  4. Joins the numeric values with a defined separator to create the final encoded string.

Decoding

When you use readCode, the function:

  1. Splits the encoded string by the defined separator into numeric parts.
  2. Decodes the numeric values back into their original characters using TextDecoder.
  3. Checks the prefix to verify the encoding type.
  4. Returns the decoded string and a flag indicating if the string was encoded with a reserved-specific prefix.

Why Use Babel Scanner?

  • Cross-layout Compatibility: Guarantees that barcodes are interpreted consistently across different keyboard layouts.
  • Simplified Input Handling: Removes the need for handling HID devices or managing keyboard layouts at the system level.
  • Lightweight: Ideal for use in both web and client-side applications, without adding heavy dependencies.

License

This project is licensed under the ISC License. See the LICENSE file for details.


Made with ❤️ by Opentrentuno.