@@ -6,7 +6,7 @@ const getPixels = require('get-pixels');
66const Buffer = require ( 'mutable-buffer' ) ;
77const EventEmitter = require ( 'events' ) ;
88const Image = require ( './image' ) ;
9- const Barcode = require ( './barcode ' ) ;
9+ const utils = require ( './utils ' ) ;
1010const _ = require ( './commands' ) ;
1111
1212/**
@@ -278,8 +278,17 @@ Printer.prototype.hardware = function(hw){
278278 * @return printer instance
279279 */
280280Printer . 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 */
366369Printer . 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 */
391394Printer . 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 ( ) ] ;
0 commit comments