|
| 1 | +import type { EncodedBlock } from 'luby-transform' |
| 2 | +import { type QrCodeGenerateOptions, type QrCodeGenerateSvgOptions, type QrCodeGenerateUnicodeOptions, encode as qrEncode, renderANSI, renderSVG, renderUnicode } from 'uqr' |
| 3 | +import { createGeneraterWithRender } from './base-generater' |
| 4 | +import { addUrlPrefix, blockToBase64, type GeneraterBaseOptions } from './shared' |
| 5 | + |
| 6 | +/** |
| 7 | + * Render QR Code to ANSI colored string. |
| 8 | + */ |
| 9 | +export const createGeneraterANSI = /* @__PURE__ */ createGeneraterWithRender(withRenderANSI) |
| 10 | +function withRenderANSI(options: GeneraterBaseOptions<QrCodeGenerateOptions>) { |
| 11 | + return (data: EncodedBlock) => { |
| 12 | + let base64str = blockToBase64(data) |
| 13 | + base64str = addUrlPrefix(options.urlPrefix, base64str) |
| 14 | + return renderANSI(base64str, options) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Render QR Code to Unicode string for each pixel. By default it uses █ and ░ to represent black and white pixels, and it can be customizable. |
| 20 | + */ |
| 21 | +export const createGeneraterUnicode = /* @__PURE__ */ createGeneraterWithRender(withRenderUnicode) |
| 22 | +function withRenderUnicode(options: GeneraterBaseOptions<QrCodeGenerateUnicodeOptions>) { |
| 23 | + return (data: EncodedBlock) => { |
| 24 | + let base64str = blockToBase64(data) |
| 25 | + base64str = addUrlPrefix(options.urlPrefix, base64str) |
| 26 | + return renderUnicode(base64str, options) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Render QR Code with two rows into one line with unicode ▀, ▄, █, . It is useful when you want to display QR Code in terminal with limited height. |
| 32 | + */ |
| 33 | +export const createGeneraterUnicodeCompact = /* @__PURE__ */ createGeneraterWithRender(withRenderUnicodeCompact) |
| 34 | +function withRenderUnicodeCompact(options: GeneraterBaseOptions<QrCodeGenerateUnicodeOptions>) { |
| 35 | + return (data: EncodedBlock) => { |
| 36 | + let base64str = blockToBase64(data) |
| 37 | + base64str = addUrlPrefix(options.urlPrefix, base64str) |
| 38 | + return renderUnicode(base64str, options) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Render QR Code to SVG string. |
| 44 | + */ |
| 45 | +export const createGeneraterSVG = /* @__PURE__ */ createGeneraterWithRender(withRenderSVG) |
| 46 | +function withRenderSVG(options: GeneraterBaseOptions<QrCodeGenerateSvgOptions>) { |
| 47 | + return (data: EncodedBlock) => { |
| 48 | + let base64str = blockToBase64(data) |
| 49 | + base64str = addUrlPrefix(options.urlPrefix, base64str) |
| 50 | + return renderSVG(base64str, options) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Encode data into QR Code represented by a 2D array. |
| 56 | + */ |
| 57 | +export const createGeneraterQRCodeArray = /* @__PURE__ */ createGeneraterWithRender(withRenderQRCodeArray) |
| 58 | +function withRenderQRCodeArray(options: GeneraterBaseOptions<QrCodeGenerateOptions>) { |
| 59 | + return (data: EncodedBlock) => { |
| 60 | + let base64str = blockToBase64(data) |
| 61 | + base64str = addUrlPrefix(options.urlPrefix, base64str) |
| 62 | + return qrEncode(base64str, options) |
| 63 | + } |
| 64 | +} |
0 commit comments