MCPcopy Create free account
hub / github.com/Effect-TS/effect / decodeString

Function decodeString

packages/effect/src/encoding/JsonArray.ts:592–635  ·  view source on GitHub ↗
(options?: DecodeOptions)

Source from the content-addressed store, hash-verified

590 * @since 4.0.6
591 */
592export const decodeString = <IE = never, Done = unknown>(options?: DecodeOptions): Channel.Channel<
593 Arr.NonEmptyReadonlyArray<unknown>,
594 IE | JsonArrayError,
595 Done,
596 Arr.NonEmptyReadonlyArray<string>,
597 IE,
598 Done
599> => {
600 const config = makeConfig(options)
601 return Channel.fromTransform((upstream, _scope) =>
602 Effect.sync(() => {
603 const scanner = new Scanner(config)
604 let done: Pull.Pull<never, never, Done> | undefined
605 type Out = Pull.Pull<Arr.NonEmptyReadonlyArray<unknown>, IE | JsonArrayError, Done>
606
607 const emit = (run: (out: Array<unknown>) => void): Out => {
608 const out: Array<unknown> = []
609 try {
610 run(out)
611 } catch (error) {
612 if (isJsonArrayError(error)) return Effect.fail(error)
613 throw error
614 }
615 return Arr.isArrayNonEmpty(out) ? Effect.succeed(out) : pull
616 }
617
618 const pull: Out = Effect.suspend(() => {
619 if (done !== undefined) return done
620 return Pull.matchEffect(upstream, {
621 onSuccess: (texts) =>
622 emit((out) => {
623 for (const text of texts) scanner.feed(text, out)
624 }),
625 onFailure: Effect.failCause,
626 onDone: (value) => {
627 done = Cause.done(value)
628 return emit(() => scanner.end())
629 }
630 })
631 })
632 return pull
633 })
634 )
635}
636
637/**
638 * Creates a channel that parses UTF-8 bytes of one JSON document and emits the

Callers 2

decodeFunction · 0.70
decodeSchemaStringFunction · 0.70

Calls 6

feedMethod · 0.95
endMethod · 0.95
makeConfigFunction · 0.70
emitFunction · 0.70
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected