INNER CODE UNIT · TypeScript
put
blueshift-gg/doppler · packages/core/index.ts:181
function put(view: DataView, at: number, { name, type }: Slot, x: unknown): void {
if (type === 'bool') {
if (typeof x !== 'boolean') throw new TypeError(`${name}: expected a boolean`);
view.setUint8(at, x ? 1 : 0);
return;
}
if (typeof x !== 'bigint' && !(typeof x === 'number' && Number.isInteger(x))) throw new TypeError(`${name}: expected an integer for ${type}`);
const { size, signed } = TYPES[type];
const bits = BigInt(8 * size);
const n = BigInt(x);
const min = signed ? -(1n << (bits - 1n)) : 0n;
const max = (1n << (signed ? bits - 1n : bits)) - 1n;
if (n < min || n > max) throw new RangeError(`${name}: ${x} does not fit ${type}`);
if (size === 8) view.setBigUint64(at, BigInt.asUintN(64, n), true);
else if (size === 4) view.setUint32(at, Number(BigInt.asUintN(32, n)), true);
else if (size === 2) view.setUint16(at, Number(BigInt.asUintN(16, n)), true);
else view.setUint8(at, Number(BigInt.asUintN(8, n)));
}