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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@alekstar79/qr-code

v2.0.1

Published

A simple QR code generator library

Readme

QR Code Generator TS

npm version License GitHub TypeScript Coverage

A lightweight, dependency‑free QR code generator written in TypeScript.
It creates QR codes in PNG, SVG, or HTML (CSS Grid) formats, with full control over encoding mode, error correction, version, mask, module size, and quiet zone.

👉 Live Demo


Features

  • 🔢 Supports numeric, alphanumeric, and octet (UTF‑8) modes.
  • 🛡️ All four error correction levels (L, M, Q, H) – automatically chooses the best if not specified.
  • 📦 Works with any version from 1 to 40 – picks the smallest possible version automatically.
  • 🎭 Mask pattern selection (0‑7) with automatic best‑mask evaluation.
  • 🖼️ Output formats: PNG (Canvas), SVG, HTML (responsive CSS Grid), or NONE (only matrix).
  • 📏 Customizable module size (modsize) and margin (quiet zone).
  • 📥 Built‑in download() method to save the generated QR code as a file.
  • 🌐 Full TypeScript support with clear types.
  • 🔧 No external dependencies – pure JavaScript/TypeScript.

Installation

Using npm

npm install @alekstar79/qr-code-generator

Then import it in your project:

import { makeQRCode } from '@alekstar79/qr-code'

Usage

Basic Example

import { makeQRCode } from '@alekstar79/qr-code'

const qr = makeQRCode('https://example.com')
document.body.appendChild(qr.result) // result is an HTMLCanvasElement

With Options

import { makeQRCode, QROptions } from '@alekstar79/qr-code'

const options: QROptions = {
  mode: -1,           // auto (numeric, alphanumeric, or octet)
  eccl: 2,            // High error correction
  version: -1,        // auto version
  mask: -1,           // auto mask
  image: 'SVG',       // output format: 'PNG', 'SVG', 'HTML', 'NONE'
  modsize: 8,         // module size in pixels (only for PNG/SVG)
  margin: 4           // quiet zone in modules
}

const qr = makeQRCode('Hello, World!', options)
document.getElementById('qr-container')!.appendChild(qr.result)

Using the Returned Object

import { makeQRCode, QRCode } from '@alekstar79/qr-code'

const qr: QRCode = makeQRCode('My data')

// Access generated matrix (0/1/null)
console.log(qr.matrix)

// Change output format after generation
qr.image = 'HTML'          // updates qr.result accordingly

// Download as file
qr.download('my-qr.png')   // uses current format
qr.download('my-qr.svg', 'SVG') // specify format

API Reference

makeQRCode(text: string, options?: QROptions): QRCode

makeQRCode(options: QROptions): QRCode

Generates a QR code and returns an object with the following properties and methods.

QROptions

| Property | Type | Default | Description | |-----------|------------------------------------------------------|---------|------------------------------------------------------------------------------------------| | text | string | '' | The data to encode. | | mode | Mode (-1 \| 1 \| 2 \| 4) | -1 | Encoding mode: 1 = numeric, 2 = alphanumeric, 4 = octet (UTF‑8), -1 = auto. | | eccl | Eccl (-1 \| 0 \| 1 \| 2 \| 3) | -1 | Error correction level: 0 = M, 1 = L, 2 = H, 3 = Q, -1 = auto (tries H first). | | version | number (-1 \| 1–40) | -1 | QR code version (size). -1 selects the smallest possible version. | | mask | Mask (-1 \| 0–7) | -1 | Mask pattern. -1 selects the best mask automatically. | | image | ImageFormat ('PNG' \| 'SVG' \| 'HTML' \| 'NONE') | 'PNG' | Output format. 'NONE' skips image generation (only matrix is available). | | modsize | number | 4 | Module size in pixels (for PNG/SVG). Ignored for 'HTML'. | | margin | number | 4 | Quiet zone width in modules. |

Returned QRCode object

| Property | Type | Description | |--------------------------------|----------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | text | string | The original input text. | | mode | Mode | The encoding mode used. | | eccl | Eccl | Error correction level used. | | version | number | QR code version. | | mask | Mask | Mask pattern applied. | | matrix | (number \| null)[][] | The QR matrix (0 = white, 1 = black, null = reserved areas). | | modsize | number | Module size actually used (may differ from input if auto). | | margin | number | Margin size actually used. | | result | HTMLElement \| HTMLCanvasElement \| SVGElement \| null | The generated image element. null if an error occurred or format is 'NONE'. | | error | string | Error code (empty if no error). | | errorSubcode | string | Additional error details. | | download(filename?, format?) | void | Triggers download of the QR code image. If no filename given, default is qrcode.png, qrcode.svg, or qrcode.html based on format. If format is provided, it temporarily changes the image format for the download. |

Changing image property

You can change the output format after generation by assigning a new value to qr.image. This will update qr.result accordingly.

qr.image = 'SVG'      // qr.result becomes an SVGElement
qr.image = 'HTML'     // qr.result becomes a HTMLDivElement with CSS Grid

Error Handling

If an error occurs, qr.error contains the name of the property that caused the issue (e.g., 'text', 'version'), and qr.errorSubcode gives more details.

Common error codes:

| error | errorSubcode | Meaning | |-------------|----------------|-------------------------------------------------------| | 'text' | '1' | Invalid text format. | | 'text' | '2' | Empty text. | | 'text' | '3' | Text contains invalid characters for the chosen mode. | | 'mode' | '1' | Unsupported encoding mode. | | 'version' | '1' | Text too long for any version. | | 'version' | '2' | Invalid version number. | | 'version' | '3' | Text too long for the specified version. | | 'eccl' | '1' | Invalid error correction level. | | 'mask' | '1' | Invalid mask pattern. | | 'image' | '1' | Unsupported image format. | | 'modsize' | '1' | Invalid module size. | | 'margin' | '1' | Invalid margin size. |

Additionally, library‑specific error codes:

| error | Meaning | |------------------------------|-------------------------------------------------------------| | 'ERR_TEXT_EMPTY' | Text is empty. | | 'ERR_INVALID_CHAR' | Text contains unsupported characters for the selected mode. | | 'ERR_VERSION_OUT_OF_RANGE' | Invalid version number. |


Browser Support

| Chrome | Firefox | Edge | Safari | Opera | |--------|---------|-------|--------|-------| | ≥ 106 | ≥ 105 | ≥ 106 | ≥ 15 | ≥ 92 |

Works in all modern browsers that support Canvas, SVG, and CSS Grid (for the HTML output). The library itself is pure ECMAScript and does not rely on any polyfills.


Development

Project Structure

qr-code/
├── src/
│   ├── constants.ts         # QR code tables and constants
│   ├── qr-code-generator.ts # Core QR encoding logic
│   ├── image-renderer.ts    # Converts matrix to PNG/SVG/HTML
│   ├── types.ts             # TypeScript definitions
│   ├── index.ts             # Public API (makeQRCode)
│   ├── main.ts              # Demo app (Vite)
│   └── css/                 # Demo styles
├── test/                    # Unit tests (Vitest + JSDOM)
├── lib/                     # Build output (library)
├── dist/                    # Build output (demo)
├── package.json
├── tsconfig.json
├── vite.config.ts           # Demo build config
└── vite.config.lib.ts       # Library build config

Build Commands

# Build library (output to lib/)
yarn build:lib

# Build demo (output to dist/)
yarn build:demo

# Run development server (demo)
yarn dev

# Run tests
yarn test

# Run tests with coverage
yarn test:coverage

Testing

The test suite uses Vitest with JSDOM. Run yarn test to execute all tests.


License

MIT © alekstar79


Useful Links