Hyperlinkv0.8.0-beta.28

Stream

Stream.decodeTextconsteffect/Stream.ts:9387
<
  Arg extends
    | Stream<Uint8Array, any, any>
    | { readonly encoding?: string | undefined }
    | undefined = { readonly encoding?: string | undefined }
>(
  streamOrOptions?: Arg,
  options?: { readonly encoding?: string | undefined } | undefined
): [Arg] extends [Stream<Uint8Array, infer _E, infer _R>]
  ? Stream<string, _E, _R>
  : <E, R>(self: Stream<Uint8Array, E, R>) => Stream<string, E, R>

Decodes Uint8Array chunks into strings using TextDecoder with an optional encoding.

Example (Decoding Uint8Array chunks into strings using TextDecoder with an optional encoding)

import { Console, Effect, Stream } from "effect"

const encoder = new TextEncoder()
const stream = Stream.make(
  encoder.encode("Hello"),
  encoder.encode(" World")
)

const program = Effect.gen(function*() {
  const decoded = yield* stream.pipe(
    Stream.decodeText,
    Stream.runCollect
  )
  yield* Console.log(decoded)
})

Effect.runPromise(program)
// ["Hello", " World"]
encoding
Source effect/Stream.ts:938722 lines
export const decodeText: <
  Arg extends Stream<Uint8Array, any, any> | {
    readonly encoding?: string | undefined
  } | undefined = {
    readonly encoding?: string | undefined
  }
>(
  streamOrOptions?: Arg,
  options?: {
    readonly encoding?: string | undefined
  } | undefined
) => [Arg] extends [Stream<Uint8Array, infer _E, infer _R>] ? Stream<string, _E, _R>
  : <E, R>(self: Stream<Uint8Array, E, R>) => Stream<string, E, R> = dual(
    (args) => isStream(args[0]),
    <E, R>(self: Stream<Uint8Array, E, R>, options?: {
      readonly encoding?: string | undefined
    }): Stream<string, E, R> =>
      suspend(() => {
        const decoder = new TextDecoder(options?.encoding)
        return map(self, (chunk) => decoder.decode(chunk, { stream: true }))
      })
  )