From: AJ ONeal Date: Sat, 25 Jun 2022 06:54:54 +0000 (-0600) Subject: feat: add 'mini' (double quad-blocks) ascii QR output X-Git-Tag: v1.4.0~2 X-Git-Url: https://git.josue.xyz/?a=commitdiff_plain;h=20c6ad9f740a8a948939191c36c769224505b8c4;p=crowdnode.js%2F.git feat: add 'mini' (double quad-blocks) ascii QR output --- diff --git a/bin/crowdnode.js b/bin/crowdnode.js index d8697d9..7815cbf 100755 --- a/bin/crowdnode.js +++ b/bin/crowdnode.js @@ -337,7 +337,7 @@ function showQr(addr, duffs = 0) { dashUri += `?amount=${dashAmount}`; } - let dashQr = Qr.ascii(dashUri, { indent: 4, size: "micro" }); + let dashQr = Qr.ascii(dashUri, { indent: 4, size: "mini" }); let addrPad = Math.ceil((qrWidth - dashUri.length) / 2); console.info(dashQr); diff --git a/lib/qr.js b/lib/qr.js index 632218b..9469b47 100644 --- a/lib/qr.js +++ b/lib/qr.js @@ -35,60 +35,70 @@ Qr._create = function (data, opts) { }; /** - * @type {Object.} + * @typedef {Object.} BlockMap */ -let microMap = { - // top-left, top-right, bottom-left, bottom-right - 0b0000: " ", - // - 0b0001: "▗", - // - 0b0010: "▖", - // - 0b0011: "▄", - // - 0b0100: "▝", - // - 0b0101: "▐", - // - 0b0110: "▞", - // - 0b0111: "▟", - // - 0b1000: "▘", - // - 0b1001: "▚", - // - 0b1010: "▌", - // - 0b1011: "▙", - // - 0b1100: "▀", - // - 0b1101: "▜", - // - 0b1110: "▛", - // - 0b1111: "█", + +/** + * Encoded as top-left, top-right, bottom-left, bottom-right + * @type {Object.<"mini" | "micro", BlockMap>} + */ +let charMaps = { + micro: { + 0b0000: " ", + 0b0001: "▗", + 0b0010: "▖", + 0b0011: "▄", + 0b0100: "▝", + 0b0101: "▐", + 0b0110: "▞", + 0b0111: "▟", + 0b1000: "▘", + 0b1001: "▚", + 0b1010: "▌", + 0b1011: "▙", + 0b1100: "▀", + 0b1101: "▜", + 0b1110: "▛", + 0b1111: "█", + }, + mini: { + 0b0000: " ", + 0b0001: " ▄", + 0b0010: "▄ ", + 0b0011: "▄▄", + 0b0100: " ▀", + 0b0101: " █", + 0b0110: "▄▀", + 0b0111: "▄█", + 0b1000: "▀ ", + 0b1001: "▀▄", + 0b1010: "█ ", + 0b1011: "█▄", + 0b1100: "▀▀", + 0b1101: "▀█", + 0b1110: "█▀", + 0b1111: "██", + }, }; /** * @param {String} data * @param {QrOpts} opts */ -Qr.microAscii = function (data, opts) { +Qr.quadAscii = function (data, opts) { + let charMap = charMaps[opts.size || "mini"]; let qrcode = Qr._create(data, opts); let indent = opts?.indent ?? 4; let modules = qrcode.qrcode.modules; - let ascii = ``.padStart(indent, " "); + let ascii = ``.padStart(indent - 1, " "); let length = modules.length; for (let y = 0; y < length; y += 2) { for (let x = 0; x < length; x += 2) { let count = 0; // qr codes can be odd numbers if (x >= length) { - ascii += microMap[count]; + ascii += charMap[count]; continue; } if (modules[x][y]) { @@ -99,7 +109,7 @@ Qr.microAscii = function (data, opts) { } if (x + 1 >= length) { - ascii += microMap[count]; + ascii += charMap[count]; continue; } if (modules[x + 1][y]) { @@ -108,11 +118,11 @@ Qr.microAscii = function (data, opts) { if (modules[x + 1][y + 1]) { count += 1; } - ascii += microMap[count]; + ascii += charMap[count]; } ascii += `\n`.padEnd(indent, " "); } - return " ".padEnd(indent - 1, " ") + ascii.trim(); + return ascii.replace(/\s+$/, ""); }; /** @@ -120,8 +130,8 @@ Qr.microAscii = function (data, opts) { * @param {QrOpts} opts */ Qr.ascii = function (data, opts) { - if ("micro" === opts.size) { - return Qr.microAscii(data, opts); + if (opts.size) { + return Qr.quadAscii(data, opts); } let qrcode = Qr._create(data, opts);