From 46b687f0dd59694ecae7d969c9bcc3047bf2c7db Mon Sep 17 00:00:00 2001 From: Xelio Cheong Date: Sun, 26 Nov 2023 17:04:27 +0800 Subject: [PATCH] barcode() now also accept string, and add checking that only accept numeric barcode for following type: UPC-A, UPC-E, EAN-8, ENA-13, ITF, NW7 --- packages/core/src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 79c94e25..cf5b82ae 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -574,12 +574,12 @@ export class Printer extends EventEmitter { * @param {[type]} options [description] * @return {[Printer]} printer [the escpos printer instance] */ - barcode(code: number, type: BarcodeType, options : BarcodeOptions) { + barcode(code: number | string, type: BarcodeType, options : BarcodeOptions) { options.font = options.font ?? "a"; options.position = options.position ?? "blw"; options.includeParity = options.includeParity ?? true; - const convertCode = code.toString(10); + const convertCode = (typeof code === 'number') ? code.toString(10) : code; let parityBit = ""; let codeLength = ""; if (typeof type === "undefined" || type === null) @@ -591,6 +591,9 @@ export class Printer extends EventEmitter { if (type === "EAN8" && convertCode.length !== 7) throw new Error("EAN8 Barcode type requires code length 7"); + if (["UPC_A", "UPC-A", "UPC-E", "UPC_E", "EAN13", "EAN8", "ITF", "NW7"].includes(type) && !/^\d+$/.test(convertCode)) + throw new Error(type + " Barcode type only support numbers") + if (this._model === "qsprinter") this.buffer.write(_.MODEL.QSPRINTER.BARCODE_MODE.ON);