UNPKG

epc-tds-ts

Version:

EPC Tag Data Standard encoding and decoding library, written in javascript for Node.js

129 lines (128 loc) 6.22 kB
"use strict"; /** * 198-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 Sgtin96 = require('./sgtin96').Sgtin96; var Sgtin198 = /** @class */ (function (_super) { __extends(Sgtin198, _super); function Sgtin198(hexEpc) { var _this = _super.call(this, Sgtin198.TOTAL_BITS) || this; if (hexEpc) { _super.prototype.setFromHexString.call(_this, hexEpc); } else { _super.prototype.set.call(_this, Sgtin198.EPC_HEADER, Epc.EPC_HEADER_OFFSET, Epc.EPC_HEADER_END); } return _this; } Sgtin198.prototype.clone = function () { return new Sgtin198().setFromBitArray(this); }; Sgtin198.prototype.getType = function () { return Type.SGTIN198; }; Sgtin198.prototype.toTagURI = function () { var partition = Sgtin96.PARTITIONS[this.getPartition()]; return Sgtin198.TAG_URI_TEMPLATE(this.getFilter(), this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial()); }; Sgtin198.prototype.toIdURI = function () { var partition = Sgtin96.PARTITIONS[this.getPartition()]; return Sgtin198.PID_URI_TEMPLATE(this.getSegmentString(partition.a), this.getSegmentString(partition.b), this.getSerial()); }; Sgtin198.prototype.toBarcode = function () { return this.getGtin(); }; Sgtin198.prototype.getTotalBits = function () { return Sgtin198.TOTAL_BITS; }; Sgtin198.prototype.getHeader = function () { return Sgtin198.EPC_HEADER; }; Sgtin198.prototype.getPartition = function () { return _super.prototype.get.call(this, Sgtin198.PARTITION_OFFSET, Sgtin198.PARTITION_END); }; Sgtin198.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; }; Sgtin198.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); }; Sgtin198.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; }; Sgtin198.prototype.getCompanyPrefix = function () { return _super.prototype.getSegment.call(this, Sgtin96.PARTITIONS[this.getPartition()].a); }; Sgtin198.prototype.setCompanyPrefix = function (value) { _super.prototype.setSegment.call(this, value, Sgtin96.PARTITIONS[this.getPartition()].a); return this; }; Sgtin198.prototype.getItemReference = function () { return _super.prototype.getSegment.call(this, Sgtin96.PARTITIONS[this.getPartition()].b); }; Sgtin198.prototype.setItemReference = function (value) { _super.prototype.setSegment.call(this, value, Sgtin96.PARTITIONS[this.getPartition()].b); return this; }; Sgtin198.prototype.getSerial = function () { return _super.prototype.getString.call(this, Sgtin198.SERIAL_OFFSET, Sgtin198.SERIAL_END, Sgtin198.CHAR_BITS); }; /** * All values permitted by GS1 General Specifications (up to 20 alphanumeric characters) * @param value */ Sgtin198.prototype.setSerial = function (value) { if (!value || value.length > Sgtin198.MAX_SERIAL_LEN) throw new Error("Value '".concat(value, "' length out of range (max length: ").concat(Sgtin198.MAX_SERIAL_LEN, ")")); _super.prototype.setString.call(this, value, Sgtin198.SERIAL_OFFSET, Sgtin198.SERIAL_END, Sgtin198.CHAR_BITS); return this; }; 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_TEMPLATE = function (filter, company, item, serial) { return "urn:epc:tag:sgtin-198:".concat(filter, ".").concat(company, ".").concat(item, ".").concat(serial); }; // F.C.I.S (Filter, Company, Item, Serial) Sgtin198.PID_URI_TEMPLATE = function (company, item, serial) { return "urn:epc:id:sgtin:".concat(company, ".").concat(item, ".").concat(serial); }; // C.I.S (Company, Item, Serial) return Sgtin198; }(Epc)); module.exports = { Sgtin198: Sgtin198 };