3 let Qr = module.exports;
5 let Fs = require("fs").promises;
7 let QrCode = require("qrcode-svg");
11 * @property {String} [background]
12 * @property {String} [color]
13 * @property {String} [ecl]
14 * @property {Number} [height]
15 * @property {Number} [indent]
16 * @property {Number} [padding]
17 * @property {"mini" | "micro"} [size]
18 * @property {Number} [width]
22 * @param {String} data
23 * @param {QrOpts} opts
25 Qr._create = function (data, opts) {
28 padding: opts?.padding || 4,
29 width: opts?.width || 256,
30 height: opts?.height || 256,
31 color: opts?.color || "#000000",
32 background: opts?.background || "#ffffff",
33 ecl: opts?.ecl || "M",
38 * @typedef {Object.<String, String>} BlockMap
42 * Encoded as top-left, top-right, bottom-left, bottom-right
43 * @type {Object.<"mini" | "micro", BlockMap>}
85 * @param {String} data
86 * @param {QrOpts} opts
88 Qr.quadAscii = function (data, opts) {
89 let charMap = charMaps[opts.size || "mini"];
90 let qrcode = Qr._create(data, opts);
91 let indent = opts?.indent ?? 4;
92 let modules = qrcode.qrcode.modules;
94 let ascii = ``.padStart(indent - 1, " ");
95 let length = modules.length;
96 for (let y = 0; y < length; y += 2) {
97 for (let x = 0; x < length; x += 2) {
99 // qr codes can be odd numbers
101 ascii += charMap[count];
107 if (modules[x][y + 1]) {
111 if (x + 1 >= length) {
112 ascii += charMap[count];
115 if (modules[x + 1][y]) {
118 if (modules[x + 1][y + 1]) {
121 ascii += charMap[count];
123 ascii += `\n`.padEnd(indent, " ");
125 return ascii.replace(/\s+$/, "");
129 * @param {String} data
130 * @param {QrOpts} opts
132 Qr.ascii = function (data, opts) {
134 return Qr.quadAscii(data, opts);
137 let qrcode = Qr._create(data, opts);
138 let indent = opts?.indent ?? 4;
139 let modules = qrcode.qrcode.modules;
141 let ascii = ``.padStart(indent - 1, " ");
142 let length = modules.length;
143 for (let y = 0; y < length; y += 1) {
144 for (let x = 0; x < length; x += 1) {
151 ascii += `\n`.padEnd(indent, " ");
157 * @param {String} data
158 * @param {QrOpts} opts
160 Qr.svg = function (data, opts) {
161 let qrcode = Qr._create(data, opts);
166 * @param {String} filepath
167 * @param {String} data
168 * @param {QrOpts} opts
170 Qr.save = async function (filepath, data, opts) {
171 let qrcode = Qr.svg(data, opts);
172 await Fs.writeFile(filepath, qrcode, "utf8");