a primitive value encoder/packer for a given type (see PrimitiveType). to invert/decode the transformation, use unpack.
type
under the hood, this function calls one of the following primitive value encoders, based on the type that you provide:
import { assertEquals } from "jsr:@std/assert"// aliasing for brevityconst u8 = Uint8Array, eq = assertEqualseq(pack("bool" , true), u8.of(1))eq(pack("cstr" , "hello"), u8.of(104, 101, 108, 108, 111, 0))eq(pack("str" , "hello"), u8.of(104, 101, 108, 108, 111))eq(pack("bytes", u8.of(1, 2, 3)), u8.of(1, 2, 3))// NOTE: if your device's native endian is not little-endian, then the test below will fail.eq(pack("f8l[]", [1.1, 2.2, 3**45.67]), new u8(Float64Array.of(1.1, 2.2, 3**45.67).buffer)) Copy
import { assertEquals } from "jsr:@std/assert"// aliasing for brevityconst u8 = Uint8Array, eq = assertEqualseq(pack("bool" , true), u8.of(1))eq(pack("cstr" , "hello"), u8.of(104, 101, 108, 108, 111, 0))eq(pack("str" , "hello"), u8.of(104, 101, 108, 108, 111))eq(pack("bytes", u8.of(1, 2, 3)), u8.of(1, 2, 3))// NOTE: if your device's native endian is not little-endian, then the test below will fail.eq(pack("f8l[]", [1.1, 2.2, 3**45.67]), new u8(Float64Array.of(1.1, 2.2, 3**45.67).buffer))
a primitive value encoder/packer for a given
type
(see PrimitiveType). to invert/decode the transformation, use unpack.under the hood, this function calls one of the following primitive value encoders, based on the
type
that you provide:Example