UNPKG

epc-tds-ts

Version:

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

206 lines (205 loc) 7.18 kB
"use strict"; /* * EPC Tag Data Standard * 2021 Sergio S. */ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var BitArray = /** @class */ (function () { function BitArray(length) { this.length = (length + 7) >> 3; this.data = []; } /** * Set selected bit * @param index */ BitArray.prototype.setBit = function (index) { this.data[index >> 3] |= 1 << (index & 7); }; /** * Clear selected bit * @param index */ BitArray.prototype.clearBit = function (index) { this.data[index >> 3] &= ~(1 << (index & 7)); }; /** * Check if selected bit is set * @param offset * @return */ BitArray.prototype.isBit = function (index) { return (this.data[index >> 3] >> (index & 7)) & 1; }; /** * Clear data of bit array. */ BitArray.prototype.clear = function () { for (var i = 0; i < this.length; i++) { this.data[i] = 0; } }; BitArray.prototype.set = function (value, startIndex, endIndex) { var v = BigInt(value); for (var i = 0n; startIndex < endIndex; i++) { endIndex--; if ((v >> i) & 1n) { // check bit this.setBit(endIndex); } else { this.clearBit(endIndex); } } }; BitArray.prototype.getBigInt = function (startIndex, endIndex) { var result = 0n; for (var i = 0n; startIndex < endIndex; i++) { if (this.isBit(--endIndex)) { result |= 1n << i; // set bit } } return result; }; BitArray.prototype.get = function (startIndex, endIndex) { return Number(this.getBigInt(startIndex, endIndex)); }; BitArray.prototype.getSigned = function (startIndex, endIndex) { var i, result = 0n; for (i = 0n; startIndex < endIndex; i++) { if (this.isBit(--endIndex)) { result |= 1n << i; // set bit } } var mask = 1n << i - 1n; if (result & mask) { // check first bit result = (mask ^ result) - mask; } return Number(result); }; BitArray.prototype.setString = function (value, startIndex, endIndex, charBits) { for (var i = 0; i < value.length && (charBits = Math.min(charBits, endIndex - startIndex)) > 0; ++i) { // iterate bytes this.set(value.charCodeAt(i), startIndex, startIndex += charBits); } for (; startIndex < endIndex; ++startIndex) { // clear remaining bits this.clearBit(startIndex); } }; /** * Return string from bit array * @param startIndex offset * @param endIndex last bit * @param charBits how many bits has stored in a byte (max 8 bits) * @return */ BitArray.prototype.getString = function (startIndex, endIndex, charBits) { var b, result = ""; for (var i = 0; (charBits = Math.min(charBits, endIndex - startIndex)) > 0; ++i) { // iterate bytes if (b = this.get(startIndex, startIndex += charBits)) { result += String.fromCharCode(b); } } return result; }; /** * Return a hexadecimal string representation of the bit array. * @return hexadecimal base 16 */ BitArray.prototype.toHexString = function () { var b, result = ""; for (var i = 0; i < this.length; ++i) { b = this.data[i]; // iterate bytes result += BitArray.REVERSE_HEX_CHARS[b & 0xf] + BitArray.REVERSE_HEX_CHARS[(b & 0xf0) >> 4]; } return result; }; /** * Return a binary string representation of the bit array. * @return binary base 2 */ BitArray.prototype.toBitString = function () { var b, result = ""; for (var i = 0; i < this.length; ++i) { b = this.data[i]; // iterate bytes for (var j = 0; j < 8; ++j) { result += ((b >>> j) & 1) ? "1" : "0"; } } return result; }; BitArray.prototype.setFromBitArray = function (bitArray) { this.data = __spreadArray([], bitArray.data, true); return this; }; BitArray.prototype.setFromHexString = function (hex) { if (hex.length & 1) { // odd check hex = '0' + hex; // even hex } this.data = new Array(hex.length >> 1); for (var j = 0, i = 0; i < this.length; i++, j += 2) { this.data[i] = BitArray.REVERSE_DEC_TABLE[hex.charCodeAt(j)] | (BitArray.REVERSE_DEC_TABLE[hex.charCodeAt(j + 1)] << 4); } return this; }; BitArray.prototype.not = function () { var result = new BitArray(this.length << 3); for (var i = 0; i < result.data.length; ++i) { result.data[i] = ~this.data[i]; } return result; }; BitArray.prototype.or = function (bitArray) { var result; if (bitArray.length > this.length) { result = new BitArray(bitArray.data.length << 3); } else { result = new BitArray(this.length << 3); } for (var i = 0; i < result.data.length; ++i) { result.data[i] = bitArray.data[i] | this.data[i]; } return result; }; BitArray.prototype.xor = function (bitArray) { var result; if (bitArray.length > this.length) { result = new BitArray(bitArray.data.length << 3); } else { result = new BitArray(this.length << 3); } for (var i = 0; i < result.data.length; ++i) { result.data[i] = bitArray.data[i] ^ this.data[i]; } return result; }; BitArray.prototype.and = function (bitArray) { var result; if (bitArray.length > this.length) { result = new BitArray(bitArray.data.length << 3); } else { result = new BitArray(this.length << 3); } for (var i = 0; i < result.data.length; ++i) { result.data[i] = bitArray.data[i] & this.data[i]; } return result; }; BitArray.REVERSE_HEX_CHARS = ['0', '8', '4', 'C', '2', 'A', '6', 'E', '1', '9', '5', 'D', '3', 'B', '7', 'F']; BitArray.REVERSE_DEC_TABLE = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, -1, -1, -1, -1, -1, -1, -1, 5, 13, 3, 11, 7, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 13, 3, 11, 7, 15, -1]; return BitArray; }()); module.exports = { BitArray: BitArray };