@nuintun/qrcode
Version:
A pure JavaScript QRCode encode and decode library.
70 lines (66 loc) • 1.52 kB
JavaScript
/**
* @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
*/
import { Mode } from '../../common/Mode.js';
import { Charset } from '../../common/Charset.js';
import { BitArray } from '../../common/BitArray.js';
import { assertContent, assertCharset } from '../utils/asserts.js';
/**
* @module Byte
*/
class Byte {
#content;
#charset;
/**
* @constructor
* @param content The content to encode.
* @param charset The charset of the content.
*/
constructor(content, charset = Charset.ISO_8859_1) {
assertContent(content);
assertCharset(charset);
this.#content = content;
this.#charset = charset;
}
/**
* @property mode
* @description The mode of the segment.
*/
get mode() {
return 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();
const bytes = encode(this.#content, this.#charset);
for (const byte of bytes) {
bits.append(byte, 8);
}
return bits;
}
}
export { Byte };