@ombori/epc-ean
Version:
EPC Tag Data Standard encoding and decoding library, written in javascript
140 lines (139 loc) • 5.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Gdti96 = void 0;
const utils_1 = __importDefault(require("../utils/utils"));
const epc_1 = require("../epc");
const type_1 = require("../type");
const partition_1 = require("../partition");
class Gdti96 extends epc_1.Epc {
constructor(hexEpc) {
super(Gdti96.TOTAL_BITS);
if (hexEpc) {
super.setFromHexString(hexEpc);
}
else {
super.set(Gdti96.EPC_HEADER, epc_1.Epc.EPC_HEADER_OFFSET, epc_1.Epc.EPC_HEADER_END);
}
}
clone() {
return new Gdti96().setFromBitArray(this);
}
getType() {
return type_1.Type.Gdti96;
}
fromTagURI(uri) {
const value = uri.split(':');
try {
if (value[3] === this.TAG_URI) {
const data = value[4].split('.');
const result = new Gdti96();
result.setFilter(parseInt(data[0]));
result.setPartition(12 - data[1].length);
result.setCompanyPrefix(parseInt(data[1]));
result.setDocumentReference(parseInt(data[2]));
result.setSerial(parseInt(data[3]));
return result;
}
}
catch (e) {
// console.log(e)
}
throw new Error(`${uri} is not a known EPC tag URI scheme`);
}
toTagURI() {
// F.C.D.S (Filter, Company, Document, Serial)
let partition = Gdti96.PARTITIONS[this.getPartition()];
return Gdti96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
}
toIdURI() {
// C.D.S (Company, Document, Serial)
let partition = Gdti96.PARTITIONS[this.getPartition()];
return Gdti96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
}
toBarcode() {
return this.getGdti();
}
getTotalBits() {
return Gdti96.TOTAL_BITS;
}
getHeader() {
return Gdti96.EPC_HEADER;
}
getPartition() {
return super.get(Gdti96.PARTITION_OFFSET, Gdti96.PARTITION_END);
}
setPartition(value) {
if (value < 0 || value >= Gdti96.PARTITIONS.length) {
throw new Error(`Value '${value}' out of range (min: 0, max: ${Gdti96.PARTITIONS.length - 1})`);
}
super.set(value, Gdti96.PARTITION_OFFSET, Gdti96.PARTITION_END);
return this;
}
getGdti() {
let partition = Gdti96.PARTITIONS[this.getPartition()];
let companyPrefix = super.getSegmentString(partition.a);
let document = super.getSegmentString(partition.b);
let result = companyPrefix + document;
return result + utils_1.default.computeCheckDigit(result) + this.getSerial();
}
setGdti(gdti) {
let partition = Gdti96.PARTITIONS[this.getPartition()];
super.setSegment(gdti.substring(0, partition.a.digits), partition.a);
let tmp = partition.a.digits + partition.b.digits;
super.setSegment(gdti.substring(partition.a.digits, tmp), partition.b);
this.setSerial(Number(gdti.substring(tmp + 1, gdti.length)));
return this;
}
getCompanyPrefix() {
return super.getSegment(Gdti96.PARTITIONS[this.getPartition()].a);
}
setCompanyPrefix(value) {
super.setSegment(value, Gdti96.PARTITIONS[this.getPartition()].a);
return this;
}
getDocumentReference() {
return super.getSegment(Gdti96.PARTITIONS[this.getPartition()].b);
}
setDocumentReference(value) {
super.setSegment(value, Gdti96.PARTITIONS[this.getPartition()].b);
return this;
}
getSerial() {
return super.get(Gdti96.SERIAL_OFFSET, Gdti96.SERIAL_END);
}
setSerial(value) {
if (value > Gdti96.MAX_SERIAL)
throw new Error(`Value '${value}' out of range (min: 0, max: ${Gdti96.MAX_SERIAL})`);
super.set(value, Gdti96.SERIAL_OFFSET, Gdti96.SERIAL_END);
return this;
}
}
exports.Gdti96 = Gdti96;
Gdti96.EPC_HEADER = 0x2c;
Gdti96.TOTAL_BITS = 96;
Gdti96.PARTITION_OFFSET = 11;
Gdti96.PARTITION_END = 14;
Gdti96.SERIAL_OFFSET = 55;
Gdti96.SERIAL_END = Gdti96.TOTAL_BITS;
Gdti96.SERIAL_BITS = 41;
Gdti96.MAX_SERIAL = utils_1.default.getMaxValue(Gdti96.SERIAL_BITS); // 2199023255551
Gdti96.TAG_URI = 'gdti-96';
Gdti96.TAG_URI_TEMPLATE = (filter, company, document, serial) => {
return `urn:epc:tag:${this.TAG_URI}:${filter}.${company}.${document}.${serial}`;
}; // F.C.D.S (Filter, Company, Document, Serial)
Gdti96.PID_URI_TEMPLATE = (company, document, serial) => {
return `urn:epc:id:gdti:${company}.${document}.${serial}`;
}; // C.D.S (Company, Document, Serial)
// Partition table columns: Company prefix, Item Reference
Gdti96.PARTITIONS = [
new partition_1.Partition(Gdti96.PARTITION_END, 40, 12, 1, 0),
new partition_1.Partition(Gdti96.PARTITION_END, 37, 11, 4, 1),
new partition_1.Partition(Gdti96.PARTITION_END, 34, 10, 7, 2),
new partition_1.Partition(Gdti96.PARTITION_END, 30, 9, 11, 3),
new partition_1.Partition(Gdti96.PARTITION_END, 27, 8, 14, 4),
new partition_1.Partition(Gdti96.PARTITION_END, 24, 7, 17, 5),
new partition_1.Partition(Gdti96.PARTITION_END, 20, 6, 21, 6),
]; // 6 20 06 21 6