#Namespace: Option
Re-exports core Option-handling utilities under two convenient namespaces:
Option: Verbose and explicit usageO: Shorthand alias for more concise code
#Examples
Basic Usage
import { import Option Option } from 'byenull';
const const findUser: (id: string) => Option.Option<{
id: string;
name: string;
}>
findUser = import Option Option .function fn<(id: string) => {
id: string;
name: string;
} | null>(fn: (id: string) => {
id: string;
name: string;
} | null): (id: string) => Option.Option<{
id: string;
name: string;
}> (+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) =>
id: string id .String.startsWith(searchString: string, position?: 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
position. Otherwise returns false.
startsWith ('u') ? { id: string id , name: string name : 'John Doe' } : null,
);
const const option: Option.Option<string> option = import Option Option .function pipe<Option.Option<string>, Option.Option<{
id: string;
name: string;
}>, Option.Option<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<{
id: string;
name: string;
}>, bc: (b: Option.Option<{
id: string;
name: string;
}>) => Option.Option<string>): Option.Option<string> (+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<string>(value: string): Option.Option<string>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 ('u123'),
import Option Option .function andThen<(id: string) => Option.Option<{
id: string;
name: string;
}>>(fn: (id: string) => Option.Option<{
id: string;
name: string;
}>): <O1>(option: O1) => Option.OptionFor<Option.Option<{
id: string;
name: string;
}> | O1, {
id: string;
name: string;
}> (+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 (const findUser: (id: string) => Option.Option<{
id: string;
name: string;
}>
findUser ),
import Option Option .function map<Option.Option<{
id: string;
name: string;
}>, string>(fn: (a: {
id: string;
name: string;
}) => string): (option: Option.Option<{
id: string;
name: string;
}>) => 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 ((user: {
id: string;
name: string;
}
user ) => user: {
id: string;
name: string;
}
user .name: string name ),
);
if (import Option Option .function isSome<Option.Option<string>>(option: Option.Option<string>): option is Option.Some<string>Type guard to check if an
Option
is a
Some
.
@typeParamO - The Option type to check.@paramoption - The Option to check.@returnstrue if the option is a Some, otherwise false.@exampleimport { Option } from 'byenull';
declare const option: Option.Option<number>;
if (Option.isSome(option)) {
console.log(option.value); // number
}
@categoryType Guards isSome (const option: Option.Option<string> option )) {
var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log (const option: Option.Some<string> option .value: string value ); // John Doe
}Shorthand Usage
import { import O O } from 'byenull';
const const findUser: (id: string) => O.Option<{
id: string;
name: string;
}>
findUser = import O O .function fn<(id: string) => {
id: string;
name: string;
} | null>(fn: (id: string) => {
id: string;
name: string;
} | null): (id: string) => O.Option<{
id: string;
name: string;
}> (+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) =>
id: string id .String.startsWith(searchString: string, position?: 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
position. Otherwise returns false.
startsWith ('u') ? { id: string id , name: string name : 'John Doe' } : null,
);
const const option: O.Option<string> option = import O O .function pipe<O.Option<string>, O.Option<{
id: string;
name: string;
}>, O.Option<string>>(a: O.Option<string>, ab: (a: O.Option<string>) => O.Option<{
id: string;
name: string;
}>, bc: (b: O.Option<{
id: string;
name: string;
}>) => O.Option<string>): O.Option<string> (+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 O O .function some<string>(value: string): O.Option<string>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 ('u123'),
import O O .function andThen<(id: string) => O.Option<{
id: string;
name: string;
}>>(fn: (id: string) => O.Option<{
id: string;
name: string;
}>): <O1>(option: O1) => O.OptionFor<O.Option<{
id: string;
name: string;
}> | O1, {
id: string;
name: string;
}> (+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 (const findUser: (id: string) => O.Option<{
id: string;
name: string;
}>
findUser ),
import O O .function map<O.Option<{
id: string;
name: string;
}>, string>(fn: (a: {
id: string;
name: string;
}) => string): (option: O.Option<{
id: string;
name: string;
}>) => O.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 ((user: {
id: string;
name: string;
}
user ) => user: {
id: string;
name: string;
}
user .name: string name ),
);
if (import O O .function isSome<O.Option<string>>(option: O.Option<string>): option is O.Some<string>Type guard to check if an
Option
is a
Some
.
@typeParamO - The Option type to check.@paramoption - The Option to check.@returnstrue if the option is a Some, otherwise false.@exampleimport { Option } from 'byenull';
declare const option: Option.Option<number>;
if (Option.isSome(option)) {
console.log(option.value); // number
}
@categoryType Guards isSome (const option: O.Option<string> option )) {
var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log (const option: O.Some<string> option .value: string value ); // John Doe
}#Core Types
| Type Alias | Description |
|---|---|
| None | The variant of an Option that holds no value. |
| NonNullableValue | A value that is neither null nor undefined. |
| Option | A union type representing either a Some or a None. |
| OptionAsync | An asynchronous variant of Option, wrapped in a Promise. |
| OptionMaybeAsync | An option that may be either synchronous or asynchronous. |
| Some | The variant of an Option that holds a value. |
#Infer Types
| Type Alias | Description |
|---|---|
| InferSome | Infers the Some value type T from an Option or a function returning an Option. |
| OptionFor | Resolves to the appropriate Option type (sync or async) based on the input type. |
#Creators
| Function | Description |
|---|---|
| fn | Wraps a function and returns a new function that returns an Option or OptionAsync. |
| maybe | Executes a function and wraps its result in an Option or OptionAsync. |
| none | Creates a None: an Option that holds no value. |
| some | Creates a Some from a given value. Passing null or undefined returns None instead. |
#Combinators
| Function | Description |
|---|---|
| andThen | 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. |
| inspect | 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. |
| inspectNone | 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. |
| map | Applies a transformation function to the value of an Option or OptionAsync. If the input is a None, it is returned unchanged. |
| orElse | Chains a fallback computation for an Option or OptionAsync. If the input is a Some, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is. |
#Unwraps
| Function | Description |
|---|---|
| unwrap | Extracts the value from an Option or OptionAsync. If the input is a None, the default value is returned. |
#Type Guards
| Function | Description |
|---|---|
| isNone | Type guard to check if an Option is a None. |
| isOption | Type guard to check if a value is an Option. |
| isSome | Type guard to check if an Option is a Some. |
#Interop
| Function | Description |
|---|---|
| fromResult | Converts a Result from @praha/byethrow into an Option or OptionAsync. A Success becomes a Some; a Failure becomes a None, discarding the error. |
| toResult | Converts an Option or OptionAsync into a Result from @praha/byethrow. A Some becomes a Success; a None becomes a Failure containing err. |
#Utilities
| Function | Description |
|---|---|
| pipe | Applies a sequence of functions to a value, from left to right. |