one
Version:
One is a React framework for web, iOS, and Android apps.
105 lines (103 loc) • 3.49 kB
JavaScript
const HEX_DIGITS = "0123456789abcdef";
function bytesToHex(bytes) {
let out = "";
for (const byte of bytes) out += HEX_DIGITS[byte >> 4] + HEX_DIGITS[byte & 15];
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`);
}
const out = new Uint8Array(hex.length / 2);
for (let index = 0; index < out.length; index++) {
out[index] = parseInt(hex.slice(index * 2, index * 2 + 2), 16);
}
return out;
}
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BASE64_VALUES = new Int16Array(128).fill(-1);
for (let index = 0; index < BASE64_ALPHABET.length; index++) {
BASE64_VALUES[BASE64_ALPHABET.charCodeAt(index)] = index;
}
function bytesToBase64(bytes) {
let out = "";
for (let index = 0; index < bytes.length; index += 3) {
const first = bytes[index];
const second = bytes[index + 1];
const third = bytes[index + 2];
out += BASE64_ALPHABET[first >> 2];
out += BASE64_ALPHABET[(first & 3) << 4 | (second ?? 0) >> 4];
out += second === void 0 ? "=" : BASE64_ALPHABET[(second & 15) << 2 | (third ?? 0) >> 6];
out += third === void 0 ? "=" : BASE64_ALPHABET[third & 63];
}
return out;
}
function base64ToBytes(base64, verb) {
if (base64.length % 4 !== 0 || /[^A-Za-z0-9+/=]/.test(base64)) {
throw new Error(`${verb}: expected a base64 string`);
}
const padding = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
if (base64.slice(0, base64.length - padding).includes("=")) {
throw new Error(`${verb}: expected a base64 string`);
}
const out = new Uint8Array(base64.length / 4 * 3 - padding);
let at = 0;
for (let index = 0; index < base64.length; index += 4) {
const values = [
0,
1,
2,
3
].map((offset) => {
const code = base64.charCodeAt(index + offset);
return code === 61 ? 0 : BASE64_VALUES[code];
});
if (values.some((value) => 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];
}
return out;
}
function utf8Encode(text) {
const bytes = [];
for (let index = 0; index < text.length; index++) {
let code = text.charCodeAt(index);
if (code >= 55296 && code <= 56319) {
const next = text.charCodeAt(index + 1);
if (next >= 56320 && next <= 57343) {
code = 65536 + (code - 55296 << 10) + (next - 56320);
index++;
} 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) {
const length = parts.reduce((sum, part) => sum + part.length, 0);
const out = new Uint8Array(length);
let at = 0;
for (const part of parts) {
out.set(part, at);
at += part.length;
}
return out;
}
export { base64ToBytes, bytesToBase64, bytesToHex, concatBytes, exactBytes, hexToBytes, utf8Encode };
//# sourceMappingURL=bytes.mjs.map