• Creates multiple TypedArray views on a single ArrayBuffer or SharedArrayBuffer. The function takes a constructor (either ArrayBuffer or SharedArrayBuffer) and a configuration object, then allocates the buffer and returns the specified TypedArray views.

    Type Parameters

    • T extends Record<string, [ArrayTypeConstructor<SupportedArrays>, number]>

      A record specifying the TypedArray types and sizes.

    Parameters

    • BufferType: (new (size: number) => ArrayBuffer)

      The constructor for the buffer, either ArrayBuffer or SharedArrayBuffer.

        • new (size): ArrayBuffer
        • Parameters

          • size: number

          Returns ArrayBuffer

    • config: T

      An object specifying the desired views. Each key represents the name of the view, and the value is a tuple where:

      • The first element is the TypedArray class name (e.g., Float32Array, Uint32Array).
      • The second element is the number of elements for that view.

    Returns {
        [K in keyof T]: InstanceType<T[K][0]>
    }

    An object containing the views, with each key corresponding to the provided configuration.

    import { createArrayBufferViews } from '@ain1084/array-buffer-partitioner';

    const views = createArrayBufferViews(ArrayBuffer, {
    data: [Float32Array, 1024],
    index: [Uint32Array, 1],
    flag: [Uint8Array, 1]
    });

    console.log(views.data.length); // 1024
    console.log(views.data.byteOffset); // 0
    console.log(views.index.length); // 1
    console.log(views.flag.length); // 1
    console.log(views.flag.byteOffset); // 4100
    console.log(views.data.buffer.byteLength); // 4104