13.1.0 • Published 7 years ago
tsp-monads v13.1.0
Type safe Option and Result type
Inspired by Rust
NOTE: Works with TypeScript 2.1+ and JavaScript. strictNullChecks
option / flag is strongly recommended.
Documentation
Install
yarn add tsp-monads
Basic Usage
import { Option, Some, None } from 'tsp-monads'
const divide = (numerator: number, denominator: number): Option<number> => {
if (denominator === 0) {
return None
} else {
return Some(numerator / denominator)
}
};
// The return value of the function is an option
const result = divide(2.0, 3.0);
// Pattern match to retrieve the value
const message = result.match({
some: _ => `Result: ${_}`,
none: 'Cannot divide by 0',
});
console.log(message); // 'Result: 0.6666666666666666'
import { Result, Ok, Err } from 'tsp-monads'
function getIndex(values: string[], value: string): Result<number, string> {
const index = values.indexOf(value);
switch (index) {
case -1:
return Err('Value not found');
default:
return Ok(index);
}
}
console.log(getIndex(['a', 'b', 'c'], 'b')); // Ok(1)
console.log(getIndex(['a', 'b', 'c'], 'z')); // Err('Value not found')
...
Documentation
13.1.0
7 years ago
13.0.0
7 years ago
12.0.0
7 years ago
11.0.0
7 years ago
10.0.0
7 years ago
9.0.0
7 years ago
8.0.0
7 years ago
7.0.2
7 years ago
7.0.1
7 years ago
7.0.0
7 years ago
6.0.0
7 years ago
5.0.2
7 years ago
5.0.1
7 years ago
5.0.0
7 years ago
4.0.0
7 years ago
3.1.0
7 years ago
3.0.1
7 years ago
3.0.0
7 years ago
2.0.0
7 years ago
1.1.1
7 years ago
1.1.0
7 years ago
1.0.3
7 years ago
1.0.2
7 years ago
1.0.1
7 years ago
1.0.0
7 years ago