function toBytes
toBytes(iter: AsyncIterable<StandardData>): AsyncIterable<Uint8Array>

Convert strings, string arrays, or byte arrays to Uint8Array chunks.

Conversion rules:

  • string: Converted to UTF-8 bytes with trailing newline
  • string[]: Each string converted to UTF-8 with newline, concatenated
  • Uint8Array: Passed through unchanged
  • Uint8Array[]: Concatenated into single array

Strings are always treated as lines (newline added). Bytes are treated as binary data.

Examples

Convert string to bytes

import { enumerate, toBytes } from "jsr:@j50n/proc";

const bytes = await enumerate(["hello"])
  .transform(toBytes)
  .collect();
// Uint8Array with "hello\n"

Convert string array to bytes

import { enumerate, toBytes } from "jsr:@j50n/proc";

const bytes = await enumerate([["line1", "line2"]])
  .transform(toBytes)
  .collect();
// Uint8Array with "line1\nline2\n"

Parameters

iter: AsyncIterable<StandardData>

The iterable to convert.

Return Type

AsyncIterable<Uint8Array>

An AsyncIterable of Uint8Array chunks.