@sweet-monads/maybe
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@sweet-monads/interfaces": "^1.1.0" | ||
"@sweet-monads/interfaces": "^1.1.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "author": "", |
@@ -38,6 +38,10 @@ # @sweet-monads/maybe | ||
- [`Maybe.just`](#maybejust) | ||
- [`Maybe.from`](#maybefrom) | ||
- [`Maybe#isNone`](#maybeisnone) | ||
- [`Maybe#isJust`](#maybeisjust) | ||
- [`Maybe#join`](#maybejoin) | ||
- [`Maybe#map`](#maybemap) | ||
- [`Maybe#asyncMap`](#maybeasyncmap) | ||
- [`Maybe#chain`](#maybechain) | ||
- [`Maybe#asyncChain`](#maybeasyncchain) | ||
- [Helpers](#helpers) | ||
@@ -87,2 +91,16 @@ | ||
#### `Maybe.from` | ||
The same as [`Maybe.just`](#maybejust) | ||
```typescript | ||
function from<T>(value: T): Maybe<T>; | ||
``` | ||
- Returns `Maybe` with `Just` state which contain value with `T` type. | ||
Example: | ||
```typescript | ||
const v1 = Maybe.from(2); // Maybe<number>.Just | ||
const v2 = Maybe.from<2>(2); // Maybe<2>.Just | ||
``` | ||
#### `Maybe#isNone` | ||
@@ -116,2 +134,19 @@ ```typescript | ||
#### `Maybe#join` | ||
```typescript | ||
function join<V>(this: Maybe<Maybe<V>>): Maybe<V>; | ||
``` | ||
- `this: Maybe<Maybe<V>>` - `Maybe` instance which contains other `Maybe` instance as `Just` value. | ||
- Returns unwrapped `Maybe` - if current `Maybe` has `Just` state and inner `Maybe` has `Just` state then returns inner `Maybe` `Just`, otherwise returns `Maybe` `None`. | ||
Example: | ||
```typescript | ||
const v1 = Maybe.just(Maybe.just(2)); | ||
const v2 = Maybe.just(Maybe.none()); | ||
const v3 = Maybe.none<Maybe<number>>(); | ||
v1.join() // Maybe.Just with value 2 | ||
v2.join() // Maybe.None without value | ||
v3.join() // Maybe.None without value | ||
``` | ||
#### `Maybe#map` | ||
@@ -131,2 +166,18 @@ ```typescript | ||
##### `Maybe#asyncMap` | ||
```typescript | ||
function asyncMap<Val, NewVal>(fn: (val: Val) => Promise<NewVal>): Promise<Maybe<NewVal>>; | ||
``` | ||
- Returns `Promise` with mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
// Promise<Maybe<string>.Just> with value "2" | ||
const newVal1 = v1.asyncMap(a => Promise.resolve(a.toString())); | ||
// Promise<Maybe<string>.None> without value | ||
const newVal2 = v2.asyncMap(a => Promise.resolve(a.toString())); | ||
``` | ||
#### `Maybe#chain` | ||
@@ -148,2 +199,21 @@ ```typescript | ||
##### `Maybe#asyncChain` | ||
```typescript | ||
function asyncChain<Val, NewVal>(fn: (val: Val) => Promise<Maybe<NewVal>>): Promise<Maybe<NewVal>>; | ||
``` | ||
- Returns `Promise` with mapped by `fn` function value wrapped by `Maybe` if `Maybe` is `Just` otherwise `None` | ||
Example: | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
// Promise<Maybe<string>>.Just with value "2" | ||
const newVal1 = v1.asyncChain(a => Promise.resolve(Maybe.just(a.toString()))); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal2 = v1.asyncChain(a => Promise.resolve(Maybe.none())); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal3 = v2.asyncChain(a => Promise.resolve(Maybe.just(a.toString()))); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal4 = v2.asyncChain(a => Promise.resolve(Maybe.none())); | ||
``` | ||
#### Helpers | ||
@@ -150,0 +220,0 @@ |
11632
224