epc-tds-ts
Version:
EPC Tag Data Standard encoding and decoding library, written in javascript for Node.js
133 lines (132 loc) • 6.46 kB
JavaScript
"use strict";
/**
* 96-bit Serialised Global Trade Item Number (SGTIN)
*
* The Serialised Global Trade Item Number EPC scheme is used to assign a unique identity to an
* instance of a trade item, such as a specific instance of a product or SKU.
*
* Typical use: Trade item
*
* @author Sergio S.
*
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Utils = require('../utils/utils');
var Epc = require('../epc').Epc;
var Type = require('../type').Type;
var Partition = require('../partition').Partition;
var Sgtin96 = /** @class */ (function (_super) {
__extends(Sgtin96, _super);
function Sgtin96(hexEpc) {
var _this = _super.call(this, Sgtin96.TOTAL_BITS) || this;
if (hexEpc) {
_super.prototype.setFromHexString.call(_this, hexEpc);
}
else {
_super.prototype.set.call(_this, Sgtin96.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END);
}
return _this;
}
Sgtin96.prototype.clone = function () {
return new Sgtin96().setFromBitArray(this);
};
Sgtin96.prototype.getType = function () {
return Type.SGTIN96;
};
Sgtin96.prototype.toTagURI = function () {
var partition = Sgtin96.PARTITIONS[this.getPartition()];
return Sgtin96.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
};
Sgtin96.prototype.toIdURI = function () {
var partition = Sgtin96.PARTITIONS[this.getPartition()];
return Sgtin96.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial());
};
Sgtin96.prototype.toBarcode = function () {
return this.getGtin();
};
Sgtin96.prototype.getTotalBits = function () {
return Sgtin96.TOTAL_BITS;
};
Sgtin96.prototype.getHeader = function () {
return Sgtin96.EPC_HEADER;
};
Sgtin96.prototype.getPartition = function () {
return _super.prototype.get.call(this, Sgtin96.PARTITION_OFFSET, Sgtin96.PARTITION_END);
};
Sgtin96.prototype.setPartition = function (value) {
if (value < 0 || value >= Sgtin96.PARTITIONS.length) {
throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Sgtin96.PARTITIONS.length - 1, ")"));
}
_super.prototype.set.call(this, value, Sgtin96.PARTITION_OFFSET, Sgtin96.PARTITION_END);
return this;
};
Sgtin96.prototype.getGtin = function () {
var partition = Sgtin96.PARTITIONS[this.getPartition()];
var item = _super.prototype.getSegmentString.call(this, partition.b);
var result = item.substring(0, 1) + _super.prototype.getSegmentString.call(this, partition.a) + item.substring(1);
return result + Utils.computeCheckDigit(result);
};
Sgtin96.prototype.setGtin = function (gtin) {
var partition = Sgtin96.PARTITIONS[this.getPartition()];
_super.prototype.setSegment.call(this, gtin.substring(1, partition.a.digits + 1), partition.a);
_super.prototype.setSegment.call(this, Number(gtin.charAt(0) + gtin.substring(partition.a.digits + 1, partition.a.digits + partition.b.digits)), partition.b);
return this;
};
Sgtin96.prototype.getCompanyPrefix = function () {
return _super.prototype.getSegment.call(this, Sgtin96.PARTITIONS[this.getPartition()].a);
};
Sgtin96.prototype.setCompanyPrefix = function (value) {
_super.prototype.setSegment.call(this, value, Sgtin96.PARTITIONS[this.getPartition()].a);
return this;
};
Sgtin96.prototype.getItemReference = function () {
return _super.prototype.getSegment.call(this, Sgtin96.PARTITIONS[this.getPartition()].b);
};
Sgtin96.prototype.setItemReference = function (value) {
_super.prototype.setSegment.call(this, value, Sgtin96.PARTITIONS[this.getPartition()].b);
return this;
};
Sgtin96.prototype.getSerial = function () {
return _super.prototype.get.call(this, Sgtin96.SERIAL_OFFSET, Sgtin96.SERIAL_END);
};
Sgtin96.prototype.setSerial = function (value) {
if (value > Sgtin96.MAX_SERIAL)
throw new Error("Value '".concat(value, "' out of range (min: 0, max: ").concat(Sgtin96.MAX_SERIAL, ")"));
_super.prototype.set.call(this, value, Sgtin96.SERIAL_OFFSET, Sgtin96.SERIAL_END);
return this;
};
Sgtin96.EPC_HEADER = 0x30;
Sgtin96.TOTAL_BITS = 96;
Sgtin96.PARTITION_OFFSET = 11;
Sgtin96.PARTITION_END = 14;
Sgtin96.SERIAL_OFFSET = 58;
Sgtin96.SERIAL_END = Sgtin96.TOTAL_BITS;
Sgtin96.SERIAL_BITS = 38;
Sgtin96.MAX_SERIAL = Utils.getMaxValue(Sgtin96.SERIAL_BITS); // 274877906943
Sgtin96.TAG_URI_TEMPLATE = function (filter, company, item, serial) { return "urn:epc:tag:sgtin-96:".concat(filter, ".").concat(company, ".").concat(item, ".").concat(serial); }; // F.C.I.S (Filter, Company, Item, Serial)
Sgtin96.PID_URI_TEMPLATE = function (company, item, serial) { return "urn:epc:id:sgtin:".concat(company, ".").concat(item, ".").concat(serial); }; // C.I.S (Company, Item, Serial)
// Partition table columns: Company prefix, Item Reference
Sgtin96.PARTITIONS = [new Partition(Sgtin96.PARTITION_END, 40, 12, 4, 1),
new Partition(Sgtin96.PARTITION_END, 37, 11, 7, 2),
new Partition(Sgtin96.PARTITION_END, 34, 10, 10, 3),
new Partition(Sgtin96.PARTITION_END, 30, 9, 14, 4),
new Partition(Sgtin96.PARTITION_END, 27, 8, 17, 5),
new Partition(Sgtin96.PARTITION_END, 24, 7, 20, 6),
new Partition(Sgtin96.PARTITION_END, 20, 6, 24, 7)]; // 6 20 06 24 7
return Sgtin96;
}(Epc));
module.exports = { Sgtin96: Sgtin96 };