Scala like Option
type for TypeScript/JavaScript.
$ npm install --save ts-option
import {Option, option, some, none} from "ts-option";
let a: Option<number> = option(1); // Some(1)
let c: Option<number> = some(2); // Some(2)
let b: Option<number> = option(null); // None
let d: Option<number> = none; // None
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
Create an Option
instance from a value.
It returns Some<A>
when the value is not null/undefined, otherwise returns None
.
Create an Some
instance from a value.
It returns Some<A>
even if the value is null or undefined.
The None
type singleton object.
Returns true if the option is non-empty and the predicate p returns true when applied to the option's value.
Returns the option if it is non-empty and applying the predicate p to the option's value returns true.
Returns the option if it is non-empty and applying the predicate p to the option's value returns false.
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns None
.
Returns the result of applying f to the option's value if the option is non-empty, otherwise returns ifEmpty
value.
Tests whether a predicate holds for all elements of the option.
Apply the given procedure f to the option's value if it is non-empty, otherwise do nothing.
Returns the option's value if the option is non-empty, otherwise throws an error.
Returns the option's value if the option is non-empty, otherwise return the result of evaluating default.
Returns true if the option's value is non-empty, false otherwise.
Returns true if the option's value is empty, false otherwise.
Builds a new option by applying a function to all elements of this option.
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<number> = some(9);
let n: Option<number> = 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
Returns true if the option is an instance of Some
, false otherwise.
Returns the option itself if it is non-empty, otherwise return the result of evaluating alternative.
Returns the option's value if it is non-empty, or null if it is empty.
Converts the option to an array.
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
);
// result = option(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:
- Filtering must be done manually
- There is no shared scope between functions
- The result type is always
Option<any>
MIT