UNPKG

@nuintun/qrcode

Version:

A pure JavaScript QRCode encode and decode library.

72 lines (67 loc) 1.56 kB
/** * @module QRCode * @package @nuintun/qrcode * @license MIT * @version 5.0.2 * @author nuintun <nuintun@qq.com> * @description A pure JavaScript QRCode encode and decode library. * @see https://github.com/nuintun/qrcode#readme */ 'use strict'; const Mode = require('../../common/Mode.cjs'); const Charset = require('../../common/Charset.cjs'); const BitArray = require('../../common/BitArray.cjs'); const asserts = require('../utils/asserts.cjs'); /** * @module Byte */ class Byte { #content; #charset; /** * @constructor * @param content The content to encode. * @param charset The charset of the content. */ constructor(content, charset = Charset.Charset.ISO_8859_1) { asserts.assertContent(content); asserts.assertCharset(charset); this.#content = content; this.#charset = charset; } /** * @property mode * @description The mode of the segment. */ get mode() { return Mode.Mode.BYTE; } /** * @property content * @description The content of the segment. */ get content() { return this.#content; } /** * @property charset * @description The charset of the content. */ get charset() { return this.#charset; } /** * @method encode * @description Encode the segment. * @param encode The text encode function. */ encode(encode) { const bits = new BitArray.BitArray(); const bytes = encode(this.#content, this.#charset); for (const byte of bytes) { bits.append(byte, 8); } return bits; } } exports.Byte = Byte;