function arrayLP
arrayLP<TDecoded>(
elementType: Coder<TDecoded>,
lengthType: Coder<number>
): Coder<TDecoded[]>

Creates a Coder for length-prefixed arrays of a given element type.

The array is encoded with a length prefix followed by the elements. The length is encoded using the provided lengthType coder.

Examples

Example 1

import { assertEquals } from "@std/assert";
import { arrayLP } from "@hertzg/binstruct/array";
import { u16le, u32le, s32le, f32le, u8le } from "@hertzg/binstruct/numeric";
import { struct } from "@hertzg/binstruct/struct";
import { stringLP } from "@hertzg/binstruct/string";

// Define a vertex structure for 3D graphics
const vertexCoder = struct({
  x: f32le(), // X coordinate
  y: f32le(), // Y coordinate
  z: f32le(), // Z coordinate
  r: u8le(),  // Red component (0-255)
  g: u8le(),  // Green component (0-255)
  b: u8le(),  // Blue component (0-255)
});

// Define a 3D mesh structure with vertex arrays
const meshCoder = struct({
  meshId: u32le(),                    // Mesh identifier
  vertexCount: u16le(),               // Number of vertices
  vertices: arrayLP(vertexCoder, u16le()), // Array of vertices
  indices: arrayLP(u16le(), u32le()), // Triangle indices
  materialName: stringLP(u16le()),    // Material name
});

// Create sample 3D mesh data
const mesh = {
  meshId: 1001,
  vertexCount: 3,
  vertices: [
    { x: 0.0, y: 0.0, z: 0.0, r: 255, g: 0, b: 0 },     // Red vertex
    { x: 1.0, y: 0.0, z: 0.0, r: 0, g: 255, b: 0 },     // Green vertex
    { x: 0.5, y: 1.0, z: 0.0, r: 0, g: 0, b: 255 },     // Blue vertex
  ],
  indices: [0, 1, 2], // Triangle indices
  materialName: "default_material",
};

const buffer = new Uint8Array(1000);
const bytesWritten = meshCoder.encode(mesh, buffer);
const [decoded, bytesRead] = meshCoder.decode(buffer);
assertEquals(decoded, mesh);
assertEquals(bytesWritten, bytesRead);

Type Parameters

TDecoded

Parameters

elementType: Coder<TDecoded>

The coder for individual array elements

lengthType: Coder<number>

The coder for the array length (typically u32 or u16)

Return Type

Coder<TDecoded[]>

A Coder that can encode/decode arrays of the element type

Usage

import { arrayLP } from "array/array.ts";