enc-utils
Version:
Byte encoding utils
311 lines • 8.86 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is_typedarray_1 = __importDefault(require("is-typedarray"));
const typedarray_to_buffer_1 = __importDefault(require("typedarray-to-buffer"));
const ENC_HEX = 'hex';
const ENC_UTF8 = 'utf8';
const ENC_BIN = 'binary';
const TYPE_BUFFER = 'buffer';
const TYPE_ARRAY = 'array';
const TYPE_TYPED_ARRAY = 'typed-array';
const TYPE_ARRAY_BUFFER = 'array-buffer';
const STRING_ZERO = '0';
function bufferToArray(buf) {
return new Uint8Array(buf);
}
exports.bufferToArray = bufferToArray;
function bufferToHex(buf, prefixed = false) {
const hex = buf.toString(ENC_HEX);
return prefixed ? addHexPrefix(hex) : hex;
}
exports.bufferToHex = bufferToHex;
function bufferToUtf8(buf) {
return buf.toString(ENC_UTF8);
}
exports.bufferToUtf8 = bufferToUtf8;
function bufferToNumber(buf) {
return buf.readUIntBE(0, buf.length);
}
exports.bufferToNumber = bufferToNumber;
function bufferToBinary(buf) {
return arrayToBinary(bufferToArray(buf));
}
exports.bufferToBinary = bufferToBinary;
function arrayToBuffer(arr) {
return typedarray_to_buffer_1.default(arr);
}
exports.arrayToBuffer = arrayToBuffer;
function arrayToHex(arr, prefixed = false) {
return bufferToHex(arrayToBuffer(arr), prefixed);
}
exports.arrayToHex = arrayToHex;
function arrayToUtf8(arr) {
return bufferToUtf8(arrayToBuffer(arr));
}
exports.arrayToUtf8 = arrayToUtf8;
function arrayToNumber(arr) {
return bufferToNumber(arrayToBuffer(arr));
}
exports.arrayToNumber = arrayToNumber;
function arrayToBinary(arr) {
return Array.from(arr)
.map(numberToBinary)
.join('');
}
exports.arrayToBinary = arrayToBinary;
function hexToBuffer(hex) {
return Buffer.from(removeHexPrefix(hex), ENC_HEX);
}
exports.hexToBuffer = hexToBuffer;
function hexToArray(hex) {
return bufferToArray(hexToBuffer(hex));
}
exports.hexToArray = hexToArray;
function hexToUtf8(hex) {
return bufferToUtf8(hexToBuffer(hex));
}
exports.hexToUtf8 = hexToUtf8;
function hexToNumber(hex) {
return arrayToNumber(hexToArray(hex));
}
exports.hexToNumber = hexToNumber;
function hexToBinary(hex) {
return arrayToBinary(hexToArray(hex));
}
exports.hexToBinary = hexToBinary;
function utf8ToBuffer(utf8) {
return Buffer.from(utf8, ENC_UTF8);
}
exports.utf8ToBuffer = utf8ToBuffer;
function utf8ToArray(utf8) {
return bufferToArray(utf8ToBuffer(utf8));
}
exports.utf8ToArray = utf8ToArray;
function utf8ToHex(utf8, prefixed = false) {
return bufferToHex(utf8ToBuffer(utf8), prefixed);
}
exports.utf8ToHex = utf8ToHex;
function utf8ToNumber(utf8) {
const num = parseInt(utf8, 10);
assert(isDefined(num), 'Number can only safely store up to 53 bits');
return num;
}
exports.utf8ToNumber = utf8ToNumber;
function utf8ToBinary(utf8) {
return arrayToBinary(utf8ToArray(utf8));
}
exports.utf8ToBinary = utf8ToBinary;
function numberToBuffer(num) {
return binaryToBuffer(numberToBinary(num));
}
exports.numberToBuffer = numberToBuffer;
function numberToArray(num) {
return binaryToArray(numberToBinary(num));
}
exports.numberToArray = numberToArray;
function numberToHex(num, prefixed) {
return binaryToHex(numberToBinary(num), prefixed);
}
exports.numberToHex = numberToHex;
function numberToUtf8(num) {
return `${num}`;
}
exports.numberToUtf8 = numberToUtf8;
function numberToBinary(num) {
const bin = (num >>> 0).toString(2);
return sanitizeBytes(bin);
}
exports.numberToBinary = numberToBinary;
function binaryToBuffer(bin) {
return arrayToBuffer(binaryToArray(bin));
}
exports.binaryToBuffer = binaryToBuffer;
function binaryToArray(bin) {
return new Uint8Array(splitBytes(bin).map(x => parseInt(x, 2)));
}
exports.binaryToArray = binaryToArray;
function binaryToHex(bin, prefixed) {
return arrayToHex(binaryToArray(bin), prefixed);
}
exports.binaryToHex = binaryToHex;
function binaryToUtf8(bin) {
return arrayToUtf8(binaryToArray(bin));
}
exports.binaryToUtf8 = binaryToUtf8;
function binaryToNumber(bin) {
return arrayToNumber(binaryToArray(bin));
}
exports.binaryToNumber = binaryToNumber;
function isBinaryString(str) {
if (typeof str !== 'string' || !new RegExp(/^[01]+$/).test(str)) {
return false;
}
if (str.length % 8 !== 0) {
return false;
}
return true;
}
exports.isBinaryString = isBinaryString;
function isHexString(str, length) {
if (typeof str !== 'string' || !str.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && str.length !== 2 + 2 * length) {
return false;
}
return true;
}
exports.isHexString = isHexString;
function isBuffer(val) {
return Buffer.isBuffer(val);
}
exports.isBuffer = isBuffer;
function isTypedArray(val) {
return is_typedarray_1.default.strict(val) && !isBuffer(val);
}
exports.isTypedArray = isTypedArray;
function isArrayBuffer(val) {
return (!isTypedArray(val) &&
!isBuffer(val) &&
typeof val.byteLength !== 'undefined');
}
exports.isArrayBuffer = isArrayBuffer;
function getType(val) {
if (isBuffer(val)) {
return TYPE_BUFFER;
}
else if (isTypedArray(val)) {
return TYPE_TYPED_ARRAY;
}
else if (isArrayBuffer(val)) {
return TYPE_ARRAY_BUFFER;
}
else if (Array.isArray(val)) {
return TYPE_ARRAY;
}
else {
return typeof val;
}
}
exports.getType = getType;
function getEncoding(str) {
if (isBinaryString(str)) {
return ENC_BIN;
}
if (isHexString(str)) {
return ENC_HEX;
}
return ENC_UTF8;
}
exports.getEncoding = getEncoding;
function concatBuffers(...args) {
const result = Buffer.concat(args);
return result;
}
exports.concatBuffers = concatBuffers;
function concatArrays(...args) {
let result = [];
args.forEach(arg => (result = result.concat(Array.from(arg))));
return new Uint8Array([...result]);
}
exports.concatArrays = concatArrays;
function trimLeft(data, length) {
const diff = data.length - length;
if (diff > 0) {
data = data.slice(diff);
}
return data;
}
exports.trimLeft = trimLeft;
function trimRight(data, length) {
return data.slice(0, length);
}
exports.trimRight = trimRight;
function calcByteLength(length, byteSize = 8) {
const remainder = length % byteSize;
return remainder
? ((length - remainder) / byteSize) * byteSize + byteSize
: length;
}
exports.calcByteLength = calcByteLength;
function splitBytes(str, byteSize = 8) {
const bytes = sanitizeBytes(str).match(new RegExp(`.{${byteSize}}`, 'gi'));
return Array.from(bytes || []);
}
exports.splitBytes = splitBytes;
function swapBytes(str) {
return splitBytes(str)
.map(reverseString)
.join('');
}
exports.swapBytes = swapBytes;
function swapHex(str) {
return binaryToHex(swapBytes(hexToBinary(str)));
}
exports.swapHex = swapHex;
function sanitizeBytes(str, byteSize = 8, padding = STRING_ZERO) {
return padLeft(str, calcByteLength(str.length, byteSize), padding);
}
exports.sanitizeBytes = sanitizeBytes;
function padLeft(str, length, padding = STRING_ZERO) {
return padString(str, length, true, padding);
}
exports.padLeft = padLeft;
function padRight(str, length, padding = STRING_ZERO) {
return padString(str, length, false, padding);
}
exports.padRight = padRight;
function removeHexPrefix(hex) {
return hex.replace(/^0x/, '');
}
exports.removeHexPrefix = removeHexPrefix;
function addHexPrefix(hex) {
return hex.startsWith('0x') ? hex : `0x${hex}`;
}
exports.addHexPrefix = addHexPrefix;
function sanitizeHex(hex) {
hex = removeHexPrefix(hex);
hex = sanitizeBytes(hex, 2);
if (hex) {
hex = addHexPrefix(hex);
}
return hex;
}
exports.sanitizeHex = sanitizeHex;
function removeHexLeadingZeros(hex) {
const prefixed = hex.startsWith('0x');
hex = removeHexPrefix(hex);
hex = hex.startsWith(STRING_ZERO) ? hex.substring(1) : hex;
return prefixed ? addHexPrefix(hex) : hex;
}
exports.removeHexLeadingZeros = removeHexLeadingZeros;
function isUndefined(value) {
return typeof value === 'undefined';
}
function isDefined(value) {
return !isUndefined(value);
}
function assert(assertion, errorMessage) {
if (!assertion) {
throw new Error(errorMessage);
}
}
function reverseString(str) {
return str
.split('')
.reverse()
.join('');
}
function padString(str, length, left, padding = STRING_ZERO) {
const diff = length - str.length;
let result = str;
if (diff > 0) {
const pad = padding.repeat(diff);
result = left ? pad + str : str + pad;
}
return result;
}
//# sourceMappingURL=index.js.map
;