INNER CODE UNIT · TypeScript

layout

blueshift-gg/doppler · packages/core/index.ts:151

function layout(fields: unknown): { slots: Slot[]; size: number } {
  if (!Array.isArray(fields) || fields.length === 0) throw new TypeError('a payload needs at least one field');
  const slots: Slot[] = [];
  let size = 0;
  for (const field of fields as FieldLike[]) {
    const { name, type, len = 1 } = field;
    if (typeof name !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new TypeError('a field name must be an identifier');
    if (slots.some((s) => s.name === name)) throw new TypeError(`${name}: a field name must be unique`);
    if (!Object.hasOwn(TYPES, type)) throw new TypeError(`${name}: a field type is one of ${Object.keys(TYPES).join(', ')}`);
    if (!Number.isInteger(len) || len < 1 || len > 0xffff) throw new TypeError(`${name}: a field length is 1 to 65535`);
    slots.push({ name, type: type as Ty, len, offset: size });
    size += TYPES[type as Ty].size * len;
  }
  return { slots, size };
}

function get(view: DataView, at: number, type: Ty): number | bigint | boolean {
  switch (type) {

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…