UNPKG

@i-xi-dev/base64

Version:

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

111 lines (110 loc) 3.42 kB
import { inRange, normalizeNumber } from "./number.js"; export var Uint16; (function (Uint16) { /** * The number of bytes used to represent a 16-bit unsigned integer. */ Uint16.BYTES = 2; /** * The number of bits used to represent a 16-bit unsigned integer. */ Uint16.SIZE = 16; /** * The minimum value of 16-bit unsigned integer. */ Uint16.MIN_VALUE = 0x0; /** * The maximum value of 16-bit unsigned integer. */ Uint16.MAX_VALUE = 0xFFFF; /** * Determines whether the passed `test` is a 16-bit unsigned integer. * * @param test - The value to be tested * @returns Whether the passed `test` is a 16-bit unsigned integer. */ function isUint16(test) { return Number.isSafeInteger(test) && inRange(test, [Uint16.MIN_VALUE, Uint16.MAX_VALUE]); } Uint16.isUint16 = isUint16; function rotateLeft(source, amount) { _assertUint16(source, "source"); if (Number.isSafeInteger(amount) !== true) { throw new TypeError("amount"); } let normalizedAmount = amount % Uint16.SIZE; if (normalizedAmount < 0) { normalizedAmount = normalizedAmount + Uint16.SIZE; } if (normalizedAmount === 0) { return source; } return (((source << normalizedAmount) | (source >> (Uint16.SIZE - normalizedAmount))) & Uint16.MAX_VALUE); } Uint16.rotateLeft = rotateLeft; function saturateFromSafeInteger(source) { if (Number.isSafeInteger(source) !== true) { throw new TypeError("source"); } if (source > Uint16.MAX_VALUE) { return Uint16.MAX_VALUE; } else if (source < Uint16.MIN_VALUE) { return Uint16.MIN_VALUE; } return normalizeNumber(source); } Uint16.saturateFromSafeInteger = saturateFromSafeInteger; function truncateFromSafeInteger(source) { if (Number.isSafeInteger(source) !== true) { throw new TypeError("source"); } const count = 65536; if (source === 0) { return 0; } else if (source > 0) { return (source % count); } else { return (count + (source % count)); } } Uint16.truncateFromSafeInteger = truncateFromSafeInteger; function toBytes(source, littleEndian = false) { _assertUint16(source, "source"); const beBytes = [ Math.trunc(source / 0x100), (source % 0x100), ]; return (littleEndian === true) ? beBytes.reverse() : beBytes; } Uint16.toBytes = toBytes; function bitwiseAnd(a, b) { _assertUint16(a, "a"); _assertUint16(b, "b"); return (a & b) & Uint16.MAX_VALUE; } Uint16.bitwiseAnd = bitwiseAnd; function bitwiseOr(a, b) { _assertUint16(a, "a"); _assertUint16(b, "b"); return (a | b) & Uint16.MAX_VALUE; } Uint16.bitwiseOr = bitwiseOr; function bitwiseXOr(a, b) { _assertUint16(a, "a"); _assertUint16(b, "b"); return (a ^ b) & Uint16.MAX_VALUE; } Uint16.bitwiseXOr = bitwiseXOr; })(Uint16 || (Uint16 = {})); function _assertUint16(test, label) { if (Uint16.isUint16(test) !== true) { throw new TypeError(label); } }