@signumjs/util
Version:
Useful utilities and tools for building Signum Network applications
32 lines • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertByteArrayToString = void 0;
const convertByteArrayToString = (byteArray, startIndex = 0, length = null) => {
if (length === 0) {
return '';
}
let bytes = byteArray;
if (startIndex !== 0) {
const len = length === null ? byteArray.length - startIndex : length;
checkBytesToIntInput(bytes, len, startIndex);
bytes = byteArray.slice(startIndex, startIndex + len);
}
const escapedUTF8 = escape(String.fromCharCode.apply(null, Array.from(bytes)));
try {
return decodeURIComponent(escapedUTF8);
}
catch (e) {
throw new Error('Error decoding utf-8 data');
}
};
exports.convertByteArrayToString = convertByteArrayToString;
function checkBytesToIntInput(bytes, numBytes, startIndex = 0) {
if (startIndex < 0) {
throw new Error('Start index should not be negative');
}
if (bytes.length < startIndex + numBytes) {
throw new Error('Need at least ' + (numBytes) + ' bytes to convert to an integer');
}
return startIndex;
}
//# sourceMappingURL=convertByteArrayToString.js.map