@ombori/epc-ean
Version:
EPC Tag Data Standard encoding and decoding library, written in javascript
134 lines (133 loc) • 5.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sgtin198 = void 0;
const utils_1 = __importDefault(require("../utils/utils"));
const epc_1 = require("../epc");
const type_1 = require("../type");
const sgtin96_1 = require("./sgtin96");
class Sgtin198 extends epc_1.Epc {
constructor(hexEpc) {
super(Sgtin198.TOTAL_BITS);
if (hexEpc) {
super.setFromHexString(hexEpc);
}
else {
super.set(Sgtin198.EPC_HEADER, epc_1.Epc.EPC_HEADER_OFFSET, epc_1.Epc.EPC_HEADER_END);
}
}
clone() {
return new Sgtin198().setFromBitArray(this);
}
getType() {
return type_1.Type.SGTIN198;
}
fromTagURI(uri) {
const value = uri.split(':');
try {
if (value[3] === this.TAG_URI) {
const data = value[4].split('.');
const result = new Sgtin198();
result.setFilter(parseInt(data[0]));
result.setPartition(12 - data[1].length);
result.setCompanyPrefix(parseInt(data[1]));
result.setItemReference(parseInt(data[2]));
result.setSerial(data[3]);
return result;
}
}
catch (e) {
// console.log(e)
}
throw new Error(`${uri} is not a known EPC tag URI scheme`);
}
toTagURI() {
// F.C.I.S (Filter, Company, Item, Serial)
let partition = sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()];
return Sgtin198.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
}
toIdURI() {
// C.I.S (Company, Item, Serial)
let partition = sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()];
return Sgtin198.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
}
toBarcode() {
return this.getGtin();
}
getTotalBits() {
return Sgtin198.TOTAL_BITS;
}
getHeader() {
return Sgtin198.EPC_HEADER;
}
getPartition() {
return super.get(Sgtin198.PARTITION_OFFSET, Sgtin198.PARTITION_END);
}
setPartition(value) {
if (value < 0 || value >= sgtin96_1.Sgtin96.PARTITIONS.length) {
throw new Error(`Value '${value}' out of range (min: 0, max: ${sgtin96_1.Sgtin96.PARTITIONS.length - 1})`);
}
super.set(value, sgtin96_1.Sgtin96.PARTITION_OFFSET, sgtin96_1.Sgtin96.PARTITION_END);
return this;
}
getGtin() {
let partition = sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()];
let item = super.getSegmentString(partition.b);
let result = item.substring(0, 1) + super.getSegmentString(partition.a) + item.substring(1);
return result + utils_1.default.computeCheckDigit(result);
}
setGtin(gtin) {
// ean
let partition = sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()];
super.setSegment(gtin.substring(1, partition.a.digits + 1), partition.a);
super.setSegment(Number(gtin.charAt(0) +
gtin.substring(partition.a.digits + 1, partition.a.digits + partition.b.digits)), partition.b);
return this;
}
getCompanyPrefix() {
return super.getSegment(sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()].a);
}
setCompanyPrefix(value) {
super.setSegment(value, sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()].a);
return this;
}
getItemReference() {
return super.getSegment(sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()].b);
}
setItemReference(value) {
super.setSegment(value, sgtin96_1.Sgtin96.PARTITIONS[this.getPartition()].b);
return this;
}
getSerial() {
return super.getString(Sgtin198.SERIAL_OFFSET, Sgtin198.SERIAL_END, Sgtin198.CHAR_BITS);
}
/**
* All values permitted by GS1 General Specifications (up to 20 alphanumeric characters)
* @param value
*/
setSerial(value) {
if (!value || value.length > Sgtin198.MAX_SERIAL_LEN)
throw new Error(`Value '${value}' length out of range (max length: ${Sgtin198.MAX_SERIAL_LEN})`);
super.setString(value, Sgtin198.SERIAL_OFFSET, Sgtin198.SERIAL_END, Sgtin198.CHAR_BITS);
return this;
}
}
exports.Sgtin198 = Sgtin198;
Sgtin198.EPC_HEADER = 0x36;
Sgtin198.TOTAL_BITS = 198;
Sgtin198.PARTITION_OFFSET = 11;
Sgtin198.PARTITION_END = 14;
Sgtin198.SERIAL_OFFSET = 58;
Sgtin198.SERIAL_END = Sgtin198.TOTAL_BITS;
Sgtin198.MAX_SERIAL_LEN = 20;
Sgtin198.CHAR_BITS =
(Sgtin198.SERIAL_END - Sgtin198.SERIAL_OFFSET) / Sgtin198.MAX_SERIAL_LEN; // 7
Sgtin198.TAG_URI = 'sgtin-198';
Sgtin198.TAG_URI_TEMPLATE = (filter, company, item, serial) => {
return `urn:epc:tag:${this.TAG_URI}:${filter}.${company}.${item}.${serial}`;
}; // F.C.I.S (Filter, Company, Item, Serial)
Sgtin198.PID_URI_TEMPLATE = (company, item, serial) => {
return `urn:epc:id:sgtin:${company}.${item}.${serial}`;
}; // C.I.S (Company, Item, Serial)