@visisoft/staticland
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -121,2 +121,10 @@ 'use strict'; | ||
// chainIf :: (a -> Boolean) -> (a -> Promise a) -> Promise a -> Promise a | ||
chainIf = semmelRamda.curry((predicate, fn, aPromise) => | ||
new Promise((resolve, reject_) => { | ||
aPromise | ||
.then(a => predicate(a) ? fn(a).then(resolve) : resolve(a)) | ||
.catch(reject_); | ||
})), | ||
// It's essentially Promise.then and named coalesce in crocks Async | ||
@@ -155,3 +163,22 @@ // coalesce :: (e -> b) -> (a -> b) -> Promise e a -> Promise e b | ||
return p; | ||
}); | ||
}), | ||
/** | ||
* Execute an asynchronous (could be a side-effect) function and wait until it is settled. | ||
* Mostly like `tap` except for the wait part | ||
* @template T | ||
* @param {function(T): Promise<any>} fn asynchronous side effect | ||
* @param {Promise<T>} p | ||
* @return {Promise<T>} | ||
*/ | ||
// chainTap :: (a -> Promise *) -> Promise e a -> Promise e a | ||
chainTap = semmelRamda.curry((fn, p) => | ||
new Promise((resolve, reject_) => { | ||
p | ||
.then(a => | ||
fn(a) | ||
.then(() => resolve(a)) | ||
) | ||
.catch(reject_); | ||
})); | ||
@@ -166,2 +193,4 @@ let join = semmelRamda.identity; | ||
chain: chain, | ||
chainIf: chainIf, | ||
chainTap: chainTap, | ||
chainRej: chainRej, | ||
@@ -220,2 +249,4 @@ coalesce: coalesce, | ||
//join = mx => isJust(mx) ? mx.flat() : singleNothing, // alternative: mx.flat() | ||
// :: Maybe a -> Maybe a | ||
// :: Maybe Maybe a -> Maybe a | ||
join$1 = mx => | ||
@@ -225,3 +256,3 @@ isJust(mx) | ||
? mx[0] | ||
: isNothing(mx[0]) ? nothing() : mx // this else case makes the implementation different from chain(identity) | ||
: isNothing(mx[0]) ? nothing() : mx // this else case makes the implementation different from chain(identity) and supports unnested maybes as join arguments | ||
) | ||
@@ -362,2 +393,4 @@ : nothing(), | ||
isEither = semmelRamda.either(isLeft, isRight), | ||
// Transformation // | ||
@@ -373,2 +406,5 @@ | ||
join$2 = mx => | ||
isRight(mx) && isEither(mx[1]) ? mx[1] : mx, | ||
// Consumption // | ||
@@ -389,2 +425,3 @@ | ||
isRight: isRight, | ||
join: join$2, | ||
left: left, | ||
@@ -407,2 +444,5 @@ map: map$2, | ||
// :: Promise e a -> Promise * Either e a | ||
promiseToPromiseOfEither = coalesce(left, right), | ||
// maybeToPromise :: e -> Maybe a -> Promise e a | ||
@@ -430,2 +470,3 @@ maybeToPromise = semmelRamda.curry((e, ma) => maybe(semmelRamda.thunkify(reject)(e), of, ma)), | ||
maybeOfPromiseToPromiseOfMaybe: maybeOfPromiseToPromiseOfMaybe, | ||
promiseToPromiseOfEither: promiseToPromiseOfEither, | ||
keyPromiseToPromiseCollection: keyPromiseToPromiseCollection | ||
@@ -432,0 +473,0 @@ }); |
@@ -57,3 +57,3 @@ { | ||
"type": "module", | ||
"version": "0.1.6" | ||
"version": "0.1.7" | ||
} |
@@ -8,3 +8,3 @@ /** | ||
import { compose, curry, nth, o, tryCatch, unary, when } from 'semmel-ramda'; | ||
import { compose, curry, either as eitherThisOr, nth, o, tryCatch, unary, when } from 'semmel-ramda'; | ||
@@ -28,2 +28,4 @@ const | ||
isEither = eitherThisOr(isLeft, isRight), | ||
// Transformation // | ||
@@ -39,2 +41,5 @@ | ||
join = mx => | ||
isRight(mx) && isEither(mx[1]) ? mx[1] : mx, | ||
// Consumption // | ||
@@ -48,5 +53,5 @@ | ||
export { | ||
chain, either, fromThrowable, isLeft, isRight, left, map, of | ||
chain, either, fromThrowable, isLeft, isRight, join, left, map, of | ||
}; | ||
export let right = of; |
@@ -55,2 +55,4 @@ /** | ||
//join = mx => isJust(mx) ? mx.flat() : singleNothing, // alternative: mx.flat() | ||
// :: Maybe a -> Maybe a | ||
// :: Maybe Maybe a -> Maybe a | ||
join = mx => | ||
@@ -60,3 +62,3 @@ isJust(mx) | ||
? mx[0] | ||
: isNothing(mx[0]) ? nothing() : mx // this else case makes the implementation different from chain(identity) | ||
: isNothing(mx[0]) ? nothing() : mx // this else case makes the implementation different from chain(identity) and supports unnested maybes as join arguments | ||
) | ||
@@ -63,0 +65,0 @@ : nothing(), |
@@ -118,2 +118,10 @@ /** | ||
// chainIf :: (a -> Boolean) -> (a -> Promise a) -> Promise a -> Promise a | ||
chainIf = curry((predicate, fn, aPromise) => | ||
new Promise((resolve, reject_) => { | ||
aPromise | ||
.then(a => predicate(a) ? fn(a).then(resolve) : resolve(a)) | ||
.catch(reject_); | ||
})), | ||
// It's essentially Promise.then and named coalesce in crocks Async | ||
@@ -152,8 +160,27 @@ // coalesce :: (e -> b) -> (a -> b) -> Promise e a -> Promise e b | ||
return p; | ||
}); | ||
}), | ||
/** | ||
* Execute an asynchronous (could be a side-effect) function and wait until it is settled. | ||
* Mostly like `tap` except for the wait part | ||
* @template T | ||
* @param {function(T): Promise<any>} fn asynchronous side effect | ||
* @param {Promise<T>} p | ||
* @return {Promise<T>} | ||
*/ | ||
// chainTap :: (a -> Promise *) -> Promise e a -> Promise e a | ||
chainTap = curry((fn, p) => | ||
new Promise((resolve, reject_) => { | ||
p | ||
.then(a => | ||
fn(a) | ||
.then(() => resolve(a)) | ||
) | ||
.catch(reject_); | ||
})); | ||
export { | ||
of, ap, bimap, chain, chainRej, coalesce, create, map, mapRej, reject, tap, tapRegardless | ||
of, ap, bimap, chain, chainIf, chainTap, chainRej, coalesce, create, map, mapRej, reject, tap, tapRegardless | ||
}; | ||
export let join = identity; |
@@ -8,4 +8,4 @@ /** | ||
import { either } from './either.js'; | ||
import { map as map_p, reject, of as of_p } from './promise.js'; | ||
import { either, left, right } from './either.js'; | ||
import { coalesce, map as map_p, reject, of as of_p } from './promise.js'; | ||
import { maybe, nothing, of as of_mb } from './maybe.js'; | ||
@@ -18,2 +18,5 @@ import { __, always, assoc, compose, curry, objOf, thunkify } from 'semmel-ramda'; | ||
// :: Promise e a -> Promise * Either e a | ||
promiseToPromiseOfEither = coalesce(left, right), | ||
// maybeToPromise :: e -> Maybe a -> Promise e a | ||
@@ -40,3 +43,4 @@ maybeToPromise = curry((e, ma) => maybe(thunkify(reject)(e), of_p, ma)), | ||
maybeOfPromiseToPromiseOfMaybe, | ||
promiseToPromiseOfEither, | ||
keyPromiseToPromiseCollection | ||
}; |
44181
755