Skip to content

Commit 4229f50

Browse files
committed
modify barcode
1 parent 605c17c commit 4229f50

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

barcode.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

printer.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getPixels = require('get-pixels');
66
const Buffer = require('mutable-buffer');
77
const EventEmitter = require('events');
88
const Image = require('./image');
9-
const Barcode = require('./barcode');
9+
const utils = require('./utils');
1010
const _ = require('./commands');
1111

1212
/**
@@ -278,8 +278,17 @@ Printer.prototype.hardware = function(hw){
278278
* @return printer instance
279279
*/
280280
Printer.prototype.barcode = function(code, type, width, height, position, font){
281-
var convertCode = String(code);
282-
var parityBit = ''
281+
type = type || 'EAN13'; // default type is EAN13, may a good choice ?
282+
var convertCode = String(code), parityBit = '';
283+
if(typeof type === 'undefined' || type === null){
284+
throw new TypeError('barcode type is required');
285+
}
286+
if (type === 'EAN13' && convertCode.length != 12) {
287+
throw new Error('EAN13 Barcode type requires code length 12');
288+
}
289+
if (type === 'EAN8' && convertCode.length != 7) {
290+
throw new Error('EAN8 Barcode type requires code length 7');
291+
}
283292
if(width >= 2 || width <= 6){
284293
this.buffer.write(_.BARCODE_FORMAT.BARCODE_WIDTH[width]);
285294
}else{
@@ -299,14 +308,8 @@ Printer.prototype.barcode = function(code, type, width, height, position, font){
299308
this.buffer.write(_.BARCODE_FORMAT[
300309
'BARCODE_' + ((type || 'EAN13').replace('-', '_').toUpperCase())
301310
]);
302-
if ((!type || type === 'EAN13') && convertCode.length != 12) {
303-
throw Error('EAN13 Barcode type requires code length 12');
304-
}
305-
if (type === 'EAN8' && convertCode.length != 7) {
306-
throw Error('EAN8 Barcode type requires code length 7');
307-
}
308-
if (!type || type === 'EAN13' || type === 'EAN8') {
309-
parityBit = Barcode.getParityBit(code, type || 'EAN13');
311+
if (type === 'EAN13' || type === 'EAN8') {
312+
parityBit = utils.getParityBit(code);
310313
}
311314
this.buffer.write(code + parityBit);
312315
return this;
@@ -364,7 +367,7 @@ Printer.prototype.qrimage = function(content, options, callback){
364367
* @return {[type]} [description]
365368
*/
366369
Printer.prototype.image = function(image, density){
367-
if(!(image instanceof Image))
370+
if(!(image instanceof Image))
368371
throw new TypeError('Only escpos.Image supported');
369372
density = density || 'd24';
370373
var n = !!~[ 'd8', 's8' ].indexOf(density) ? 1 : 3;
@@ -389,11 +392,11 @@ Printer.prototype.image = function(image, density){
389392
* @return {[type]} [description]
390393
*/
391394
Printer.prototype.raster = function (image, mode) {
392-
if(!(image instanceof Image))
395+
if(!(image instanceof Image))
393396
throw new TypeError('Only escpos.Image supported');
394397
mode = mode || 'normal';
395-
if (mode === 'dhdw' ||
396-
mode === 'dwh' ||
398+
if (mode === 'dhdw' ||
399+
mode === 'dwh' ||
397400
mode === 'dhw') mode = 'dwdh';
398401
var raster = image.toRaster();
399402
var header = _.GSV0_FORMAT['GSV0_' + mode.toUpperCase()];

utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* [getParityBit description]
3+
* @return {[type]} [description]
4+
*/
5+
exports.getParityBit = function (str){
6+
var parity = 0, reversedCode = str.split('').reverse().join('');
7+
for (var counter = 0; counter < reversedCode.length; counter += 1) {
8+
parity += parseInt(reversedCode.charAt(counter), 10) * Math.pow(3, ((counter + 1) % 2));
9+
}
10+
return String((10 - (parity % 10)) % 10);
11+
};

yarn.lock

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,12 @@ [email protected]:
151151
version "0.0.3"
152152
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-0.0.3.tgz#18ae979a6a0ca994b0625853916d2662bbae0b1a"
153153

154-
154+
[email protected], debug@^2.2.0, debug@^2.3.2:
155155
version "2.6.0"
156156
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.0.tgz#bc596bcabe7617f11d9fa15361eded5608b8499b"
157157
dependencies:
158158
ms "0.7.2"
159159

160-
debug@^2.2.0, debug@^2.3.2:
161-
version "2.6.6"
162-
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.6.tgz#a9fa6fbe9ca43cf1e79f73b75c0189cbb7d6db5a"
163-
dependencies:
164-
ms "0.7.3"
165-
166160
deep-extend@~0.4.0:
167161
version "0.4.1"
168162
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.1.tgz#efe4113d08085f4e6f9687759810f807469e2253"
@@ -540,10 +534,6 @@ [email protected]:
540534
version "0.7.2"
541535
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"
542536

543-
544-
version "0.7.3"
545-
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.3.tgz#708155a5e44e33f5fd0fc53e81d0d40a91be1fff"
546-
547537
mutable-buffer@*:
548538
version "2.0.3"
549539
resolved "https://registry.yarnpkg.com/mutable-buffer/-/mutable-buffer-2.0.3.tgz#67bca5568bf15d3f6035fb716e3899dc44901c8c"

0 commit comments

Comments
 (0)