Skip to content

Commit 46b687f

Browse files
author
Xelio Cheong
committed
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
1 parent 50f8a76 commit 46b687f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

packages/core/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,12 @@ export class Printer<AdapterCloseArgs extends []> extends EventEmitter {
574574
* @param {[type]} options [description]
575575
* @return {[Printer]} printer [the escpos printer instance]
576576
*/
577-
barcode(code: number, type: BarcodeType, options : BarcodeOptions) {
577+
barcode(code: number | string, type: BarcodeType, options : BarcodeOptions) {
578578
options.font = options.font ?? "a";
579579
options.position = options.position ?? "blw";
580580
options.includeParity = options.includeParity ?? true;
581581

582-
const convertCode = code.toString(10);
582+
const convertCode = (typeof code === 'number') ? code.toString(10) : code;
583583
let parityBit = "";
584584
let codeLength = "";
585585
if (typeof type === "undefined" || type === null)
@@ -591,6 +591,9 @@ export class Printer<AdapterCloseArgs extends []> extends EventEmitter {
591591
if (type === "EAN8" && convertCode.length !== 7)
592592
throw new Error("EAN8 Barcode type requires code length 7");
593593

594+
if (["UPC_A", "UPC-A", "UPC-E", "UPC_E", "EAN13", "EAN8", "ITF", "NW7"].includes(type) && !/^\d+$/.test(convertCode))
595+
throw new Error(type + " Barcode type only support numbers")
596+
594597
if (this._model === "qsprinter")
595598
this.buffer.write(_.MODEL.QSPRINTER.BARCODE_MODE.ON);
596599

0 commit comments

Comments
 (0)