one
Version:
One is a React framework for web, iOS, and Android apps.
146 lines (144 loc) • 4.7 kB
JavaScript
var HEX_DIGITS = "0123456789abcdef";
function bytesToHex(bytes) {
var out = "";
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = bytes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var byte = _step.value;
out += HEX_DIGITS[byte >> 4] + HEX_DIGITS[byte & 15];
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return out;
}
function hexToBytes(hex, verb) {
if (hex.length % 2 !== 0 || /[^0-9a-fA-F]/.test(hex)) {
throw new Error(`${verb}: expected an even-length hex string`);
}
var out = new Uint8Array(hex.length / 2);
for (var index2 = 0; index2 < out.length; index2++) {
out[index2] = parseInt(hex.slice(index2 * 2, index2 * 2 + 2), 16);
}
return out;
}
var BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var BASE64_VALUES = new Int16Array(128).fill(-1);
for (var index = 0; index < BASE64_ALPHABET.length; index++) {
BASE64_VALUES[BASE64_ALPHABET.charCodeAt(index)] = index;
}
function bytesToBase64(bytes) {
var out = "";
for (var index2 = 0; index2 < bytes.length; index2 += 3) {
var first = bytes[index2];
var second = bytes[index2 + 1];
var third = bytes[index2 + 2];
out += BASE64_ALPHABET[first >> 2];
out += BASE64_ALPHABET[(first & 3) << 4 | (second !== null && second !== void 0 ? second : 0) >> 4];
out += second === void 0 ? "=" : BASE64_ALPHABET[(second & 15) << 2 | (third !== null && third !== void 0 ? third : 0) >> 6];
out += third === void 0 ? "=" : BASE64_ALPHABET[third & 63];
}
return out;
}
function base64ToBytes(base64, verb) {
var _loop = function(index3) {
var values = [
0,
1,
2,
3
].map(function(offset) {
var code = base64.charCodeAt(index3 + offset);
return code === 61 ? 0 : BASE64_VALUES[code];
});
if (values.some(function(value) {
return value < 0;
})) throw new Error(`${verb}: expected a base64 string`);
if (at < out.length) out[at++] = values[0] << 2 | values[1] >> 4;
if (at < out.length) out[at++] = (values[1] & 15) << 4 | values[2] >> 2;
if (at < out.length) out[at++] = (values[2] & 3) << 6 | values[3];
};
if (base64.length % 4 !== 0 || /[^A-Za-z0-9+/=]/.test(base64)) {
throw new Error(`${verb}: expected a base64 string`);
}
var padding = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
if (base64.slice(0, base64.length - padding).includes("=")) {
throw new Error(`${verb}: expected a base64 string`);
}
var out = new Uint8Array(base64.length / 4 * 3 - padding);
var at = 0;
for (var index2 = 0; index2 < base64.length; index2 += 4) _loop(index2);
return out;
}
function utf8Encode(text) {
var bytes = [];
for (var index2 = 0; index2 < text.length; index2++) {
var code = text.charCodeAt(index2);
if (code >= 55296 && code <= 56319) {
var next = text.charCodeAt(index2 + 1);
if (next >= 56320 && next <= 57343) {
code = 65536 + (code - 55296 << 10) + (next - 56320);
index2++;
} else {
code = 65533;
}
} else if (code >= 56320 && code <= 57343) {
code = 65533;
}
if (code < 128) {
bytes.push(code);
} else if (code < 2048) {
bytes.push(192 | code >> 6, 128 | code & 63);
} else if (code < 65536) {
bytes.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
} else {
bytes.push(240 | code >> 18, 128 | code >> 12 & 63, 128 | code >> 6 & 63, 128 | code & 63);
}
}
return new Uint8Array(bytes);
}
function exactBytes(view) {
return new Uint8Array(view);
}
function concatBytes(parts) {
var length = parts.reduce(function(sum, part2) {
return sum + part2.length;
}, 0);
var out = new Uint8Array(length);
var at = 0;
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = parts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var part = _step.value;
out.set(part, at);
at += part.length;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return out;
}
export { base64ToBytes, bytesToBase64, bytesToHex, concatBytes, exactBytes, hexToBytes, utf8Encode };
//# sourceMappingURL=bytes.native.js.map