UNPKG

@i-xi-dev/base64

Version:

A JavaScript Base64 encoder and decoder, implements Forgiving base64 defined in WHATWG Infra Standard.

123 lines (122 loc) 4.21 kB
import { inRange, normalizeNumber } from "./number.js"; import { NumberRange } from "./number_range.js"; import { Radix } from "./radix.js"; import { SafeInteger } from "./safe_integer.js"; function _toSafeIntegerFromOptions(options = {}) { const clampRange = NumberRange.resolve(options?.clampRange); clampRange[0] = Math.max(clampRange[0], Uint8.MIN_VALUE); clampRange[1] = Math.min(clampRange[1], Uint8.MAX_VALUE); return SafeInteger.FromOptions.resolve({ ...options, clampRange, }); } export var Uint8; (function (Uint8) { /** * The number of bytes used to represent an 8-bit unsigned integer. */ Uint8.BYTES = 1; /** * The number of bits used to represent an 8-bit unsigned integer. */ Uint8.SIZE = 8; /** * The minimum value of 8-bit unsigned integer. */ Uint8.MIN_VALUE = 0x0; /** * The maximum value of 8-bit unsigned integer. */ Uint8.MAX_VALUE = 0xFF; /** * Determines whether the passed `test` is an 8-bit unsigned integer. * * @param test - The value to be tested * @returns Whether the passed `test` is an 8-bit unsigned integer. */ function isUint8(test) { return Number.isSafeInteger(test) && inRange(test, [Uint8.MIN_VALUE, Uint8.MAX_VALUE]); } Uint8.isUint8 = isUint8; function fromNumber(source, options) { const resolvedOptions = _toSafeIntegerFromOptions(options); return SafeInteger.fromNumber(source, resolvedOptions); } Uint8.fromNumber = fromNumber; function fromBigInt(source, options) { const resolvedOptions = _toSafeIntegerFromOptions(options); return SafeInteger.fromBigInt(source, resolvedOptions); } Uint8.fromBigInt = fromBigInt; function toBigInt(source) { if (isUint8(source)) { return BigInt(source); } throw new TypeError("source"); } Uint8.toBigInt = toBigInt; function fromString(source, options) { const resolvedOptions = _toSafeIntegerFromOptions(options); return SafeInteger.fromString(source, resolvedOptions); } Uint8.fromString = fromString; function toString(source) { if (isUint8(source)) { return normalizeNumber(source).toString(Radix.DECIMAL); } throw new TypeError("source"); } Uint8.toString = toString; function rotateLeft(source, amount) { if (Uint8.isUint8(source) !== true) { throw new TypeError("source"); } if (Number.isSafeInteger(amount) !== true) { throw new TypeError("amount"); } let normalizedAmount = amount % Uint8.SIZE; if (normalizedAmount < 0) { normalizedAmount = normalizedAmount + Uint8.SIZE; } if (normalizedAmount === 0) { return source; } return (((source << normalizedAmount) | (source >> (Uint8.SIZE - normalizedAmount))) & 0b11111111); } Uint8.rotateLeft = rotateLeft; // Uint8ClampedArrayにオーバーフローorアンダーフローする整数をセットしたのと同じ結果 function saturateFromSafeInteger(source) { if (Number.isSafeInteger(source) !== true) { throw new TypeError("source"); } if (source > Uint8.MAX_VALUE) { return Uint8.MAX_VALUE; } else if (source < Uint8.MIN_VALUE) { return Uint8.MIN_VALUE; } return normalizeNumber(source); } Uint8.saturateFromSafeInteger = saturateFromSafeInteger; // Uint8Arrayにオーバーフローorアンダーフローする整数をセットしたのと同じ結果 function truncateFromSafeInteger(source) { if (Number.isSafeInteger(source) !== true) { throw new TypeError("source"); } const count = 256; if (source === 0) { return 0; } else if (source > 0) { return (source % count); } else { return (count + (source % count)); } } Uint8.truncateFromSafeInteger = truncateFromSafeInteger; })(Uint8 || (Uint8 = {}));