Initial commit on EAN supplements; Relates to #71
parent
318681d66b
commit
eb9a4ca4cb
@ -0,0 +1,46 @@
|
||||
import EANReader from './ean_reader';
|
||||
|
||||
function EAN2Reader() {
|
||||
EANReader.call(this);
|
||||
}
|
||||
|
||||
var properties = {
|
||||
FORMAT: {value: "ean_2", writeable: false}
|
||||
};
|
||||
|
||||
EAN2Reader.prototype = Object.create(EANReader.prototype, properties);
|
||||
EAN2Reader.prototype.constructor = EAN2Reader;
|
||||
|
||||
EAN2Reader.prototype.decode = function(row, start) {
|
||||
this._row = row;
|
||||
var counters = [0, 0, 0, 0],
|
||||
codeFrequency = 0,
|
||||
i = 0,
|
||||
offset = start,
|
||||
end = this._row.length,
|
||||
code,
|
||||
result = [];
|
||||
|
||||
for (i = 0; i < 2 && offset < end; i++) {
|
||||
code = this._decodeCode(offset);
|
||||
if (!code) {
|
||||
return null;
|
||||
}
|
||||
result.push(code.code % 10);
|
||||
if (code.code >= this.CODE_G_START) {
|
||||
codeFrequency |= 1 << (1 - i);
|
||||
}
|
||||
if (i != 1) {
|
||||
offset = this._nextSet(this._row, code.end);
|
||||
offset = this._nextUnset(this._row, offset);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.length != 2 || (result.reduce((sum, i) => sum + i, 0) % 4 !== codeFrequency)) {
|
||||
return null;
|
||||
}
|
||||
console.log(result);
|
||||
return offset;
|
||||
};
|
||||
|
||||
export default EAN2Reader;
|
@ -0,0 +1,79 @@
|
||||
import EANReader from './ean_reader';
|
||||
|
||||
function EAN5Reader() {
|
||||
EANReader.call(this);
|
||||
}
|
||||
|
||||
var properties = {
|
||||
FORMAT: {value: "ean_5", writeable: false}
|
||||
};
|
||||
|
||||
const CHECK_DIGIT_ENCODINGS = [24, 20, 18, 17, 12, 6, 3, 10, 9, 5];
|
||||
|
||||
EAN5Reader.prototype = Object.create(EANReader.prototype, properties);
|
||||
EAN5Reader.prototype.constructor = EAN5Reader;
|
||||
|
||||
EAN5Reader.prototype.decode = function(row, start) {
|
||||
this._row = row;
|
||||
var counters = [0, 0, 0, 0],
|
||||
codeFrequency = 0,
|
||||
i = 0,
|
||||
offset = start,
|
||||
end = this._row.length,
|
||||
code,
|
||||
result = [];
|
||||
|
||||
for (i = 0; i < 5 && offset < end; i++) {
|
||||
code = this._decodeCode(offset);
|
||||
if (!code) {
|
||||
return null;
|
||||
}
|
||||
result.push(code.code % 10);
|
||||
if (code.code >= this.CODE_G_START) {
|
||||
codeFrequency |= 1 << (4 - i);
|
||||
}
|
||||
if (i != 4) {
|
||||
offset = this._nextSet(this._row, code.end);
|
||||
offset = this._nextUnset(this._row, offset);
|
||||
}
|
||||
}
|
||||
|
||||
if (result.length != 5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (extensionChecksum(result) !== determineCheckDigit(codeFrequency)) {
|
||||
return null;
|
||||
}
|
||||
console.log(result);
|
||||
return offset;
|
||||
};
|
||||
|
||||
function determineCheckDigit(codeFrequency) {
|
||||
var i;
|
||||
for (i = 0; i < 10; i++) {
|
||||
if (codeFrequency === CHECK_DIGIT_ENCODINGS[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
function extensionChecksum(result) {
|
||||
var length = result.length,
|
||||
sum = 0,
|
||||
i;
|
||||
|
||||
for (i = length - 2; i >= 0; i -= 2) {
|
||||
sum += result[i];
|
||||
}
|
||||
sum *= 3;
|
||||
for (i = length - 1; i >= 0; i -= 2) {
|
||||
sum += result[i];
|
||||
}
|
||||
sum *= 3;
|
||||
return sum % 10;
|
||||
}
|
||||
|
||||
export default EAN5Reader;
|
Loading…
Reference in New Issue