@sweet-monads/maybe
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -25,4 +25,8 @@ import { Monad } from "@sweet-monads/interfaces"; | ||
asyncMap<V>(f: (r: T) => Promise<V>): Promise<Maybe<V>>; | ||
apply<A, B>(this: Maybe<(a: A) => B>, arg: Maybe<A>): Maybe<B>; | ||
apply<A, B>(this: Maybe<A>, fn: Maybe<(a: A) => B>): Maybe<B>; | ||
asyncApply<A, B>(this: Maybe<(a: Promise<A> | A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>; | ||
asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: Promise<A> | A) => Promise<B>>): Promise<Maybe<B>>; | ||
chain<V>(f: (r: T) => Maybe<V>): Maybe<V>; | ||
asyncChain<V>(f: (r: T) => Promise<Maybe<V>>): Promise<Maybe<V>>; | ||
} |
23
index.js
const MaybeState = { | ||
Just: "Just", | ||
None: "None" | ||
None: "None", | ||
}; | ||
function isWrappedFunction(m) { | ||
return typeof m.value === "function"; | ||
} | ||
export class Maybe { | ||
@@ -43,2 +46,20 @@ constructor(state, value) { | ||
} | ||
apply(argOrFn) { | ||
if (isWrappedFunction(this) && !isWrappedFunction(argOrFn)) { | ||
if (this.isJust()) { | ||
return argOrFn.map(this.value); | ||
} | ||
return Maybe.none(); | ||
} | ||
return argOrFn.apply(this); | ||
} | ||
asyncApply(argOrFn) { | ||
if (isWrappedFunction(this) && !isWrappedFunction(argOrFn)) { | ||
if (this.isJust()) { | ||
return argOrFn.asyncMap(this.value); | ||
} | ||
return Promise.resolve(Maybe.none()); | ||
} | ||
return argOrFn.asyncApply(this); | ||
} | ||
chain(f) { | ||
@@ -45,0 +66,0 @@ if (this.isNone()) { |
{ | ||
"name": "@sweet-monads/maybe", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@sweet-monads/interfaces": "^1.1.1" | ||
"@sweet-monads/interfaces": "^1.2.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "author": "", |
@@ -44,2 +44,4 @@ # @sweet-monads/maybe | ||
- [`Maybe#asyncMap`](#maybeasyncmap) | ||
- [`Maybe#apply`](#maybeapply) | ||
- [`Maybe#asyncApply`](#maybeasyncapply) | ||
- [`Maybe#chain`](#maybechain) | ||
@@ -180,2 +182,47 @@ - [`Maybe#asyncChain`](#maybeasyncchain) | ||
##### `Maybe#apply` | ||
```typescript | ||
function apply<A, B>(this: Maybe<(a: A) => B>, arg: Maybe<A>): Maybe<B>; | ||
function apply<A, B>(this: Maybe<A>, fn: Maybe<(a: A) => B>): Maybe<B>; | ||
``` | ||
- `this | fn` - function wrapped by Maybe, which should be applied to value `arg` | ||
- `arg | this` - value which should be applied to `fn` | ||
- Returns 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>(); | ||
const fn1 = Maybe.just((a: number) => a * 2); | ||
const fn2 = Maybe.none<(a: number) => number>(); | ||
const newVal1 = fn1.apply(v1); // Maybe<number>.Just with value 4 | ||
const newVal2 = fn1.apply(v2); // Maybe<number>.None without value | ||
const newVal3 = fn2.apply(v1); // Maybe<number>.None without value | ||
const newVal4 = fn2.apply(v2); // Maybe<number>.None without value | ||
``` | ||
##### `Maybe#asyncApply` | ||
Async variant of [`Maybe#apply`](#maybeapply) | ||
```typescript | ||
function asyncApply<A, B>(this: Maybe<(a: Promise<A> | A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>; | ||
function asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: Promise<A> | A) => Promise<B>>): Promise<Maybe<B>>; | ||
``` | ||
- `this | fn` - function wrapped by Maybe, which should be applied to value `arg` | ||
- `arg | this` - value which should be applied to `fn` | ||
- 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>(); | ||
const fn1 = Maybe.just((a: number) => Promise,resolve(a * 2)); | ||
const fn2 = Maybe.none<(a: number) => Promise<number>>(); | ||
const newVal1 = fn1.apply(v1); // Promise<Maybe<number>.Just> with value 4 | ||
const newVal2 = fn1.apply(v2); // Promise<Maybe<number>.None> without value | ||
const newVal3 = fn2.apply(v1); // Promise<Maybe<number>.None> without value | ||
const newVal4 = fn2.apply(v2); // Promise<Maybe<number>.None> without value | ||
``` | ||
#### `Maybe#chain` | ||
@@ -182,0 +229,0 @@ ```typescript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14687
107
271