Skip to content

Commit 5afb663

Browse files
committed
Added utility to calculate EAN barcode type parity bit #56
1 parent 609fbca commit 5afb663

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
*.sublime*
44
psd
55
thumb
6-
*.log
6+
*.log
7+
package-lock.json

barcode.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
function getParityBit(code, type) {
4+
switch(type) {
5+
case 'EAN13':
6+
case 'EAN8': {
7+
var parity = 0, reversedCode = code.split('').reverse().join('');
8+
for (var counter = 0; counter < reversedCode.length; counter += 1) {
9+
parity += parseInt(reversedCode.charAt(counter), 10) * Math.pow(3, ((counter + 1) % 2));
10+
}
11+
return String((10 - (parity % 10)) % 10);
12+
}
13+
default: {
14+
return '';
15+
}
16+
}
17+
}
18+
19+
module.exports = {
20+
getParityBit,
21+
};

examples/barcode.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const escpos = require('../');
3+
4+
const device = new escpos.USB();
5+
6+
const printer = new escpos.Printer(device);
7+
8+
device.open(function() {
9+
printer
10+
.font('a')
11+
.align('ct')
12+
.size(1, 1)
13+
.text('EAN13 barcode example')
14+
.barcode('123456789012', 'EAN13') // code length 12
15+
.barcode('109876543210') // default type 'EAN13'
16+
.barcode('7654321', 'EAN8') // The EAN parity bit is automatically added.
17+
.cut()
18+
.cut()
19+
.close();
20+
});

examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ device.open(function(){
1616
.size(1, 1)
1717
.text('The quick brown fox jumps over the lazy dog')
1818
.text('敏捷的棕色狐狸跳过懒狗')
19-
.barcode('12345678', 'EAN8')
19+
.barcode('1234567', 'EAN8')
2020
.qrimage('https://github.com/song940/node-escpos', function(err){
2121
this.cut();
2222
this.close();

printer.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +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');
910
const _ = require('./commands');
1011

1112
/**
@@ -277,6 +278,8 @@ Printer.prototype.hardware = function(hw){
277278
* @return printer instance
278279
*/
279280
Printer.prototype.barcode = function(code, type, width, height, position, font){
281+
var convertCode = String(code);
282+
var parityBit = ''
280283
if(width >= 2 || width <= 6){
281284
this.buffer.write(_.BARCODE_FORMAT.BARCODE_WIDTH[width]);
282285
}else{
@@ -296,7 +299,16 @@ Printer.prototype.barcode = function(code, type, width, height, position, font){
296299
this.buffer.write(_.BARCODE_FORMAT[
297300
'BARCODE_' + ((type || 'EAN13').replace('-', '_').toUpperCase())
298301
]);
299-
this.buffer.write(code);
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');
310+
}
311+
this.buffer.write(code + parityBit);
300312
return this;
301313
};
302314

0 commit comments

Comments
 (0)