UNPKG

ts-barcode-generator

Version:

Simple Barcode Generator created in TypeScript

59 lines (58 loc) 1.91 kB
export default class QR { constructor(data) { this.data = data; this.qrCode = []; this.initializeQRCode(); this.encodeData(); } initializeQRCode() { for (let i = 0; i < 21; i++) { this.qrCode[i] = new Array(21).fill('0'); } // TODO: Add version, format, position, alignment, and timing patterns } encodeData() { // TODO: Implement data encoding } charToBinary(char) { const charCode = char.charCodeAt(0); return charCode.toString(2).padStart(8, '0'); } renderQRCode() { // TODO: Implement rendering with quiet zones for (let i = 0; i < 21; i++) { console.log(this.qrCode[i].join('')); } } renderQRCodeAsSVG() { const whiteRectangles = []; const blackRectangles = []; for (let i = 0; i < 21; i++) { for (let j = 0; j < 21; j++) { const rectangle = `<rect x="${j * 10}" y="${i * 10}" width="10" height="10" fill="${this.qrCode[i][j] === '1' ? 'black' : 'white'}" />`; if (this.qrCode[i][j] === '1') { blackRectangles.push(rectangle); } else { whiteRectangles.push(rectangle); } } } const svg = `<svg width="210" height="210" xmlns="http://www.w3.org/2000/svg">${whiteRectangles.join('')}${blackRectangles.join('')}</svg>`; return svg; } // A QR code is capable of encoding a maximum of 2953 bytes of data static isValidData(data) { // TODO: Implement this return true; } static generate(data) { if (this.isValidData(data) === false) { throw new Error('Invalid data'); } else { const qr = new QR(data); return qr.renderQRCodeAsSVG(); } } }