Scala like Option type for TypeScript
npm install ts-optionOption type for TypeScript/JavaScript.
yarn
$ yarn add ts-optionnpm
$ npm install --save ts-option
`
$3
#### TypeScript
`
import {Option, option, some, none} from "ts-option";let a: Option = option(1); // Some(1)
let c: Option = some(2); // Some(2)
let b: Option = option(null); // None
let d: Option = none; // None
`#### JavaScript (ES Modules)
`
import {Option, option, some, none} from "ts-option";let a = option(1); // Some(1)
let c = some(2); // Some(2)
let b = option(null); // None
let d = none; // None
`
$3
#### option(value?: A | null): Option
Create an Option instance from a value.
It returns Some when the value is not null/undefined, otherwise returns None.####
some(value: A): Option
Create an Some instance from a value.
It returns Some even if the value is null or undefined.
If strict null checks are enabled in your tsconfig.json, undefined and null won't be permitted here.####
none: Option
The None type singleton object.####
option(...).exists(p: (_: A) => boolean): boolean
Returns true if the option is non-empty and the predicate p returns true when applied to the option's value.####
option(...).filter(p: (_: A) => boolean): Option
Returns the option if it is non-empty and applying the predicate p to the option's value returns true.####
option(...).filterNot(p: (_: A) => boolean): Option
Returns the option if it is non-empty and applying the predicate p to the option's value returns false.####
option(...).flatMap(f: (_: A) => Option): Option
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns None.####
option(...).fold(ifEmpty: () => B)(f: (_: A) => B): B
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns ifEmpty value.####
option(...).forAll(p: (_: A) => boolean): boolean
Tests whether a predicate holds for all elements of the option.####
option(...).forComprehension(...fns: (a: any) => Option
Performs a for-comprehension like operation using the given list of functions. For example:`
const nestedOptions = some({
anOption: some({
anotherOption: some({
finalValue: true
})
})
});const result = nestedOptions.forComprehension(
obj => obj.anOption,
anOption => anOption.anotherOption,
anotherOption => anotherOption.finalValue
);
console.log(
${result}) // Some(true)
`As with the Scala for comprehension the result of each function is flat-mapped with the next, except for the last, which is mapped.
Please note that there are currently some limitations:
1) Filtering must be done manually
2) There is no shared scope between functions
3) The result type is always
Option####
option(...).forEach(f: (_: A) => any): void
Apply the given procedure f to the option's value if it is non-empty, otherwise do nothing.####
option(...).get: A
Returns the option's value if the option is non-empty, otherwise throws an error.####
option(...).getOrElse(defaultValue: () => A): A
Returns the option's value if the option is non-empty, otherwise return the result of evaluating defaultValue.####
option(...).getOrElseValue(defaultValue: A): A
Returns the option's value if the option is non-empty, otherwise return the defaultValue.####
option(...).isDefined: boolean
Returns true if the option's value is non-empty, false otherwise.####
option(...).isEmpty: boolean
Returns true if the option is an instance of None, false otherwise.####
option(...).map(f: (_: A) => B): Option
Builds a new option by applying a function to all elements of this option.####
option(...).match(matcher: { some: (_: A) => B, none: () => B }): B
Scala's "Pattern Match" like signature. Returns the value that the function some returns if the option is non-empty,
otherwise returns the value that function none returns.`
let s: Option = some(9);
let n: Option = none;let a: number = s.match({
some: x => x * 9,
none: () => -1,
});
let b: number = n.match({
some: x => x * 9,
none: () => -1,
});
console.log(a); // 81
console.log(b); // -1
`####
option(...).nonEmpty: boolean
Returns true if the option is an instance of Some, false otherwise.####
option(...).orElse(alternative: () => Option): Option
Returns the option itself if it is non-empty, otherwise return the result of evaluating alternative.####
option(...).orElseValue(alternative: Option): Option
Returns the option itself if it is non-empty, otherwise return the alternative.####
option(...).orNull: A | null
Returns the option's value if it is non-empty, or null if it is empty.####
option(...).orUndefined: A | undefined
Returns the option's value if it is non-empty, or undefined if it is empty.####
option(...).toArray: Array`