Skip to content

Commit

Permalink
barcode() now also accept string, and add checking that only accept n…
Browse files Browse the repository at this point in the history
…umeric barcode for following type: UPC-A, UPC-E, EAN-8, ENA-13, ITF, NW7
  • Loading branch information
Xelio Cheong committed Nov 26, 2023
1 parent 50f8a76 commit 46b687f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,12 @@ export class Printer<AdapterCloseArgs extends []> 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)
Expand All @@ -591,6 +591,9 @@ export class Printer<AdapterCloseArgs extends []> 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);

Expand Down

0 comments on commit 46b687f

Please sign in to comment.