#Inspecting options
When a pipeline doesn't behave as expected, you want to see the value at each step. inspect and inspectNone let you run a side effect, such as logging, without affecting the Option that flows through the pipeline.
#Observing values with inspect
For a Some, inspect calls the given function with the inner value, ignores its result, and returns the original Option unchanged. For a None, the function is not called:
import { import Option Option } from "byenull";
const const found: Option.Option<number> found = import Option Option .function pipe<Option.Option<number>, Option.Option<number>, Option.Option<number>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Option.Option<number>, bc: (b: Option.Option<number>) => Option.Option<number>): Option.Option<number> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function some<number>(value: number): Option.Option<number>Creates a
Some
from a given value.
Passing null or undefined returns
None
instead.
@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (42),
import Option Option .function inspect<Option.Option<number>, void>(fn: (a: number) => void): (option: Option.Option<number>) => Option.Option<number> (+1 overload)Executes a side effect function on the value of a
Some
in an
Option
or
OptionAsync
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
None
, the provided function is never called and the option is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamR - The return type of the inspection function.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Option: { type: 'Some', value: 42 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspect((value: number) => console.log('Debug:', value)),
);
// No console output
// Option: { type: 'None' }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.some(42),
Option.inspect(async (value) => {
await fetch(`/api/logs?value=${value}`);
}),
);
// { type: 'Some', value: 42 }
@seeinspectNone - For inspecting a None instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: number value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ("Value:", value: number value )),
import Option Option .function map<Option.Option<number>, number>(fn: (a: number) => number): (option: Option.Option<number>) => Option.Option<number> (+1 overload)Applies a transformation function to the value of an
Option
or
OptionAsync
.
If the input is a
None
, it is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@exampleNone Case (unchanged)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators map ((value: number value ) => value: number value * 2),
);
// Console: "Value: 42"
// { type: 'Some', value: 84 }
const const missing: Option.Option<never> missing = import Option Option .function pipe<Option.Option<never>, Option.Option<never>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Option.Option<never>): Option.Option<never> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function none(): Option.Option<never>Creates a
None
: an Option that holds no value.
@returnsA None.@exampleimport { Option } from 'byenull';
const option = Option.none();
// { type: 'None' }
@categoryCreators none (),
import Option Option .function inspect<Option.Option<never>, void>(fn: (a: never) => void): (option: Option.Option<never>) => Option.Option<never> (+1 overload)Executes a side effect function on the value of a
Some
in an
Option
or
OptionAsync
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
None
, the provided function is never called and the option is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamR - The return type of the inspection function.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Option: { type: 'Some', value: 42 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspect((value: number) => console.log('Debug:', value)),
);
// No console output
// Option: { type: 'None' }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.some(42),
Option.inspect(async (value) => {
await fetch(`/api/logs?value=${value}`);
}),
);
// { type: 'Some', value: 42 }
@seeinspectNone - For inspecting a None instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: number value : number) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ("Value:", value: number value )), // Not called
);
// { type: 'None' }Unlike map, the callback does not produce the next pipeline value. It must return void or a Promise<void>, so the type checker keeps inspect focused on side effects.
#Observing None with inspectNone
inspectNone is the counterpart of inspect for missing values. It calls the given function for a None and does nothing for a Some. The callback takes no parameters, because a None holds no value:
import { import Option Option } from "byenull";
const const missing: Option.Option<never> missing = import Option Option .function pipe<Option.Option<never>, Option.Option<never>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Option.Option<never>): Option.Option<never> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function none(): Option.Option<never>Creates a
None
: an Option that holds no value.
@returnsA None.@exampleimport { Option } from 'byenull';
const option = Option.none();
// { type: 'None' }
@categoryCreators none (),
import Option Option .function inspectNone<() => void>(fn: () => void): <O>(option: O) => Option.OptionFor<void | O, Option.InferSome<O>>Executes a side effect function when an
Option
or
OptionAsync
is a
None
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
Some
, the provided function is never called and the option is returned unchanged.
@typeParamF - The side effect function to execute on a None.@typeParamO - The input Option or OptionAsync.@exampleNone Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// Console output: "Debug: No value present"
// Option: { type: 'None' }
@exampleSome Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// No console output
// Option: { type: 'Some', value: 42 }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.none(),
Option.inspectNone(async () => {
await fetch('/api/logs?value=missing');
}),
);
// { type: 'None' }
@seeinspect - For inspecting a Some instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectNone (() => var console: Console console .Console.warn(...data: any[]): voidThe console.warn() static method outputs a warning message to the console at the "warning" log level. The message is only displayed to the user if the console is configured to display warning output. In most cases, the log level is configured within the console UI. The message may receive special formatting, such as yellow colors and a warning icon.
warn ("No value present")),
);
// Console: "No value present"
// { type: 'None' }
const const found: Option.Option<number> found = import Option Option .function pipe<Option.Option<number>, Option.Option<number>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Option.Option<number>): Option.Option<number> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Option Option .function some<number>(value: number): Option.Option<number>Creates a
Some
from a given value.
Passing null or undefined returns
None
instead.
@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (42),
import Option Option .function inspectNone<() => void>(fn: () => void): <O>(option: O) => Option.OptionFor<void | O, Option.InferSome<O>>Executes a side effect function when an
Option
or
OptionAsync
is a
None
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
Some
, the provided function is never called and the option is returned unchanged.
@typeParamF - The side effect function to execute on a None.@typeParamO - The input Option or OptionAsync.@exampleNone Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// Console output: "Debug: No value present"
// Option: { type: 'None' }
@exampleSome Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// No console output
// Option: { type: 'Some', value: 42 }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.none(),
Option.inspectNone(async () => {
await fetch('/api/logs?value=missing');
}),
);
// { type: 'None' }
@seeinspect - For inspecting a Some instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectNone (() => var console: Console console .Console.warn(...data: any[]): voidThe console.warn() static method outputs a warning message to the console at the "warning" log level. The message is only displayed to the user if the console is configured to display warning output. In most cases, the log level is configured within the console UI. The message may receive special formatting, such as yellow colors and a warning icon.
warn ("No value present")), // Not called
);
// { type: 'Some', value: 42 }#Tracing a pipeline
Insert inspect between the steps you want to observe, and inspectNone at the end to log when a lookup came up empty:
import { import Option Option } from "byenull";
type type User = {
id: string;
name: string;
avatarId: string;
}
User = { id: string id : string; name: string name : string; avatarId: string avatarId : string };
type type Avatar = {
id: string;
url: string;
}
Avatar = { id: string id : string; url: string url : string };
const const users: Map<string, User> users = new var Map: MapConstructor
new <string, User>(iterable?: Iterable<readonly [string, User]> | null | undefined) => Map<string, User> (+3 overloads)
Map <string, type User = {
id: string;
name: string;
avatarId: string;
}
User >([
["u1", { id: string id : "u1", name: string name : "Alice", avatarId: string avatarId : "a1" }],
["u2", { id: string id : "u2", name: string name : "Bob", avatarId: string avatarId : "a2" }],
]);
const const avatars: Map<string, Avatar> avatars = new var Map: MapConstructor
new <string, Avatar>(iterable?: Iterable<readonly [string, Avatar]> | null | undefined) => Map<string, Avatar> (+3 overloads)
Map <string, type Avatar = {
id: string;
url: string;
}
Avatar >([
["a1", { id: string id : "a1", url: string url : "https://cdn.example.com/a1.png" }],
]);
const const findUser: (id: string) => Option.Option<User> findUser = import Option Option .function fn<(id: string) => User | undefined>(fn: (id: string) => User | undefined): (id: string) => Option.Option<User> (+1 overload)Wraps a function and returns a new function that returns an
Option
or
OptionAsync
.
Nullish values and Promise rejections produce
None
; synchronous throws are not captured.
@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@exampleSynchronous Function
import { Option } from 'byenull';
type User = { id: string; name: string };
const findUser = Option.fn((id: string, users: User[]) =>
users.find((user) => user.id === id),
);
findUser('u1', [{ id: 'u1', name: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@exampleAsynchronous Function
import { Option } from 'byenull';
type User = { id: string; name: string };
const fetchUser = Option.fn(async (id: string) => {
const response = await fetch(`/api/users/${id}`);
return response.json() as Promise<User>;
});
await fetchUser('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators fn ((id: string id : string) => const users: Map<string, User> users .Map<string, User>.get(key: string): User | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
@returnsReturns the element associated with the specified key. If no element is associated with the specified key, undefined is returned. get (id: string id ));
const const findAvatar: (avatarId: string) => Option.Option<Avatar> findAvatar = import Option Option .function fn<(avatarId: string) => Avatar | undefined>(fn: (avatarId: string) => Avatar | undefined): (avatarId: string) => Option.Option<Avatar> (+1 overload)Wraps a function and returns a new function that returns an
Option
or
OptionAsync
.
Nullish values and Promise rejections produce
None
; synchronous throws are not captured.
@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@exampleSynchronous Function
import { Option } from 'byenull';
type User = { id: string; name: string };
const findUser = Option.fn((id: string, users: User[]) =>
users.find((user) => user.id === id),
);
findUser('u1', [{ id: 'u1', name: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@exampleAsynchronous Function
import { Option } from 'byenull';
type User = { id: string; name: string };
const fetchUser = Option.fn(async (id: string) => {
const response = await fetch(`/api/users/${id}`);
return response.json() as Promise<User>;
});
await fetchUser('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators fn ((avatarId: string avatarId : string) => const avatars: Map<string, Avatar> avatars .Map<string, Avatar>.get(key: string): Avatar | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
@returnsReturns the element associated with the specified key. If no element is associated with the specified key, undefined is returned. get (avatarId: string avatarId ));
const const avatarUrl: Option.Option<string> avatarUrl = import Option Option .function pipe<Option.Option<User>, Option.Option<User>, Option.Option<Avatar>, Option.Option<string>, Option.Option<string>>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.Option<Avatar>, cd: (c: Option.Option<Avatar>) => Option.Option<string>, de: (d: Option.Option<string>) => Option.Option<string>): Option.Option<...> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const findUser: (id: string) => Option.Option<User> findUser ("u1"),
import Option Option .function inspect<Option.Option<User>, void>(fn: (a: User) => void): (option: Option.Option<User>) => Option.Option<User> (+1 overload)Executes a side effect function on the value of a
Some
in an
Option
or
OptionAsync
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
None
, the provided function is never called and the option is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamR - The return type of the inspection function.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Option: { type: 'Some', value: 42 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspect((value: number) => console.log('Debug:', value)),
);
// No console output
// Option: { type: 'None' }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.some(42),
Option.inspect(async (value) => {
await fetch(`/api/logs?value=${value}`);
}),
);
// { type: 'Some', value: 42 }
@seeinspectNone - For inspecting a None instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((user: User user ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ("Found user:", user: User user .name: string name )),
import Option Option .function andThen<(user: any) => Option.Option<Avatar>>(fn: (user: any) => Option.Option<Avatar>): <O1>(option: O1) => Option.OptionFor<Option.Option<Avatar> | O1, Avatar> (+1 overload)Chains the next computation using the value of an
Option
or
OptionAsync
.
If the input is a
None
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamF - The function type to chain.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.andThen((x: number) => Option.some(x * 2)),
);
// { type: 'None' }
@exampleAsynchronous Case
import { Option } from 'byenull';
type User = { id: string; name: string };
const fetchUser = Option.fn(async (id: string) =>
fetch(`/api/users/${id}`).then((response) => response.json() as Promise<User>),
);
const option = await Option.pipe(
Option.some('u123'),
Option.andThen(fetchUser),
);
// { type: 'Some', value: User }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThen ((user: any user ) => const findAvatar: (avatarId: string) => Option.Option<Avatar> findAvatar (user: any user .avatarId)),
import Option Option .function map<Option.Option<Avatar>, string>(fn: (a: Avatar) => string): (option: Option.Option<Avatar>) => Option.Option<string> (+1 overload)Applies a transformation function to the value of an
Option
or
OptionAsync
.
If the input is a
None
, it is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@exampleNone Case (unchanged)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators map ((avatar: Avatar avatar ) => avatar: Avatar avatar .url: string url ),
import Option Option .function inspectNone<() => void>(fn: () => void): <O>(option: O) => Option.OptionFor<void | O, Option.InferSome<O>>Executes a side effect function when an
Option
or
OptionAsync
is a
None
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
Some
, the provided function is never called and the option is returned unchanged.
@typeParamF - The side effect function to execute on a None.@typeParamO - The input Option or OptionAsync.@exampleNone Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// Console output: "Debug: No value present"
// Option: { type: 'None' }
@exampleSome Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// No console output
// Option: { type: 'Some', value: 42 }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.none(),
Option.inspectNone(async () => {
await fetch('/api/logs?value=missing');
}),
);
// { type: 'None' }
@seeinspect - For inspecting a Some instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectNone (() => var console: Console console .Console.warn(...data: any[]): voidThe console.warn() static method outputs a warning message to the console at the "warning" log level. The message is only displayed to the user if the console is configured to display warning output. In most cases, the log level is configured within the console UI. The message may receive special formatting, such as yellow colors and a warning icon.
warn ("No user or avatar found")),
);
// Console: "Found user: Alice"
// { type: 'Some', value: 'https://cdn.example.com/a1.png' }
const const missing: Option.Option<string> missing = import Option Option .function pipe<Option.Option<User>, Option.Option<User>, Option.Option<Avatar>, Option.Option<string>, Option.Option<string>>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.Option<Avatar>, cd: (c: Option.Option<Avatar>) => Option.Option<string>, de: (d: Option.Option<string>) => Option.Option<string>): Option.Option<...> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 25 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const findUser: (id: string) => Option.Option<User> findUser ("u2"),
import Option Option .function inspect<Option.Option<User>, void>(fn: (a: User) => void): (option: Option.Option<User>) => Option.Option<User> (+1 overload)Executes a side effect function on the value of a
Some
in an
Option
or
OptionAsync
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
None
, the provided function is never called and the option is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamR - The return type of the inspection function.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Option: { type: 'Some', value: 42 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspect((value: number) => console.log('Debug:', value)),
);
// No console output
// Option: { type: 'None' }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.some(42),
Option.inspect(async (value) => {
await fetch(`/api/logs?value=${value}`);
}),
);
// { type: 'Some', value: 42 }
@seeinspectNone - For inspecting a None instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((user: User user ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ("Found user:", user: User user .name: string name )),
import Option Option .function andThen<(user: any) => Option.Option<Avatar>>(fn: (user: any) => Option.Option<Avatar>): <O1>(option: O1) => Option.OptionFor<Option.Option<Avatar> | O1, Avatar> (+1 overload)Chains the next computation using the value of an
Option
or
OptionAsync
.
If the input is a
None
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamF - The function type to chain.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@exampleNone Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.andThen((x: number) => Option.some(x * 2)),
);
// { type: 'None' }
@exampleAsynchronous Case
import { Option } from 'byenull';
type User = { id: string; name: string };
const fetchUser = Option.fn(async (id: string) =>
fetch(`/api/users/${id}`).then((response) => response.json() as Promise<User>),
);
const option = await Option.pipe(
Option.some('u123'),
Option.andThen(fetchUser),
);
// { type: 'Some', value: User }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThen ((user: any user ) => const findAvatar: (avatarId: string) => Option.Option<Avatar> findAvatar (user: any user .avatarId)),
import Option Option .function map<Option.Option<Avatar>, string>(fn: (a: Avatar) => string): (option: Option.Option<Avatar>) => Option.Option<string> (+1 overload)Applies a transformation function to the value of an
Option
or
OptionAsync
.
If the input is a
None
, it is returned unchanged.
@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@exampleNone Case (unchanged)
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators map ((avatar: Avatar avatar ) => avatar: Avatar avatar .url: string url ),
import Option Option .function inspectNone<() => void>(fn: () => void): <O>(option: O) => Option.OptionFor<void | O, Option.InferSome<O>>Executes a side effect function when an
Option
or
OptionAsync
is a
None
,
without modifying the original option. This is useful for debugging, logging, or performing
other side effects while maintaining the original value.
If the input is a
Some
, the provided function is never called and the option is returned unchanged.
@typeParamF - The side effect function to execute on a None.@typeParamO - The input Option or OptionAsync.@exampleNone Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.none(),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// Console output: "Debug: No value present"
// Option: { type: 'None' }
@exampleSome Case (fn is never called)
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(42),
Option.inspectNone(() => console.log('Debug: No value present')),
);
// No console output
// Option: { type: 'Some', value: 42 }
@exampleAsynchronous Side Effect
import { Option } from 'byenull';
const option = await Option.pipe(
Option.none(),
Option.inspectNone(async () => {
await fetch('/api/logs?value=missing');
}),
);
// { type: 'None' }
@seeinspect - For inspecting a Some instead.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectNone (() => var console: Console console .Console.warn(...data: any[]): voidThe console.warn() static method outputs a warning message to the console at the "warning" log level. The message is only displayed to the user if the console is configured to display warning output. In most cases, the log level is configured within the console UI. The message may receive special formatting, such as yellow colors and a warning icon.
warn ("No user or avatar found")),
);
// Console: "Found user: Bob"
// Console: "No user or avatar found"
// { type: 'None' }The second pipeline shows both callbacks in their natural roles: the user is found, so inspect logs the name, and the missing avatar leaves the pipeline at None, where inspectNone reports it.
#Things to keep in mind
- The callback result is ignored. Whatever the callback does, the original
Optionis passed on. If a step should influence the pipeline, useandThenwith a callback that returns an Option instead. - Async callbacks are awaited. A callback that returns a
Promisemakes the step asynchronous: the pipeline waits for the side effect before continuing, and the step widens the pipeline type like any other async step. See Mixing sync and async. - Not only for debugging. Placing
inspectandinspectNoneat the end of a pipeline is a simple way to act on the final outcome, such as printing the resolved value or logging that nothing was found.
#References
| API | Purpose |
|---|---|
| inspect(fn) | Run a side effect on the Some value |
| inspectNone(fn) | Run a side effect on a None |