(str: string): Result.Result<Uint8Array, EncodingError>Decodes a base64 (RFC4648) string into bytes safely.
When to use
Use to decode a standard padded Base64 string 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 is not valid base64.
Example (Decoding Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64("SGVsbG8=")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]
}export const const decodeBase64: (
str: string
) => Result.Result<Uint8Array, EncodingError>
Decodes a base64 (RFC4648) string into bytes safely.
When to use
Use to decode a standard padded Base64 string 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 is not valid base64.
Example (Decoding Base64 bytes)
import { Encoding, Result } from "effect"
const result = Encoding.decodeBase64("SGVsbG8=")
if (Result.isSuccess(result)) {
console.log(Array.from(result.success)) // [72, 101, 108, 108, 111]
}
decodeBase64 = (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 stripped: stringstripped = const stripCrlf: (str: string) => stringstripCrlf(str: stringstr)
const const length: numberlength = const stripped: stringstripped.String.length: numberReturns the length of a String object.
length
if (const length: numberlength % 4 !== 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({
kind: stringkind: "Decode",
module: stringmodule: "Base64",
input: stringinput: const stripped: stringstripped,
message: stringmessage: `Length must be a multiple of 4, but is ${const length: numberlength}`
})
)
}
const const index: numberindex = const stripped: stringstripped.String.indexOf(searchString: string, position?: number): numberReturns the position of the first occurrence of a substring.
indexOf("=")
if (const index: numberindex !== -1 && ((const index: numberindex < const length: numberlength - 2) || (const index: numberindex === const length: numberlength - 2 && const stripped: stringstripped[const length: numberlength - 1] !== "="))) {
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({
kind: stringkind: "Decode",
module: stringmodule: "Base64",
input: stringinput: const stripped: stringstripped,
message: stringmessage: `Found a '=' character, but it is not at the end`
})
)
}
try {
const const missingOctets: 1 | 2 | 0missingOctets = const stripped: stringstripped.String.endsWith(searchString: string, endPosition?: number): booleanReturns true if the sequence of elements of searchString converted to a String is the
same as the corresponding elements of this object (converted to a String) starting at
endPosition – length(this). Otherwise returns false.
endsWith("==") ? 2 : const stripped: stringstripped.String.endsWith(searchString: string, endPosition?: number): booleanReturns true if the sequence of elements of searchString converted to a String is the
same as the corresponding elements of this object (converted to a String) starting at
endPosition – length(this). Otherwise returns false.
endsWith("=") ? 1 : 0
const const result: Uint8Array<ArrayBuffer>result = new var Uint8Array: Uint8ArrayConstructor
new (length: number) => Uint8Array<ArrayBuffer> (+6 overloads)
Uint8Array(3 * (const length: numberlength / 4) - const missingOctets: 1 | 2 | 0missingOctets)
for (let let i: numberi = 0, let j: numberj = 0; let i: numberi < const length: numberlength; let i: numberi += 4, let j: numberj += 3) {
const const buffer: numberbuffer = function getBase64Code(
charCode: number
): number
getBase64Code(const stripped: stringstripped.String.charCodeAt(index: number): numberReturns the Unicode value of the character at the specified location.
charCodeAt(let i: numberi)) << 18 |
function getBase64Code(
charCode: number
): number
getBase64Code(const stripped: stringstripped.String.charCodeAt(index: number): numberReturns the Unicode value of the character at the specified location.
charCodeAt(let i: numberi + 1)) << 12 |
function getBase64Code(
charCode: number
): number
getBase64Code(const stripped: stringstripped.String.charCodeAt(index: number): numberReturns the Unicode value of the character at the specified location.
charCodeAt(let i: numberi + 2)) << 6 |
function getBase64Code(
charCode: number
): number
getBase64Code(const stripped: stringstripped.String.charCodeAt(index: number): numberReturns the Unicode value of the character at the specified location.
charCodeAt(let i: numberi + 3))
const result: Uint8Array<ArrayBuffer>result[let j: numberj] = const buffer: numberbuffer >> 16
const result: Uint8Array<ArrayBuffer>result[let j: numberj + 1] = (const buffer: numberbuffer >> 8) & 0xff
const result: Uint8Array<ArrayBuffer>result[let j: numberj + 2] = const buffer: numberbuffer & 0xff
}
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({
kind: stringkind: "Decode",
module: stringmodule: "Base64",
input: stringinput: const stripped: stringstripped,
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"
})
)
}
}