@zuke/qr

← API reference

`@zuke/qr` — QR codes for any data, from a Zuke build, with no dependency.

Symbols

#

ErrorCorrectionLevel

type
type ErrorCorrectionLevel = L | M | Q | H

A QR error-correction level, from "L" (~7% recoverable) to "H" (~30%).

#

MAX_QUIET_ZONE

const
const MAX_QUIET_ZONE: 16

The widest quiet zone accepted; the specification asks for 4.

#

QrCapacityError

class
class QrCapacityError extends Error

Thrown when the text does not fit the largest symbol at the requested level.

MemberSignatureDoc
name name: string The error's class name, for instanceof-free identification.
#

QrCode

interface
interface QrCode

A finished QR code, as the caller sees it.

MemberSignatureDoc
version version: number The symbol version (1–40).
size size: number The side length in modules (version * 4 + 17).
errorCorrection errorCorrection: ErrorCorrectionLevel The error-correction level actually used (boosting can raise the requested one).
mask mask: number The mask pattern (0–7) the penalty score selected.
modules modules: unknown The modules, modules[y][x], true for dark.
#

QrPrintOptions

interface
interface QrPrintOptions

Options for QrTasks.print beyond the settings lambda.

MemberSignatureDoc
write? write?: unknown Where each rendered line goes (default console.log).
#

QrSettings

class
class QrSettings

Configuration for encoding and rendering a QR code, set through a lambda passed to a QrTasks method. Every setter returns this so calls chain.

MemberSignatureDoc
errorCorrection errorCorrection(level: ErrorCorrectionLevel): this Set the error-correction level: "L" (~7%), "M" (~15%), "Q" (~25%) or "H" (~30%).
boostErrorCorrection boostErrorCorrection(enabled?: boolean): this Whether to raise the error-correction level when a higher one fits the same version. On by default; turn it off to get exactly the level asked for.
quietZone quietZone(modules: number): this Set the width of the light border around the symbol, in modules (0 to MAX_QUIET_ZONE).
invert invert(enabled?: boolean): this Swap the colours: light modules become blocks and dark ones spaces. Use it on a terminal whose text is light on dark, where the default rendering shows the code with dark and light exchanged.
compact compact(enabled?: boolean): this Whether to draw two module rows per line with ▀/▄/█ (the default, which keeps a version-3 code under 20 lines) or one row per line with a two-character ██ cell per module.
errorCorrection_ errorCorrection_: ErrorCorrectionLevel The requested error-correction level (default "M").
boostErrorCorrection_ boostErrorCorrection_: boolean Raise the level for free when the chosen version has room (default true).
quietZone_ quietZone_: number Light modules drawn around the symbol, in modules (default 2; the spec asks for 4).
invert_ invert_: boolean Draw dark modules as spaces and light ones as blocks (default false).
compact_ compact_: boolean Pack two module rows into one text line with half-block characters (default true).
#

QrTasks

const
const QrTasks: QrTasksApi

Task-shaped QR code operations. Each method takes the text to encode (a URL, usually) and an optional settings lambda:

import { QrTasks } from "@zuke/qr";

QrTasks.print("https://zuke.build", (s) => s.errorCorrection("Q"));
#

QrTasksApi

interface
interface QrTasksApi

The surface of QrTasks.

MemberSignatureDoc
encode encode(text: string, configure?: Configure<QrSettings>): QrCode Encode text (as UTF-8, byte mode) into a QrCode matrix.
renderLines renderLines(text: string, configure?: Configure<QrSettings>): string[] Encode text and render it as terminal lines (see QrSettings.compact).
render render(text: string, configure?: Configure<QrSettings>): string Encode text and render it as one newline-joined string.
print print(text: string, configure?: Configure<QrSettings>, options?: QrPrintOptions): void Encode text and write each rendered line through options.write (default console.log).