Skip to content

Commit

Permalink
Merge pull request #80 from Xelio/string-type-barcode
Browse files Browse the repository at this point in the history
Allow letters other then 0-9 in barcode printing
  • Loading branch information
dohooo committed Jan 14, 2024
2 parents 50f8a76 + 46b687f commit 1ff91c6
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 1ff91c6

Please sign in to comment.