@i-xi-dev/base64
Version:
A JavaScript Base64 encoder and decoder, implements Forgiving base64 defined in WHATWG Infra Standard.
74 lines (73 loc) • 2.25 kB
JavaScript
import { inRange } from "./number.js";
export var Uint24;
(function (Uint24) {
/**
* The number of bytes used to represent a 24-bit unsigned integer.
*/
Uint24.BYTES = 3;
/**
* The number of bits used to represent a 24-bit unsigned integer.
*/
Uint24.SIZE = 24;
/**
* The minimum value of 24-bit unsigned integer.
*/
Uint24.MIN_VALUE = 0x0;
/**
* The maximum value of 24-bit unsigned integer.
*/
Uint24.MAX_VALUE = 0xFFFFFF;
/**
* Determines whether the passed `test` is a 24-bit unsigned integer.
*
* @param test - The value to be tested
* @returns Whether the passed `test` is a 24-bit unsigned integer.
*/
function isUint24(test) {
return Number.isSafeInteger(test) &&
inRange(test, [Uint24.MIN_VALUE, Uint24.MAX_VALUE]);
}
Uint24.isUint24 = isUint24;
function rotateLeft(source, amount) {
_assertUint24(source, "source");
if (Number.isSafeInteger(amount) !== true) {
throw new TypeError("amount");
}
let normalizedAmount = amount % Uint24.SIZE;
if (normalizedAmount < 0) {
normalizedAmount = normalizedAmount + Uint24.SIZE;
}
if (normalizedAmount === 0) {
return source;
}
return (((source << normalizedAmount) |
(source >> (Uint24.SIZE - normalizedAmount))) & Uint24.MAX_VALUE);
}
Uint24.rotateLeft = rotateLeft;
// saturateFromSafeInteger
// truncateFromSafeInteger
// toBytes
function bitwiseAnd(a, b) {
_assertUint24(a, "a");
_assertUint24(b, "b");
return (a & b) & Uint24.MAX_VALUE;
}
Uint24.bitwiseAnd = bitwiseAnd;
function bitwiseOr(a, b) {
_assertUint24(a, "a");
_assertUint24(b, "b");
return (a | b) & Uint24.MAX_VALUE;
}
Uint24.bitwiseOr = bitwiseOr;
function bitwiseXOr(a, b) {
_assertUint24(a, "a");
_assertUint24(b, "b");
return (a ^ b) & Uint24.MAX_VALUE;
}
Uint24.bitwiseXOr = bitwiseXOr;
})(Uint24 || (Uint24 = {}));
function _assertUint24(test, label) {
if (Uint24.isUint24(test) !== true) {
throw new TypeError(label);
}
}