qrmake 1.0.0-test → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ All notable changes to this package are documented here. This project follows [Semantic Versioning](https://semver.org/).
4
+
5
+ ## 1.0.0 - Unreleased
6
+
7
+ First release.
8
+
9
+ - `qrmake()` / `encode()`: a from-scratch ISO/IEC 18004 QR encoder. Versions 1-40, error correction L/M/Q/H, Reed-Solomon over GF(256), all 8 mask patterns with penalty scoring, BCH-coded format and version information.
10
+ - Automatic smallest version, and optimal splitting of text into numeric, alphanumeric and byte segments. Optional kanji mode (`kanji: true`) and UTF-8 ECI header (`eci: true`).
11
+ - Options to force `version`, `mask`, `mode` and `ecl`, limit `minVersion`/`maxVersion`, or raise the level for free with `boostEcl`.
12
+ - Input as a string (UTF-8), a `Uint8Array` of raw bytes, or an array of explicit segments.
13
+ - Returns a `QRCode` matrix with `version`, `size`, `ecl`, `mask`, `segments`, `modules`, `get(x, y)` and `toArray()`.
14
+ - `toSVG()`: one compact `<path>`, colours, transparent background, margin, scale or size, rounded modules (`radius`), accessible `title`, XML-escaped attributes.
15
+ - `toPNG()`: synchronous 1-bit PNG with a built-in deflate encoder; no zlib, canvas or `Buffer`. Hex colours with alpha, exact `size` in pixels.
16
+ - `toDataURL()` (PNG or SVG), `toTerminal()` (half-block, `invert`, `small`, `ansi`), `toText()`.
17
+ - Payload helpers with proper escaping: `wifi()`, `email()`, `sms()`, `tel()`, `geo()`, `url()`, `mecard()`, `vcard()`.
18
+ - `capacity()` for the characters that fit per mode, `isQRCode()`, `QRError`.
19
+ - `qrmake` CLI: print to the terminal or write `.svg`, `.png` and `.txt` files; reads stdin.
20
+ - ESM + CommonJS, TypeScript types, zero dependencies.
package/README.md CHANGED
@@ -1,18 +1,664 @@
1
1
  # qrmake
2
2
 
3
- Generate QR codes as SVG, PNG or terminal text, with zero dependencies. Work in progress.
3
+ Make QR codes as SVG, PNG, data URLs or terminal text, with no dependencies.
4
4
 
5
- > This is an early placeholder release. The full API is coming soon.
5
+ [![npm version](https://img.shields.io/npm/v/qrmake.svg)](https://www.npmjs.com/package/qrmake) [![bundle size](https://img.shields.io/bundlephobia/minzip/qrmake)](https://bundlephobia.com/package/qrmake) [![license](https://img.shields.io/npm/l/qrmake.svg)](LICENSE)
6
+
7
+ qrmake turns text, links, Wi-Fi details or contact cards into QR codes. Every function is synchronous and works the same in Node.js, Deno, Bun, browsers and workers. Use it to save QR code images, show them on a web page, print them in a terminal, or make them from the command line.
8
+
9
+ ## Features
10
+
11
+ - **Follows the QR code standard** (ISO/IEC 18004): all sizes (versions 1-40), all four error correction levels (L, M, Q, H), all 8 masks. Tested against published reference data and read back with OpenCV.
12
+ - **Smallest code for your data**: text is split into numeric, alphanumeric and byte parts automatically. Kanji mode is optional.
13
+ - **Unicode support**: text is encoded as UTF-8, emoji included.
14
+ - **Many output formats**: SVG (one small `<path>`), PNG, data URL, terminal text, plain text, or the raw grid of squares.
15
+ - **PNG without zlib or canvas**: a built-in, synchronous encoder that runs in every JavaScript runtime.
16
+ - **Payload helpers** for Wi-Fi, email, SMS, phone, map location, URL, MECARD and vCard, with correct escaping.
17
+ - **Command line tool**: `npx qrmake "text"`, or save to `.svg`, `.png` or `.txt`.
18
+ - **Small and typed**: about 9 KB gzipped, zero dependencies, TypeScript types included, ESM and CommonJS.
19
+
20
+ ## Installation
6
21
 
7
22
  ```sh
8
23
  npm install qrmake
9
24
  ```
10
25
 
26
+ ```sh
27
+ pnpm add qrmake
28
+ yarn add qrmake
29
+ bun add qrmake
30
+ ```
31
+
32
+ ## Quick start
33
+
34
+ ```js
35
+ import { toSVG, toPNG, toDataURL, toTerminal, wifi } from 'qrmake'
36
+
37
+ toSVG('https://example.com')
38
+ // => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33 33" width="132" ...'
39
+
40
+ toPNG('https://example.com', { scale: 8 })
41
+ // => Uint8Array [137, 80, 78, 71, ...] (the bytes of a PNG file)
42
+
43
+ toDataURL('https://example.com')
44
+ // => 'data:image/png;base64,iVBORw0KGgo...'
45
+
46
+ console.log(toTerminal('https://example.com')) // prints the code in the terminal
47
+
48
+ toSVG(wifi({ ssid: 'Home', password: 'secret' })) // scan this to join the network
49
+ ```
50
+
51
+ CommonJS works too: `const qrmake = require('qrmake')`, then `qrmake.toSVG('hi')`.
52
+
53
+ ## Table of contents
54
+
55
+ - [Usage](#usage)
56
+ - [Command line](#command-line)
57
+ - [API](#api)
58
+ - [Comparison](#comparison)
59
+ - [Migrating from qrcode](#migrating-from-qrcode)
60
+ - [How it is tested](#how-it-is-tested)
61
+ - [Notes and limitations](#notes-and-limitations)
62
+ - [Requirements](#requirements)
63
+ - [License](#license)
64
+
65
+ ## Usage
66
+
67
+ A few words used below:
68
+
69
+ - **Module**: one small square of the QR code. Each module is dark or light.
70
+ - **Quiet zone**: the empty border around the code. Scanners need it to find the code.
71
+ - **Version**: the size of the code, from 1 (21 x 21 modules) to 40 (177 x 177 modules). qrmake picks the smallest version that fits your data.
72
+
73
+ ### Save a PNG or SVG file
74
+
75
+ `toPNG()` returns the bytes of a PNG file. `toSVG()` returns the SVG as a string. Write either one to disk with `node:fs`.
76
+
77
+ ```js
78
+ import { writeFileSync } from 'node:fs'
79
+ import { toPNG, toSVG } from 'qrmake'
80
+
81
+ writeFileSync('code.png', toPNG('https://example.com', { size: 512 })) // 512 x 512 pixels
82
+ writeFileSync('code.svg', toSVG('https://example.com'))
83
+ ```
84
+
85
+ Use `scale` to set the pixels per module, or `size` to set the exact image width in pixels.
86
+
87
+ ### Show a QR code in the browser
88
+
89
+ The simplest way is a data URL in an `<img>` tag. A data URL holds the whole image inside the string, so no file or server is needed.
90
+
91
+ ```js
92
+ import { toDataURL } from 'qrmake'
93
+
94
+ const img = document.createElement('img')
95
+ img.src = toDataURL('https://example.com', { type: 'svg' }) // or leave out type for PNG
96
+ img.alt = 'QR code for example.com'
97
+ document.body.append(img)
98
+ ```
99
+
100
+ The same in React:
101
+
102
+ ```jsx
103
+ import { toDataURL } from 'qrmake'
104
+
105
+ export function QRCode({ value }) {
106
+ return <img src={toDataURL(value, { scale: 6 })} alt={`QR code: ${value}`} />
107
+ }
108
+ ```
109
+
110
+ To put the SVG straight into the page, use `toSVG()`. With `size: 0` the SVG has no fixed width or height, so it fills its container:
111
+
112
+ ```js
113
+ import { toSVG } from 'qrmake'
114
+
115
+ container.innerHTML = toSVG('https://example.com', { size: 0, title: 'Link to example.com' })
116
+ ```
117
+
118
+ To let users download a PNG, wrap the bytes in a `Blob`:
119
+
120
+ ```js
121
+ import { toPNG } from 'qrmake'
122
+
123
+ const blob = new Blob([toPNG('https://example.com', { size: 512 })], { type: 'image/png' })
124
+ link.href = URL.createObjectURL(blob)
125
+ link.download = 'qr-code.png'
126
+ ```
127
+
128
+ ### Print a QR code in the terminal
129
+
130
+ `toTerminal()` draws the code with the block characters `▀ ▄ █`. Two rows of modules fit in one line of text, so the code stays square and small.
131
+
132
+ ```js
133
+ import { toTerminal } from 'qrmake'
134
+
135
+ console.log(toTerminal('https://example.com'))
136
+ ```
137
+
138
+ ```text
139
+ █████████████████████████████
140
+ ██ ▄▄▄▄▄ ███▀▄▄ ▀▀ █ ▄▄▄▄▄ ██
141
+ ██ █ █ █ ▄█▄█▀ ▀██ █ █ ██
142
+ ██ █▄▄▄█ █ ▄▄▀█▄▀▀▄█ █▄▄▄█ ██
143
+ ██▄▄▄▄▄▄▄█ █▄█▄█ █ █▄▄▄▄▄▄▄██
144
+ ...
145
+ ```
146
+
147
+ By default the output suits a terminal with a dark background. For a light background, use `{ invert: true }`. To make the code scan on any terminal theme, use `{ ansi: true }`: it paints the code black on white with ANSI colours.
148
+
149
+ ### Share Wi-Fi login details
150
+
151
+ `wifi()` builds the text that phone cameras read as "join this network". Pass it to any renderer.
152
+
153
+ ```js
154
+ import { writeFileSync } from 'node:fs'
155
+ import { wifi, toPNG } from 'qrmake'
156
+
157
+ const text = wifi({ ssid: 'Office Guest', password: 'tea&biscuits' })
158
+ // => 'WIFI:T:WPA;S:Office Guest;P:tea&biscuits;;'
159
+
160
+ writeFileSync('wifi.png', toPNG(text, { size: 400 }))
161
+ ```
162
+
163
+ Special characters in the name or password are escaped for you:
164
+
165
+ ```js
166
+ wifi({ ssid: 'My;Net', password: 'p:w', security: 'WPA', hidden: true })
167
+ // => 'WIFI:T:WPA;S:My\\;Net;P:p\\:w;H:true;;'
168
+
169
+ wifi({ ssid: 'Cafe' }) // open network, no password
170
+ // => 'WIFI:T:nopass;S:Cafe;;'
171
+ ```
172
+
173
+ ### Share a contact card
174
+
175
+ `vcard()` makes a contact card that phones can save to the address book. `mecard()` makes a shorter card with fewer fields, so the QR code is smaller.
176
+
177
+ ```js
178
+ import { vcard, mecard, toSVG } from 'qrmake'
179
+
180
+ const card = vcard({
181
+ name: 'Jane Doe',
182
+ org: 'Acme Inc.',
183
+ title: 'Sales Manager',
184
+ phone: '+1 555 0100',
185
+ email: 'jane@example.com',
186
+ url: 'https://example.com',
187
+ })
188
+ // => 'BEGIN:VCARD\r\nVERSION:3.0\r\nN:Doe;Jane;;;\r\nFN:Jane Doe\r\nORG:Acme Inc.\r\n...'
189
+
190
+ const svg = toSVG(card)
191
+
192
+ mecard({ name: 'Jane Doe', phone: '+1 555 0100', email: 'jane@example.com' })
193
+ // => 'MECARD:N:Jane Doe;TEL:+15550100;EMAIL:jane@example.com;;'
194
+ ```
195
+
196
+ ### Choose an error correction level
197
+
198
+ A QR code stores extra recovery data. If part of the code is dirty, torn or covered, a scanner can still read it. The error correction level sets how much of the code can be damaged:
199
+
200
+ | Level | Damage it can recover from | When to use it |
201
+ |---|---|---|
202
+ | `L` | about 7% | Clean screens, when you need the smallest code |
203
+ | `M` (default) | about 15% | Most uses |
204
+ | `Q` | about 25% | Printed codes that may get dirty or worn |
205
+ | `H` | about 30% | Codes with a logo on top, or harsh places |
206
+
207
+ A higher level makes the code bigger for the same data:
208
+
209
+ ```js
210
+ import qrmake from 'qrmake'
211
+
212
+ qrmake('https://example.com/products/12345', { ecl: 'L' }).size // => 29
213
+ qrmake('https://example.com/products/12345', { ecl: 'H' }).size // => 33
214
+ ```
215
+
216
+ With `boostEcl: true`, qrmake uses a higher level for free when the data still fits in the same size:
217
+
218
+ ```js
219
+ qrmake('https://example.com', { boostEcl: true }).ecl // => 'Q'
220
+ ```
221
+
222
+ ### Encode once, render in several ways
223
+
224
+ `qrmake()` returns the encoded grid of modules. Every renderer accepts this grid as well as plain text, so you can encode once and draw it many times.
225
+
226
+ ```js
227
+ import qrmake, { toSVG, toPNG } from 'qrmake'
228
+
229
+ const qr = qrmake('https://example.com', { ecl: 'H' })
230
+ const svg = toSVG(qr, { dark: '#1a1a2e' })
231
+ const png = toPNG(qr, { size: 1024 })
232
+ ```
233
+
234
+ ### Handle data that is too long
235
+
236
+ A QR code holds a limited amount of data. If your data does not fit, qrmake throws a `QRError`. Use `capacity()` to check the limits ahead of time.
237
+
238
+ ```js
239
+ import qrmake, { capacity, QRError } from 'qrmake'
240
+
241
+ capacity(40, 'L').byte // => 2953 (the largest code holds 2953 bytes)
242
+
243
+ try {
244
+ qrmake('x'.repeat(3000), { ecl: 'L' })
245
+ } catch (error) {
246
+ error instanceof QRError // => true
247
+ error.message // => 'Data too long: needs 24020 bits, but version 40-L holds 23648'
248
+ }
249
+ ```
250
+
251
+ ## Command line
252
+
253
+ qrmake includes a command line tool. Run it with `npx`, or install the package and run `qrmake`.
254
+
255
+ ```sh
256
+ npx qrmake "https://example.com" # print in the terminal
257
+ npx qrmake -i "text" # for terminals with a light background
258
+ npx qrmake -o code.svg -e H "Hello" # save an SVG with level H
259
+ npx qrmake -o code.png --size 512 --dark "#123" "Hello" # save a 512 x 512 PNG
260
+ npx qrmake -o code.txt "Hello" # save as plain text
261
+ git remote get-url origin | npx qrmake # read the text from stdin
262
+ npx qrmake -t svg "Hello" > code.svg # write SVG (or png, text) to stdout
263
+ ```
264
+
265
+ With `-o`, the file extension sets the format: `.svg`, `.png` or `.txt`. When the text comes from stdin, one trailing newline is removed.
266
+
267
+ | Flag | Value | Default | Description |
268
+ |---|---|---|---|
269
+ | `-o`, `--output` | file | none | Write to a file. The format comes from the extension (`.svg`, `.png`, `.txt`). |
270
+ | `-t`, `--type` | `terminal`, `svg`, `png`, `text` | `terminal` | Output format. Also overrides the format from the `-o` extension. |
271
+ | `-e`, `--ecl` | `L`, `M`, `Q`, `H` | `M` | Error correction level. |
272
+ | `--qr-version` | 1-40 | smallest that fits | Force a QR version (size). |
273
+ | `-m`, `--margin` | number | 2 in the terminal, else 4 | Quiet zone in modules. |
274
+ | `-s`, `--scale` | number | 4 | Pixels per module for SVG and PNG. |
275
+ | `--size` | pixels | none | Image width and height for SVG and PNG. |
276
+ | `--dark` | colour | `#000` | Dark module colour for SVG and PNG. |
277
+ | `--light` | colour | `#fff` | Background colour for SVG and PNG, or `transparent`. |
278
+ | `-i`, `--invert` | | off | Terminal: draw the dark modules, for light backgrounds. |
279
+ | `-l`, `--large` | | off | Terminal: two characters per module instead of half blocks. |
280
+ | `-a`, `--ansi` | | off | Terminal: paint black on white with ANSI colours. |
281
+ | `-h`, `--help` | | | Show help. |
282
+ | `-v`, `--version` | | | Show the qrmake version. |
283
+
284
+ Long flags also accept `--flag=value`. The tool exits with code `0` on success, `1` when the code cannot be made (for example, the text is too long) and `2` for wrong flags or missing text.
285
+
286
+ ## API
287
+
288
+ ### qrmake(input, options?)
289
+
290
+ ```ts
291
+ function qrmake(input: QRInput, options?: QROptions): QRCode
292
+ function encode(input: QRInput, options?: QROptions): QRCode
293
+ ```
294
+
295
+ Encodes text, bytes or segments into a `QRCode` grid. `encode` is the same function. `qrmake` is also the default export, and it has every other export attached (`qrmake.toSVG`, `qrmake.wifi`, and so on).
296
+
297
+ ```js
298
+ import qrmake from 'qrmake'
299
+
300
+ const qr = qrmake('HELLO WORLD', { ecl: 'Q' })
301
+ qr.version // => 1
302
+ qr.size // => 21
303
+ qr.ecl // => 'Q'
304
+ qr.mask // => 6
305
+ qr.segments // => [{ mode: 'alphanumeric', length: 11 }]
306
+ qr.get(0, 0) // => true (dark module at column 0, row 0)
307
+ ```
308
+
309
+ #### Input
310
+
311
+ - **A string**: encoded as UTF-8. Runs of digits or capital letters use the smaller numeric and alphanumeric modes when that saves space. For example, `'Order 12345678901234567890'` uses byte mode for `Order ` and numeric mode for the digits.
312
+ - **A `Uint8Array`**: raw bytes (binary data), also split into the best modes.
313
+ - **An array of segments**, to choose the modes yourself:
314
+
315
+ ```js
316
+ qrmake([
317
+ { mode: 'alphanumeric', data: 'INVOICE:' },
318
+ { mode: 'numeric', data: '000123456' },
319
+ { mode: 'byte', data: ' paid €' },
320
+ ])
321
+ ```
322
+
323
+ `kanji` segments take a string, or Shift JIS bytes as a `Uint8Array`.
324
+
325
+ #### Encoder options
326
+
327
+ Every function that takes text (`qrmake`, `encode` and all renderers) accepts these options.
328
+
329
+ | Option | Type | Default | Description |
330
+ |---|---|---|---|
331
+ | `ecl` | `'L' \| 'M' \| 'Q' \| 'H'` | `'M'` | Error correction level: about 7%, 15%, 25% or 30% of the code can be damaged. See [Choose an error correction level](#choose-an-error-correction-level). |
332
+ | `boostEcl` | `boolean` | `false` | Use a higher level for free when the data still fits in the same version. |
333
+ | `version` | `number` | smallest that fits | Force a version, 1-40 (21 x 21 to 177 x 177 modules). Throws if the data does not fit. |
334
+ | `minVersion` | `number` | `1` | Smallest version to consider. |
335
+ | `maxVersion` | `number` | `40` | Largest version to consider. |
336
+ | `mask` | `0`-`7` | lowest penalty | Force a mask pattern. A mask flips some modules so the code is easier to scan; by default qrmake tries all 8 and picks the best. |
337
+ | `mode` | `'numeric' \| 'alphanumeric' \| 'byte' \| 'kanji'` | automatic | Encode everything in one mode. Throws if a character does not fit that mode. |
338
+ | `optimize` | `boolean` | `true` | Split text into the cheapest mix of modes. `false` uses one segment in the most compact mode that fits all of it. |
339
+ | `kanji` | `boolean` | `false` | Allow kanji mode for Japanese text (13 bits per character instead of 24 for UTF-8). Needs `TextDecoder` with Shift JIS support; throws otherwise. |
340
+ | `eci` | `boolean` | `false` | Add a UTF-8 ECI header (a marker that says "this text is UTF-8", designator 26) when the text is not plain ASCII. Most readers assume UTF-8 anyway; some older ones need this. |
341
+
342
+ #### The QRCode object
343
+
344
+ | Property | Type | Description |
345
+ |---|---|---|
346
+ | `version` | `number` | Version used, 1-40. |
347
+ | `size` | `number` | Width and height in modules (`version * 4 + 17`), without the quiet zone. |
348
+ | `ecl` | `ErrorCorrectionLevel` | Error correction level used. |
349
+ | `mask` | `MaskPattern` | Mask pattern used, 0-7. |
350
+ | `segments` | `SegmentInfo[]` | How the data was split into modes: `{ mode, length }`. |
351
+ | `modules` | `Uint8Array` | All modules, row by row. `1` is dark. Index with `y * size + x`. |
352
+ | `get(x, y)` | `boolean` | `true` if the module at column `x`, row `y` is dark. Outside the code it returns `false`. |
353
+ | `toArray()` | `boolean[][]` | The modules as rows of booleans (`true` is dark). |
354
+
355
+ ### toSVG(input, options?)
356
+
357
+ ```ts
358
+ function toSVG(input: RenderInput, options?: SVGOptions): string
359
+ ```
360
+
361
+ Returns an SVG string. All dark modules are drawn as one `<path>` with side-by-side modules merged, so the markup stays small. Colours and the title are XML-escaped. `input` is text, bytes, segments, or a `QRCode` from `qrmake()`. All [encoder options](#encoder-options) also apply.
362
+
363
+ | Option | Type | Default | Description |
364
+ |---|---|---|---|
365
+ | `margin` | `number` | `4` | Quiet zone in modules. 4 is what the standard asks for. |
366
+ | `scale` | `number` | `4` | Pixels per module, used for the `width` and `height` attributes. |
367
+ | `size` | `number` | `(size + 2 * margin) * scale` | `width` and `height` attributes in pixels. `0` leaves them out, so the SVG fills its container. |
368
+ | `dark` | `string` | `'#000'` | Colour of dark modules. Any CSS colour. |
369
+ | `light` | `string` | `'#fff'` | Background colour. Any CSS colour, or `'transparent'` for no background. |
370
+ | `radius` | `number` | `0` | Corner radius of each module, from `0` (square) to `0.5` (round dots). |
371
+ | `title` | `string` | none | Adds a `<title>` element for screen readers. |
372
+
373
+ ```js
374
+ toSVG('hi', { size: 256 }) // width="256" height="256"
375
+ toSVG('hi', { dark: '#0b5', light: 'transparent' }) // green, no background
376
+ toSVG('hi', { radius: 0.5 }) // round dots
377
+ ```
378
+
379
+ ### toPNG(input, options?)
380
+
381
+ ```ts
382
+ function toPNG(input: RenderInput, options?: PNGOptions): Uint8Array
383
+ ```
384
+
385
+ Returns the bytes of a 1-bit palette PNG file. It uses a small built-in deflate (compression) encoder, so it needs no `zlib`, canvas or `Buffer`, and runs synchronously anywhere. All [encoder options](#encoder-options) also apply.
386
+
387
+ | Option | Type | Default | Description |
388
+ |---|---|---|---|
389
+ | `margin` | `number` | `4` | Quiet zone in modules. |
390
+ | `scale` | `number` | `4` | Pixels per module. Must be a whole number. Ignored when `size` is set. |
391
+ | `size` | `number` | none | Exact width and height in pixels. Each module gets a whole number of pixels; leftover pixels widen the quiet zone so the image stays sharp. Throws if the size is too small for 1 pixel per module. |
392
+ | `dark` | `string` | `'#000000'` | Dark module colour: `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`. |
393
+ | `light` | `string` | `'#ffffff'` | Background colour in the same formats, or `'transparent'`. |
394
+
395
+ ### toDataURL(input, options?)
396
+
397
+ ```ts
398
+ function toDataURL(input: RenderInput, options?: DataURLOptions): string
399
+ ```
400
+
401
+ Returns a `data:` URL, ready for `<img src>`. By default it is a PNG (`data:image/png;base64,...`) and the [PNG options](#topnginput-options) apply. With `{ type: 'svg' }` it is an SVG (`data:image/svg+xml;charset=utf-8,...`) and the [SVG options](#tosvginput-options) apply.
402
+
403
+ | Option | Type | Default | Description |
404
+ |---|---|---|---|
405
+ | `type` | `'png' \| 'svg'` | `'png'` | Image format. |
406
+
407
+ ### toTerminal(input, options?)
408
+
409
+ ```ts
410
+ function toTerminal(input: RenderInput, options?: TerminalOptions): string
411
+ ```
412
+
413
+ Returns text that draws the code with Unicode block characters. Print it with `console.log()`. All [encoder options](#encoder-options) also apply.
414
+
415
+ | Option | Type | Default | Description |
416
+ |---|---|---|---|
417
+ | `margin` | `number` | `2` | Quiet zone in modules. |
418
+ | `invert` | `boolean` | `false` | By default the light modules are drawn, which suits terminals with a dark background. Set `true` for a light background. |
419
+ | `small` | `boolean` | `true` | Two rows of modules per line with half blocks (`▀ ▄ █`). `false` draws each module as `██`, which is twice as big. |
420
+ | `ansi` | `boolean` | `false` | Paint black on white with ANSI colours, so the code scans on any terminal theme. |
421
+
422
+ ### toText(input, options?)
423
+
424
+ ```ts
425
+ function toText(input: RenderInput, options?: TextOptions): string
426
+ ```
427
+
428
+ Returns plain text with one line per row of modules. All [encoder options](#encoder-options) also apply.
429
+
11
430
  ```js
12
- const { hello } = require('qrmake') // or: import { hello } from 'qrmake'
13
- console.log(hello()) // 'Hello, world! (from qrmake)'
431
+ toText('hi', { margin: 0, dark: '#', light: '.' }).split('\n')[0]
432
+ // => '#######..####.#######'
433
+ ```
434
+
435
+ | Option | Type | Default | Description |
436
+ |---|---|---|---|
437
+ | `margin` | `number` | `4` | Quiet zone in modules. |
438
+ | `dark` | `string` | `'##'` | Text for a dark module. |
439
+ | `light` | `string` | `' '` | Text for a light module. |
440
+ | `newline` | `string` | `'\n'` | Line separator. |
441
+
442
+ ### wifi(options)
443
+
444
+ ```ts
445
+ function wifi(options: WifiOptions): string
14
446
  ```
15
447
 
448
+ Returns the text to join a Wi-Fi network. The characters `\ ; , : "` are escaped with a backslash. Throws a `TypeError` if `ssid` is empty, or if a password is needed but missing.
449
+
450
+ | Option | Type | Default | Description |
451
+ |---|---|---|---|
452
+ | `ssid` | `string` | required | Network name. |
453
+ | `password` | `string` | none | Leave out for an open network. |
454
+ | `security` | `'WPA' \| 'WEP' \| 'SAE' \| 'nopass'` | `'WPA'` with a password, `'nopass'` without | `'WPA'` covers WPA and WPA2 personal. `'SAE'` is WPA3 only. |
455
+ | `hidden` | `boolean` | `false` | The network does not broadcast its name. |
456
+
457
+ ### email(to, options?)
458
+
459
+ ```ts
460
+ function email(to: string | string[], options?: EmailOptions): string
461
+ ```
462
+
463
+ Returns a `mailto:` link. Values are percent-encoded.
464
+
465
+ ```js
466
+ email('hi@example.com', { subject: 'Hello', body: 'Hi!' })
467
+ // => 'mailto:hi@example.com?subject=Hello&body=Hi!'
468
+ ```
469
+
470
+ | Option | Type | Default | Description |
471
+ |---|---|---|---|
472
+ | `subject` | `string` | none | Email subject. |
473
+ | `body` | `string` | none | Email text. |
474
+ | `cc` | `string \| string[]` | none | Copy to these addresses. |
475
+ | `bcc` | `string \| string[]` | none | Blind copy to these addresses. |
476
+
477
+ ### sms(number, message?)
478
+
479
+ ```ts
480
+ function sms(number: string, message?: string): string
481
+ ```
482
+
483
+ Returns the text to send an SMS. Spaces, dashes, dots and brackets are removed from the number. Any other invalid character throws a `TypeError`.
484
+
485
+ ```js
486
+ sms('+1 555 0100', 'On my way') // => 'SMSTO:+15550100:On my way'
487
+ ```
488
+
489
+ ### tel(number)
490
+
491
+ ```ts
492
+ function tel(number: string): string
493
+ ```
494
+
495
+ Returns a `tel:` link to call a number. The number is cleaned the same way as in `sms()`.
496
+
497
+ ```js
498
+ tel('+1 (555) 010-0199') // => 'tel:+15550100199'
499
+ ```
500
+
501
+ ### geo(latitude, longitude, altitude?)
502
+
503
+ ```ts
504
+ function geo(latitude: number, longitude: number, altitude?: number): string
505
+ ```
506
+
507
+ Returns a `geo:` link to a map location. Values out of range (latitude beyond 90, longitude beyond 180) throw a `RangeError`.
508
+
509
+ ```js
510
+ geo(48.8584, 2.2945) // => 'geo:48.8584,2.2945'
511
+ ```
512
+
513
+ ### url(link)
514
+
515
+ ```ts
516
+ function url(link: string): string
517
+ ```
518
+
519
+ Returns the link, with `https://` added when it has no scheme.
520
+
521
+ ```js
522
+ url('example.com') // => 'https://example.com'
523
+ ```
524
+
525
+ ### vcard(contact)
526
+
527
+ ```ts
528
+ function vcard(contact: ContactOptions): string
529
+ ```
530
+
531
+ Returns a vCard 3.0 contact card. It carries more fields than MECARD. Throws a `TypeError` if `name` is missing.
532
+
533
+ | Option | Type | Default | Description |
534
+ |---|---|---|---|
535
+ | `name` | `string` | required | Full name, for example `'Jane Doe'`. |
536
+ | `phone` | `string \| string[]` | none | One or more phone numbers. |
537
+ | `email` | `string \| string[]` | none | One or more email addresses. |
538
+ | `url` | `string` | none | Website. |
539
+ | `org` | `string` | none | Company or organization. |
540
+ | `title` | `string` | none | Job title. vCard only; `mecard()` ignores it. |
541
+ | `address` | `string` | none | Postal address as one line. |
542
+ | `note` | `string` | none | Free text. |
543
+ | `birthday` | `string` | none | Date as `YYYY-MM-DD`. |
544
+
545
+ ### mecard(contact)
546
+
547
+ ```ts
548
+ function mecard(contact: ContactOptions): string
549
+ ```
550
+
551
+ Returns a MECARD contact card. It is smaller than a vCard, so the QR code is smaller too. It takes the same options as [`vcard()`](#vcardcontact), except `title`. Throws a `TypeError` if `name` is missing.
552
+
553
+ ### capacity(version, ecl?)
554
+
555
+ ```ts
556
+ function capacity(version: number, ecl?: ErrorCorrectionLevel): Capacity
557
+ ```
558
+
559
+ Returns how much fits in one segment of each mode, for a version and error correction level (default `'M'`). `bits` is the total data space, including mode and length headers.
560
+
561
+ ```js
562
+ capacity(1, 'M') // => { bits: 128, numeric: 34, alphanumeric: 20, byte: 14, kanji: 8 }
563
+ capacity(40, 'L') // => { bits: 23648, numeric: 7089, alphanumeric: 4296, byte: 2953, kanji: 1817 }
564
+ ```
565
+
566
+ ### isQRCode(value)
567
+
568
+ ```ts
569
+ function isQRCode(value: unknown): value is QRCode
570
+ ```
571
+
572
+ Returns `true` if `value` is a `QRCode` returned by `qrmake()`.
573
+
574
+ ```js
575
+ isQRCode(qrmake('hi')) // => true
576
+ isQRCode({}) // => false
577
+ ```
578
+
579
+ ### QRError
580
+
581
+ ```ts
582
+ class QRError extends RangeError {}
583
+ ```
584
+
585
+ Thrown when the data does not fit, or when an option is invalid. Because it extends `RangeError`, `error instanceof RangeError` is also `true`.
586
+
587
+ ### Types
588
+
589
+ All types are exported: `QRCode`, `QROptions`, `QRInput`, `QRMake`, `Segment`, `SegmentInfo`, `ErrorCorrectionLevel`, `Mode`, `MaskPattern`, `Capacity`, `RenderInput`, `SVGOptions`, `PNGOptions`, `DataURLOptions`, `TerminalOptions`, `TextOptions`, `WifiOptions`, `WifiSecurity`, `EmailOptions`, `ContactOptions`.
590
+
591
+ ## Comparison
592
+
593
+ How qrmake compares with `qrcode`, `qrcode-generator`, `qrcode-terminal` and `qr-image`:
594
+
595
+ | | `qrcode` | `qrcode-generator` | `qrcode-terminal` | `qr-image` | qrmake |
596
+ |---|---|---|---|---|---|
597
+ | Runtime dependencies | 3 (`pngjs`, `yargs`, `dijkstrajs`) | 0 | 0 | 0 | 0 |
598
+ | SVG | Yes | Yes | No | Yes | Yes |
599
+ | PNG | Yes: Node (via `pngjs`), canvas in browsers | No (GIF only) | No | Node only (`zlib`, `Buffer`) | Yes, in every runtime |
600
+ | PNG and data URL API | Async (callback or Promise) | Sync | n/a | Sync or stream | Sync |
601
+ | Terminal output | Yes | Half-block ASCII | Yes | No | Yes (plus invert and ANSI colours) |
602
+ | Mixed-mode segment optimization | Yes | No (you pick the mode per `addData`) | No (byte mode only) | No (one mode per code; `parse_url` splits a URL in two) | Yes |
603
+ | UTF-8 text by default | Yes | No (keeps the low byte of each character unless you change `stringToBytes`) | No (keeps the low byte of each character) | Yes | Yes |
604
+ | Kanji mode | Yes, with a Shift JIS helper | Yes | No | No | Yes (opt-in, no extra code) |
605
+ | Wi-Fi, vCard and email helpers | No | No | No | No | Yes |
606
+ | Command line tool | Yes | No | Yes | No | Yes |
607
+ | TypeScript types | `@types/qrcode` | Yes | `@types/qrcode-terminal` | `@types/qr-image` | Yes, built in |
608
+
609
+ ## Migrating from qrcode
610
+
611
+ The main change: qrmake is synchronous, so you do not need `await`.
612
+
613
+ ```js
614
+ // Before (qrcode)
615
+ const svg = await QRCode.toString(text, { type: 'svg' })
616
+ const url = await QRCode.toDataURL(text, { errorCorrectionLevel: 'H' })
617
+ const png = await QRCode.toBuffer(text, { width: 512 })
618
+ const modules = QRCode.create(text).modules
619
+
620
+ // After (qrmake)
621
+ const svg = toSVG(text)
622
+ const url = toDataURL(text, { ecl: 'H' })
623
+ const png = toPNG(text, { size: 512 })
624
+ const qr = qrmake(text)
625
+ ```
626
+
627
+ Option names:
628
+
629
+ | qrcode | qrmake |
630
+ |---|---|
631
+ | `errorCorrectionLevel` | `ecl` |
632
+ | `color.dark`, `color.light` | `dark`, `light` |
633
+ | `width` | `size` |
634
+ | `margin`, `scale`, `version` | same names |
635
+
636
+ ## How it is tested
637
+
638
+ - Reed-Solomon codewords (the recovery data) match published examples: the Thonky tutorial's HELLO WORLD 1-M and 5-Q blocks (including the final interleaved message), and the `01234567` 1-M example from ISO/IEC 18004 Annex I.
639
+ - All 32 format strings and all 34 version strings match the published tables. Alignment positions and block structure match ISO/IEC 18004 Annex E and Table 9 for every version and level.
640
+ - Capacity is spot-checked against the published capacity table, and version selection is checked at every boundary.
641
+ - Every version at every level, filled to capacity in numeric, alphanumeric and byte mode, plus every mask, is read back by an independent reader in the test suite. The reader checks the format and version information, every Reed-Solomon block, the padding and the decoded text.
642
+ - PNGs are decoded by OpenCV's QR reader when Python with `cv2` is installed. The test is skipped otherwise.
643
+ - During development, 480 codes made by OpenCV's own encoder (every version and level, in numeric, alphanumeric and byte mode) were compared with qrmake's output for the same data, version, level and mask. The codewords matched every time, except for version 21, where OpenCV puts the last alignment pattern at 92 instead of 94 (ISO/IEC 18004 Annex E). The matrices were identical, except for a few unmasked remainder bits in OpenCV's versions 2-6.
644
+
645
+ ## Notes and limitations
646
+
647
+ - **Kanji mode** needs `TextDecoder` with Shift JIS support (Node.js with ICU, Deno, browsers). Without it, `kanji: true` throws.
648
+ - **PNG colours** must be hex (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`) or `'transparent'`. SVG accepts any CSS colour.
649
+ - **PNG `scale`** must be a whole number. A PNG `size` that is too small for 1 pixel per module throws a `QRError`.
650
+ - **Quiet zone**: keep `margin` at 4 or more for images, as the standard asks. A smaller border can make the code harder to scan.
651
+ - **Non-ASCII text**: most readers assume UTF-8. If an older reader shows the wrong characters, try `eci: true`.
652
+ - **Errors**: the encoder and renderers throw `QRError`. The payload helpers throw `TypeError` (or `RangeError` from `geo()`) for invalid input.
653
+ - **Command line colours**: `--dark` and `--light` only affect SVG and PNG output, not the terminal.
654
+
655
+ ## Requirements
656
+
657
+ - Node.js 20 or newer. Also runs in Deno, Bun, browsers and web workers.
658
+ - ESM and CommonJS builds.
659
+ - TypeScript types included.
660
+ - Zero dependencies.
661
+
16
662
  ## License
17
663
 
18
664
  MIT