-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
47 lines (39 loc) · 1.08 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { curry, head } = require('ramda')
const Maybe = require('data.maybe')
const Task = require('data.task')
const Either = require('data.either')
// eitherToTask :: Either a -> Task a
const eitherToTask = either =>
either.fold(Task.rejected, Task.of)
// maybeTask :: Maybe a -> Task a
const maybeToTask = maybe =>
maybe.isNothing
? Task.rejected()
: Task.of(maybe.get())
const maybeResultToTaskResult = err => maybe =>
maybe.isNothing
? Task.of(err)
: Task.of(maybe.get())
// eitherToMaybe :: Either a -> Maybe a
const eitherToMaybe = either =>
either.fold(Maybe.Nothing, Maybe.Just)
// maybeToEither :: String a -> Maybe b -> Either a b
const maybeToEither = err => maybe =>
maybe.isNothing
? Either.Left(err)
: Either.Right(maybe.get())
// safeProp :: String -> {} -> Maybe a
const safeProp = curry((prop, obj) =>
Maybe.fromNullable(obj[prop]))
// safeHead :: [a] -> Maybe a
const safeHead = aList =>
Maybe.fromNullable(head(aList))
module.exports = {
safeProp,
safeHead,
maybeToTask,
maybeToEither,
maybeResultToTaskResult,
eitherToTask,
eitherToMaybe,
}