Option with static functions
v2.0.0
Static
resolve all options to single ISome array result or INone
Array of IOptions or Functions return IOption
const test1 = (): ISome<string> => Some('hello');const test2 = (): ISome<number> => Some(10);const output: ISome<[string, number]> | INone = Option.all(test1, test2); Copy
const test1 = (): ISome<string> => Some('hello');const test2 = (): ISome<number> => Some(10);const output: ISome<[string, number]> | INone = Option.all(test1, test2);
resolve all possible async options to single ISome array result or INone
Array of async IOptions or Functions return IOption
const test1 = (): ISome<string> => Promise.resolve(Some('hello'));const test2 = (): ISome<number> => Promise.resolve(Some(10));const output: ISome<[string, number]> | INone = await Option.asyncAll(test1, test2); Copy
const test1 = (): ISome<string> => Promise.resolve(Some('hello'));const test2 = (): ISome<number> => Promise.resolve(Some(10));const output: ISome<[string, number]> | INone = await Option.asyncAll(test1, test2);
Get Option from any instance types (Some, None, JsonOption)
instance
from any value to Some(), nullish and NaN will return None()
value to wrap
const data: IOption<string> = Option.fromNullish(getStringValueOrNull()) // returns Some(value) or None() Copy
const data: IOption<string> = Option.fromNullish(getStringValueOrNull()) // returns Some(value) or None()
from Number to Some(), nullish and NaN will return None()
extends number
Option.fromNum(parseInt(stringNumber, 10)) // returns Some(value) or None() Copy
Option.fromNum(parseInt(stringNumber, 10)) // returns Some(value) or None()
Wrap Promise function to Promised Option function (try-catch)
function
const readFile = Options.wrapAsyncFn(fs.promises.readFile);const fileOption: IOption<string | Buffer> = await readFile('./some.file'); Copy
const readFile = Options.wrapAsyncFn(fs.promises.readFile);const fileOption: IOption<string | Buffer> = await readFile('./some.file');
Wrap function to Option function (try-catch)
const bufferFrom = Option.wrapFn(Buffer.from);const bufferOptionData: IOption<Buffer> = bufferFrom('test', 'utf-8'); Copy
const bufferFrom = Option.wrapFn(Buffer.from);const bufferOptionData: IOption<Buffer> = bufferFrom('test', 'utf-8');
Option with static functions
Since
v2.0.0