@mfgx/shared-lib-crocks-adjunct
v3.35.0
Published
A set of additional utility functions for use with crocksjs.
Downloads
2
Readme
Crocks Adjunct
This package contains a set of utility functions that extend on the base functionality of the crocks package.
fromP
fromP :: Number -> (*... -> Promise b c) -> (*... -> Async b c)
The fromP function converts a Promise returning function into an Async returning function.
There are two additional exports related to this function, fromP1
, and fromP2
. These simply apply the first argument (arity of the returned function) with the value of 1 and 2, respectively.
fromN
NodeCallback :: (e, a) -> Unit
fromN :: Number -> ((*..., NodeCallback) -> Unit) -> ((*...) -> Async e a)
The fromN function converts a callback function into an Async returning function.
There are two additional exports related to this function, fromN1
, and fromN2
. These simply apply the first argument (the arity of the returned function) with the value of 1 and 2, respectively.
inject
inject :: Monad m => (ReaderT e (m c)) r -> Int n -> (e -> *... -> m a) -> (*... -> ReaderT e (m a))
This function transforms a Monad returning function into a ReaderT(Monad) returning function by injecting the environment as the first argument to the given Monad returning function.
The first argument is the arity of the resulting function after subtracting the first argument (e.g. 1 if the given function is arity 2). The second argument is a ReaderT wrapped Monad (e.g. ReaderT(Async)). The third argument is the Monad returning function to transform.
There is one additional export related to this function, inject1
, which simply applies the first argument with a value of 1.
traverseSerial
traverseSerial :: Applicative TypeRep t, Apply f => (t | (c -> f c)) -> (a -> f b) -> m (f a) -> f (m b)
Like traverse, except any monads supporting parallel operations will be run in series. This is accomplished by doing a reduction over the list using chain to build the final output. An index is provided to the reducing function, which differs from regular traverse since normally traverse does not support providing indices (as it works over non-list types).
cacheWith
CachingOptions :: { cache?: Map, cacheKeyFn?: (*... -> String), ofFn?: (a -> m a) }
cacheWith :: CachingOptions -> Map -> (*... -> a) -> (*... -> a)
Caches (memoizes) the result of a function. The behavior of this function is controllable through its options.
Options:
- cache: An ES6 Map compatible cache implementation. If none is provided, a new ES6 Map instance will be used.
- cacheKeyFn: The function to use for determining the cache key to use. If none is provided, identity is utilized.
- ofFn: The function to use for lifting the result value into a Monad. If none is provided, the result of the function is cached. If one is provided, the value inside the Monad result of the function is cached.
trampoline
trampoline :: (*... -> b) -> (*... -> b)
The trampoline function can be used to create stack-safe recursive functions. Rather than directly calling itself again, the function should return a thunk (form: () -> a
). Trampoline will then invoke any functions that are returned without providing any arguments.
mapPropsM
mapPropsM :: Monad m => (a -> m a) -> Object s -> Object a -> m Object b
This function is essentially the same as mapProps from crocks, except that it allows for the output of a mapping function to be an instance of the Monad created by the given of function. If it is, the value inside the monad will be used in property.
parallelAsync
parallelAsync :: Int l -> ((a, i) -> Async b c) -> [a] -> Async b [c]
This function maps over the given array with the given Async returning function, capping concurrency at the given limit. This function only works with Async returning functions.
parallelAsyncReader
parallelAsyncReader :: Int l -> ((a, i) -> ReaderT e (Async b c)) -> [a] -> Async b [c]
This function maps over the given array with the given Async returning function, capping concurrency at the given limit. This function only works with ReaderT of Async returning functions.
retryA
Logger :: { trace: a -> Unit, debug: a -> Unit, info: a -> Unit, warn: a -> Unit, error: a -> Unit, fatal: a -> Unit }
RetryOptions :: { retries?: Int, initialDelayMs?: Int, retryIf?: Error -> Boolean, logger: Logger }
retryA :: Integer -> RetryOptions -> (_... -> Async b c) -> (_... -> Async b c)
Wraps an Async-returning function such that any error that occurs can cause the function to be re-executed.
The first parameter is the arity of the function to retry. The second parameter is the options to use for retrying. The third parameter is the Async-returning function to wrap.
By default, in the options, retries is 0, initialDelayMs is 1000, retryIf is () -> true, and logger is mock logger of () -> Unit functions.
There are two additional exports, retryA1 and retryA2, that pre-apply the first argument.