a buffer decoder/unpacker for a primitive value of a certain type (see PrimitiveType). this is the inverse of pack.

under the hood, this function calls one of the following primitive value decoders, based on the type that you provide:

import { assertEquals } from "jsr:@std/assert"

// aliasing for brevity
const u8 = Uint8Array, eq = assertEquals

eq(unpack(
"bool", // type of value to decode
u8.of(1, 0, 1), // buffer to decode from
1, // the index to begin decoding from
), [
false, // returned decoded value
1, // number of bytes traversed while decoding (i.e. byte size of the decoded object)
])

eq(unpack("cstr" , u8.of(97, 98, 99, 0, 100, 101), 0), ["abc" , 4])
eq(unpack("str" , u8.of(97, 98, 99, 0, 100, 101), 0), ["abc\x00de", 6])
eq(unpack("str" , u8.of(97, 98, 99, 0, 100, 101), 0, 4), ["abc\x00" , 4])
eq(unpack("bytes", u8.of(0, 1, 2, 3, 4, 5, 6), 2, 4), [u8.of(2, 3, 4, 5), 4])
eq(unpack("i2b[]", u8.of(0, 1, 2, 3, 4, 5), 2), [[0x0203, 0x0405] , 4])
eq(unpack("i2b[]", u8.of(0, 1, 2, 3, 4, 5), 0, 2), [[0x0001, 0x0203] , 4])