(str: string): Result.Result<Uint8Array, EncodingError>Decodes a hexadecimal string into bytes safely.
When to use
Use to decode hexadecimal text into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input has an odd length or
contains invalid hex characters.
Example (Decoding hex bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeHex("48656c6c6f")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]
}export const const decodeHex: (
str: string
) => Result.Result<Uint8Array, EncodingError>
Decodes a hexadecimal string into bytes safely.
When to use
Use to decode hexadecimal text into bytes without throwing on invalid input.
Details
Returns Result.succeed with a Uint8Array when decoding succeeds, or
Result.fail with an EncodingError when the input has an odd length or
contains invalid hex characters.
Example (Decoding hex bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeHex("48656c6c6f")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]
}
decodeHex = (str: stringstr: string): import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array, class EncodingErrorclass EncodingError {
name: string;
message: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
kind: 'Decode' | 'Encode';
module: string;
input: unknown;
}
Error returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError> => {
const const bytes: Uint8Array<ArrayBuffer>bytes = new var TextEncoder: new () => TextEncoderThe TextEncoder interface takes a stream of code points as input and emits a stream of UTF-8 bytes.
TextEncoder().TextEncoder.encode(input?: string): Uint8Array<ArrayBuffer>The TextEncoder.encode() method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object.
encode(str: stringstr)
if (const bytes: Uint8Array<ArrayBuffer>bytes.Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>.length: numberThe length of the array.
length % 2 !== 0) {
return import ResultResult.const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(
new constructor EncodingError(): EncodingErrorError returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError({
module: stringmodule: "Hex",
kind: stringkind: "Decode",
input: stringinput: str: stringstr,
message: stringmessage: `Length must be a multiple of 2, but is ${const bytes: Uint8Array<ArrayBuffer>bytes.Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>.length: numberThe length of the array.
length}`
})
)
}
try {
const const length: numberlength = const bytes: Uint8Array<ArrayBuffer>bytes.Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>.length: numberThe length of the array.
length / 2
const const result: Uint8Array<ArrayBuffer>result = new var Uint8Array: Uint8ArrayConstructor
new (length: number) => Uint8Array<ArrayBuffer> (+6 overloads)
Uint8Array(const length: numberlength)
for (let let i: numberi = 0; let i: numberi < const length: numberlength; let i: numberi++) {
const const a: numbera = const fromHexChar: (
byte: number
) => number
fromHexChar(const bytes: Uint8Array<ArrayBuffer>bytes[let i: numberi * 2])
const const b: numberb = const fromHexChar: (
byte: number
) => number
fromHexChar(const bytes: Uint8Array<ArrayBuffer>bytes[let i: numberi * 2 + 1])
const result: Uint8Array<ArrayBuffer>result[let i: numberi] = (const a: numbera << 4) | const b: numberb
}
return import ResultResult.const succeed: <A>(right: A) => Result<A>Creates a Result holding a Success value.
Details
- Use when you have a value and want to lift it into the
Result type
- The error type
E defaults to never
Example (Wrapping a value)
import { Result } from "effect"
const result = Result.succeed(42)
console.log(Result.isSuccess(result))
// Output: true
succeed(const result: Uint8Array<ArrayBuffer>result)
} catch (function (local var) e: unknowne) {
return import ResultResult.const fail: <E>(
left: E
) => Result<never, E>
Creates a Result holding a Failure value.
When to use
Use to represent a failed Result with a typed failure value.
Details
- The success type
A defaults to never
Example (Creating a failure)
import { Result } from "effect"
const result = Result.fail("Something went wrong")
console.log(Result.isFailure(result))
// Output: true
fail(
new constructor EncodingError(): EncodingErrorError returned when an encoding or decoding operation cannot process its
input.
When to use
Use when you need to handle or inspect failures from encoding or decoding
operations.
Details
The error records whether the failure happened during encoding or decoding,
which encoding module reported it, the original input, and a human-readable
message.
EncodingError({
module: stringmodule: "Hex",
kind: stringkind: "Decode",
input: stringinput: str: stringstr,
message: stringmessage: function (local var) e: unknowne instanceof var Error: ErrorConstructorError ? function (local var) e: Error(local var) e: {
name: string;
message: string;
stack: string;
cause: unknown;
}
e.Error.message: stringmessage : "Invalid input"
})
)
}
}