#Function: unwrap()
unwrap<
D>(defaultValue): <O>(option) =>Unwrapped<O,D>
Extracts the value from an Option or OptionAsync. If the input is a None, the default value is returned.
#Type Parameters
#D
D
The default value type.
#Parameters
#defaultValue
D
#Returns
<O>(option) => Unwrapped<O, D>
#Examples
Some Case
import { import Option Option } from 'byenull';
const const value: number value = import Option Option .function pipe<Option.Option<number>, number>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number): 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 (2),
import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, Option.InferSome<O>>Extracts the value from an
Option
or
OptionAsync
.
If the input is a
None
, the default value is returned.
@typeParamD - The default value type.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish Default
import { Option } from 'byenull';
const value: number | null = Option.pipe(
Option.some(2),
Option.unwrap(null),
);
@seeorElse - For recovering with a fallback Option instead of a plain value.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryUnwraps unwrap (0),
);
// 2None Case
import { import Option Option } from 'byenull';
const const empty: Option.Option<number> empty : import Option Option .type Option<T> = Option.Some<T> | Option.NoneA union type representing either a
Some
or a
None
.
@typeParamT - The type of the Some value.@exampleimport { Option } from 'byenull';
const doSomething = (): Option.Option<number> => {
return Math.random() > 0.5
? Option.some(10)
: Option.none();
};
@categoryCore Types Option <number> = 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 ();
const const value: number value = import Option Option .function pipe<Option.Option<number>, number>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number): 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 (
const empty: Option.Option<number> empty ,
import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, Option.InferSome<O>>Extracts the value from an
Option
or
OptionAsync
.
If the input is a
None
, the default value is returned.
@typeParamD - The default value type.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish Default
import { Option } from 'byenull';
const value: number | null = Option.pipe(
Option.some(2),
Option.unwrap(null),
);
@seeorElse - For recovering with a fallback Option instead of a plain value.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryUnwraps unwrap (0),
);
// 0Nullish Default
import { import Option Option } from 'byenull';
const const value: number | null value : number | null = import Option Option .function pipe<Option.Option<number>, number | null>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number | null): number | null (+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 (2),
import Option Option .function unwrap<null>(defaultValue: null): <O>(option: O & ([null] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, null, Option.InferSome<O>>Extracts the value from an
Option
or
OptionAsync
.
If the input is a
None
, the default value is returned.
@typeParamD - The default value type.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish Default
import { Option } from 'byenull';
const value: number | null = Option.pipe(
Option.some(2),
Option.unwrap(null),
);
@seeorElse - For recovering with a fallback Option instead of a plain value.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryUnwraps unwrap (null),
);