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
Maintainers
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.
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:
- Sanitizes the input string.
- Adds a scanner-specific prefix (or reserved-specific prefix, if provided).
- Converts the string into a series of numeric values using
TextEncoder
. - Joins the numeric values with a defined separator to create the final encoded string.
Decoding
When you use readCode
, the function:
- Splits the encoded string by the defined separator into numeric parts.
- Decodes the numeric values back into their original characters using
TextDecoder
. - Checks the prefix to verify the encoding type.
- 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.