A modern **symbol encoding toolkit** for the web. Currently supports **QR Code** (v1–v40 planned; v1–v10 implemented), with extensible design for adding other optical codes (Barcodes, EAN, DataMatrix, …).
npm install @pkvsinha/symbol-codecA modern symbol encoding toolkit for the web.
Currently supports QR Code (v1–v40 planned; v1–v10 implemented), with extensible design for adding other optical codes (Barcodes, EAN, DataMatrix, …).
- ✅ Written in TypeScript
- ✅ Zero runtime dependencies
- ✅ ESM, CJS, and UMD/IIFE builds
- ✅ Works in Node.js and browsers (via CDN or bundlers)
- ✅ Modular & tree-shakable
- ✅ Correct Reed–Solomon, masking, format/version info, placement order
- ✅ Supports ECC levels L/M/Q/H, version auto-selection, mask scoring
- ✅ Helpers for common use-cases (e.g. UPI payments, text, URLs)
- 🚧 Roadmap: add barcodes (EAN-13, Code128, UPC-A), DataMatrix, PDF417
---
``bash`
npm install @pkvsinha/symbol-codec
`js
const { encodeJS, renderCanvas } = require("@pkvsinha/symbol-codec");
const { grid } = encodeJS("HELLO WORLD", { ecc: "M", version: "auto" });
// renderCanvas is browser-only; in Node you can write PNG/SVG manually
`
`ts
import { encodeJS } from "@pkvsinha/symbol-codec";
import { renderSVG } from "@pkvsinha/symbol-codec/render";
const { grid } = encodeJS("https://heypkv.com", { ecc: "Q" });
const svg = renderSVG(grid, 8, 4); // scale=8, margin=4
document.body.innerHTML = svg;
`
`html
`
encodeJS(input: string, options?: EncodeOptions): EncodeResult
Encodes a string into a QR Code matrix.
ecc: Error correction level 'L'|'M'|'Q'|'H' (default 'M')
version: Version number 1..40 or 'auto' (default 'auto')
mask: Force mask 0..7 or 'auto' (default 'auto')
`ts`
{
grid: { size: number; data: Uint8Array }, // size x size, row-major
version: number,
mask: number,
ecc: 'L'|'M'|'Q'|'H'
}
- renderCanvas(canvas: HTMLCanvasElement, grid, scale, margin)renderSVG(grid, scale, margin): string
-
Both accept a grid (from encodeJS), a module scale, and margin modules.
helpers.makeUPIPayload(address, name, amount, currency, note)
More to be added for common patterns (URLs, WiFi, contact vCards).
- ESM: dist/esm → modern bundlersdist/cjs
- CJS: → Node.js requiredist/types
- Types: → .d.tsdist/umd/symbol-codec.min.js
- UMD/IIFE: → global window.SymbolCodec
#### CDN fields in package.json:
- unpkg: dist/umd/symbol-codec.min.jsdist/umd/symbol-codec.min.js
- jsdelivr:
`ts
import { encodeJS } from "@pkvsinha/symbol-codec";
import { renderSVG } from "@pkvsinha/symbol-codec/render";
const payload =
"upi://pay?pa=prashant@oksbi&pn=HEYPKV&am=990&cu=INR&tn=Invoice%20INV-1042";
const { grid } = encodeJS(payload, { ecc: "M", version: "auto" });
const svg = renderSVG(grid, 8, 4);
console.log(svg);
`
`bash``
git clone
cd symbol-codec
npm install
npm run build # builds esm/cjs/types/umd
npm run dev # runs vite dev server with examples
npm run size-check # prints bundle sizes
- ✅ QR Code v1–v10 basic support (text, URLs, UPI)
- ✅ Correct RS error correction and mask selection
- ✅ ESM/CJS/UMD builds
- 🚧 Expand QR to full v1–v40
- 🚧 Add barcodes: EAN-13, UPC-A, Code128
- 🚧 Add DataMatrix, PDF417
- 🚧 Add optimal DP segmentation for mode mixes
MIT © Prashant Sinha