@sweet-monads/maybe
Advanced tools
Comparing version 1.3.2 to 2.0.0
import { Monad } from "@sweet-monads/interfaces"; | ||
export default class Maybe<T> implements Monad<T> { | ||
private readonly state; | ||
readonly value?: T | undefined; | ||
declare const enum MaybeState { | ||
Just = "Just", | ||
None = "None" | ||
} | ||
export default class MaybeConstructor<T, S extends MaybeState = MaybeState> implements Monad<T> { | ||
private readonly type; | ||
readonly value: S extends MaybeState.Just ? T : undefined; | ||
static merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>; | ||
@@ -15,2 +19,3 @@ static merge<V1, V2>(values: [Maybe<V1>, Maybe<V2>]): Maybe<[V1, V2]>; | ||
static merge<V1, V2, V3, V4, V5, V6, V7, V8, V9, L10, V10>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>, Maybe<V4>, Maybe<V5>, Maybe<V6>, Maybe<V7>, Maybe<V8>, Maybe<V9>, Maybe<V10>]): Maybe<[V1, V2, V3, V4, V5, V6, V7, V8, V9, V10]>; | ||
static merge<T>(maybies: Array<Maybe<T>>): Maybe<T[]>; | ||
static from<T>(v: T): Maybe<T>; | ||
@@ -20,5 +25,4 @@ static none<T>(): Maybe<T>; | ||
private constructor(); | ||
private constructor(); | ||
isNone(): boolean; | ||
isJust(): boolean; | ||
isNone(): this is MaybeConstructor<T, MaybeState.None>; | ||
isJust(): this is MaybeConstructor<T, MaybeState.Just>; | ||
join<V>(this: Maybe<Maybe<V>>): Maybe<V>; | ||
@@ -34,1 +38,5 @@ map<V>(f: (r: T) => V): Maybe<V>; | ||
} | ||
export declare type Maybe<T> = MaybeConstructor<T, MaybeState.Just> | MaybeConstructor<T, MaybeState.None>; | ||
export declare const merge: typeof MaybeConstructor.merge, just: typeof MaybeConstructor.just, none: typeof MaybeConstructor.none, from: typeof MaybeConstructor.from; | ||
export declare const isMaybe: <T>(value: unknown) => value is Maybe<T>; | ||
export {}; |
66
index.js
@@ -6,45 +6,45 @@ "use strict"; | ||
} | ||
var Maybe = /** @class */ (function () { | ||
function Maybe(state, value) { | ||
this.state = state; | ||
var MaybeConstructor = /** @class */ (function () { | ||
function MaybeConstructor(type, value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
Maybe.merge = function (maybies) { | ||
MaybeConstructor.merge = function (maybies) { | ||
return maybies.reduce(function (res, v) { | ||
return v.chain(function (v) { return res.map(function (res) { return res.concat([v]); }); }); | ||
}, Maybe.just([])); | ||
}, MaybeConstructor.just([])); | ||
}; | ||
Maybe.from = function (v) { | ||
MaybeConstructor.from = function (v) { | ||
return this.just(v); | ||
}; | ||
Maybe.none = function () { | ||
return new Maybe("None" /* None */); | ||
MaybeConstructor.none = function () { | ||
return new MaybeConstructor("None" /* None */, undefined); | ||
}; | ||
Maybe.just = function (v) { | ||
return new Maybe("Just" /* Just */, v); | ||
MaybeConstructor.just = function (v) { | ||
return new MaybeConstructor("Just" /* Just */, v); | ||
}; | ||
Maybe.prototype.isNone = function () { | ||
return this.state === "None" /* None */; | ||
MaybeConstructor.prototype.isNone = function () { | ||
return this.type === "None" /* None */; | ||
}; | ||
Maybe.prototype.isJust = function () { | ||
return this.state === "Just" /* Just */; | ||
MaybeConstructor.prototype.isJust = function () { | ||
return this.type === "Just" /* Just */; | ||
}; | ||
Maybe.prototype.join = function () { | ||
MaybeConstructor.prototype.join = function () { | ||
return this.chain(function (x) { return x; }); | ||
}; | ||
Maybe.prototype.map = function (f) { | ||
MaybeConstructor.prototype.map = function (f) { | ||
if (this.isJust()) { | ||
return Maybe.just(f(this.value)); | ||
return MaybeConstructor.just(f(this.value)); | ||
} | ||
return Maybe.none(); | ||
return MaybeConstructor.none(); | ||
}; | ||
Maybe.prototype.asyncMap = function (f) { | ||
MaybeConstructor.prototype.asyncMap = function (f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(Maybe.none()); | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
return f(this.value).then(function (v) { return Maybe.just(v); }); | ||
return f(this.value).then(function (v) { return MaybeConstructor.just(v); }); | ||
}; | ||
Maybe.prototype.apply = function (argOrFn) { | ||
MaybeConstructor.prototype.apply = function (argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return Maybe.none(); | ||
return MaybeConstructor.none(); | ||
} | ||
@@ -59,5 +59,5 @@ if (isWrappedFunction(this)) { | ||
}; | ||
Maybe.prototype.asyncApply = function (argOrFn) { | ||
MaybeConstructor.prototype.asyncApply = function (argOrFn) { | ||
if (this.isNone() || argOrFn.isNone()) { | ||
return Promise.resolve(Maybe.none()); | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
@@ -72,16 +72,20 @@ if (isWrappedFunction(this)) { | ||
}; | ||
Maybe.prototype.chain = function (f) { | ||
MaybeConstructor.prototype.chain = function (f) { | ||
if (this.isNone()) { | ||
return Maybe.none(); | ||
return MaybeConstructor.none(); | ||
} | ||
return f(this.value); | ||
}; | ||
Maybe.prototype.asyncChain = function (f) { | ||
MaybeConstructor.prototype.asyncChain = function (f) { | ||
if (this.isNone()) { | ||
return Promise.resolve(Maybe.none()); | ||
return Promise.resolve(MaybeConstructor.none()); | ||
} | ||
return f(this.value); | ||
}; | ||
return Maybe; | ||
return MaybeConstructor; | ||
}()); | ||
exports["default"] = Maybe; | ||
exports["default"] = MaybeConstructor; | ||
exports.merge = MaybeConstructor.merge, exports.just = MaybeConstructor.just, exports.none = MaybeConstructor.none, exports.from = MaybeConstructor.from; | ||
exports.isMaybe = function (value) { | ||
return value instanceof MaybeConstructor; | ||
}; |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
123
README.md
@@ -21,3 +21,3 @@ # @sweet-monads/maybe | ||
```typescript | ||
import Maybe from "@sweet-monads/maybe"; | ||
import { Maybe, just, none } from "@sweet-monads/maybe"; | ||
@@ -27,3 +27,3 @@ type User = { email: string, password: string }; | ||
function getUser(id: number): Maybe<User> { | ||
return Maybe.just({ email: "test@gmail.com", password: "test" }); | ||
return just({ email: "test@gmail.com", password: "test" }); | ||
} | ||
@@ -37,6 +37,7 @@ | ||
- [`Maybe.merge`](#maybemerge) | ||
- [`Maybe.none`](#maybenone) | ||
- [`Maybe.just`](#maybejust) | ||
- [`Maybe.from`](#maybefrom) | ||
- [`merge`](#merge) | ||
- [`none`](#none) | ||
- [`just`](#just) | ||
- [`from`](#from) | ||
- [`isMaybe`](#ismaybe) | ||
- [`Maybe#isNone`](#maybeisnone) | ||
@@ -53,3 +54,3 @@ - [`Maybe#isJust`](#maybeisjust) | ||
#### `Maybe.merge` | ||
#### `merge` | ||
```typescript | ||
@@ -66,11 +67,11 @@ function merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>; | ||
```typescript | ||
const v1 = Maybe.just(2); // Maybe<number>.Just | ||
const v2 = Maybe.just("test"); // Maybe<string>.Just | ||
const v3 = Maybe.none<boolean>(); // Maybe<boolean>.None | ||
const v1 = just(2); // Maybe<number>.Just | ||
const v2 = just("test"); // Maybe<string>.Just | ||
const v3 = none<boolean>(); // Maybe<boolean>.None | ||
Maybe.merge([v1, v2]) // Maybe<[number, string]>.Just | ||
Maybe.merge([v1, v2, v3]) // Maybe<[number, string, boolean]>.None | ||
merge([v1, v2]) // Maybe<[number, string]>.Just | ||
merge([v1, v2, v3]) // Maybe<[number, string, boolean]>.None | ||
``` | ||
#### `Maybe.none` | ||
#### `none` | ||
```typescript | ||
@@ -82,7 +83,7 @@ function none<T>(): Maybe<T>; | ||
```typescript | ||
const v1 = Maybe.none(); // Maybe<unknown>.None | ||
const v2 = Maybe.none<number>(); // Maybe<number>.None | ||
const v1 = none(); // Maybe<unknown>.None | ||
const v2 = none<number>(); // Maybe<number>.None | ||
``` | ||
#### `Maybe.just` | ||
#### `just` | ||
```typescript | ||
@@ -94,9 +95,9 @@ function just<T>(value: T): Maybe<T>; | ||
```typescript | ||
const v1 = Maybe.just(2); // Maybe<number>.Just | ||
const v2 = Maybe.just<2>(2); // Maybe<2>.Just | ||
const v1 = just(2); // Maybe<number>.Just | ||
const v2 = just<2>(2); // Maybe<2>.Just | ||
``` | ||
#### `Maybe.from` | ||
#### `from` | ||
The same as [`Maybe.just`](#maybejust) | ||
The same as [`just`](#just) | ||
@@ -109,6 +110,20 @@ ```typescript | ||
```typescript | ||
const v1 = Maybe.from(2); // Maybe<number>.Just | ||
const v2 = Maybe.from<2>(2); // Maybe<2>.Just | ||
const v1 = from(2); // Maybe<number>.Just | ||
const v2 = from<2>(2); // Maybe<2>.Just | ||
``` | ||
#### `isMaybe` | ||
```typescript | ||
function isMaybe<T>(value: unknown | Maybe<T>): value is Maybe<T>; | ||
``` | ||
- Returns `boolean` if given `value` is instance of Maybe constructor. | ||
Example: | ||
```typescript | ||
const value: unknown = 2; | ||
if (isMaybe(value)) { | ||
// ... value is Maybe<unknown> at this block | ||
} | ||
``` | ||
#### `Maybe#isNone` | ||
@@ -121,4 +136,4 @@ ```typescript | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none(); | ||
const v1 = just(2); | ||
const v2 = none(); | ||
@@ -136,4 +151,4 @@ v1.isNone() // false | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none(); | ||
const v1 = just(2); | ||
const v2 = none(); | ||
@@ -152,5 +167,5 @@ v1.isJust() // true | ||
```typescript | ||
const v1 = Maybe.just(Maybe.just(2)); | ||
const v2 = Maybe.just(Maybe.none()); | ||
const v3 = Maybe.none<Maybe<number>>(); | ||
const v1 = just(just(2)); | ||
const v2 = just(none()); | ||
const v3 = none<Maybe<number>>(); | ||
@@ -169,4 +184,4 @@ v1.join() // Maybe.Just with value 2 | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
@@ -184,4 +199,4 @@ const newVal1 = v1.map(a => a.toString()); // Maybe<string>.Just with value "2" | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
@@ -204,6 +219,6 @@ // Promise<Maybe<string>.Just> with value "2" | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const fn1 = Maybe.just((a: number) => a * 2); | ||
const fn2 = Maybe.none<(a: number) => number>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
const fn1 = just((a: number) => a * 2); | ||
const fn2 = none<(a: number) => number>(); | ||
@@ -229,6 +244,6 @@ const newVal1 = fn1.apply(v1); // Maybe<number>.Just with value 4 | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const fn1 = Maybe.just((a: number) => Promise,resolve(a * 2)); | ||
const fn2 = Maybe.none<(a: number) => Promise<number>>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
const fn1 = just((a: number) => Promise,resolve(a * 2)); | ||
const fn2 = none<(a: number) => Promise<number>>(); | ||
@@ -248,9 +263,9 @@ const newVal1 = fn1.apply(v1); // Promise<Maybe<number>.Just> with value 4 | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
const newVal1 = v1.chain(a => Maybe.just(a.toString())); // Maybe<string>.Just with value "2" | ||
const newVal2 = v1.chain(a => Maybe.none()); // Maybe<string>.None without value | ||
const newVal3 = v2.chain(a => Maybe.just(a.toString())); // Maybe<string>.None without value | ||
const newVal4 = v2.chain(a => Maybe.none()); // Maybe<string>.None without value | ||
const newVal1 = v1.chain(a => just(a.toString())); // Maybe<string>.Just with value "2" | ||
const newVal2 = v1.chain(a => none()); // Maybe<string>.None without value | ||
const newVal3 = v2.chain(a => just(a.toString())); // Maybe<string>.None without value | ||
const newVal4 = v2.chain(a => none()); // Maybe<string>.None without value | ||
``` | ||
@@ -265,12 +280,12 @@ | ||
```typescript | ||
const v1 = Maybe.just(2); | ||
const v2 = Maybe.none<number>(); | ||
const v1 = just(2); | ||
const v2 = none<number>(); | ||
// Promise<Maybe<string>>.Just with value "2" | ||
const newVal1 = v1.asyncChain(a => Promise.resolve(Maybe.just(a.toString()))); | ||
const newVal1 = v1.asyncChain(a => Promise.resolve(just(a.toString()))); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal2 = v1.asyncChain(a => Promise.resolve(Maybe.none())); | ||
const newVal2 = v1.asyncChain(a => Promise.resolve(none())); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal3 = v2.asyncChain(a => Promise.resolve(Maybe.just(a.toString()))); | ||
const newVal3 = v2.asyncChain(a => Promise.resolve(just(a.toString()))); | ||
// Promise<Maybe<string>>.None without value | ||
const newVal4 = v2.asyncChain(a => Promise.resolve(Maybe.none())); | ||
const newVal4 = v2.asyncChain(a => Promise.resolve(none())); | ||
``` | ||
@@ -282,3 +297,3 @@ | ||
// Value from Maybe instance | ||
const { value } = Maybe.just(2); // number | undefined | ||
const { value } = just(2); // number | undefined | ||
``` | ||
@@ -285,0 +300,0 @@ |
15789
127
286