-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.elm
75 lines (61 loc) · 2.21 KB
/
Util.elm
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Node.Util exposing
( Options
, defaultOptions
, inspect
, inspectLog
)
{-|
Node API: https://nodejs.org/docs/latest/api/util.html
The util module is primarily designed to support the needs of Node.js' own internal APIs. However, many of the utilities are useful for application and module developers as well.
@docs Options, defaultOptions, inspect, inspectLog
-}
import Json.Encode as Encode
import Node.Constants exposing (Depth)
import Node.Internals as Internals exposing (encodeMaybeField, encodeDepth)
import Native.Node.Util
{-| Check https://nodejs.org/docs/latest/api/util.html#util_util_inspect_object_options -}
type alias Options =
{ showHidden: Maybe Bool
, depth: Maybe Depth
, colors: Maybe Bool
, customInspect: Maybe Bool
, showProxy: Maybe Bool
, maxArrayLength: Maybe Int
, breakLength: Maybe Int
}
{-| Empty options, all Node defaults -}
defaultOptions: Options
defaultOptions =
{ showHidden = Nothing
, depth = Nothing
, colors = Nothing
, customInspect = Nothing
, showProxy = Nothing
, maxArrayLength = Nothing
, breakLength = Nothing
}
{-| Stringify any object. Support recursive values -}
inspect: Maybe Options -> a -> String
inspect options value =
Native.Node.Util.inspect value (encodeMaybeOptions options)
{-| Just like `Debug.log` but using inspect -}
inspectLog: Maybe Options -> a -> a
inspectLog options value =
Native.Node.Util.inspectLog value (encodeMaybeOptions options)
-- -----------------------------------------------------------------------------
-- JSON
encodeOptions: Options -> Encode.Value
encodeOptions options =
[ encodeMaybeField "showHidden" Encode.bool options.showHidden
, encodeMaybeField "depth" encodeDepth options.depth
, encodeMaybeField "colors" Encode.bool options.colors
, encodeMaybeField "customInspect" Encode.bool options.customInspect
, encodeMaybeField "showProxy" Encode.bool options.showProxy
, encodeMaybeField "maxArrayLength" Encode.int options.maxArrayLength
, encodeMaybeField "breakLength" Encode.int options.breakLength
]
|> List.filterMap identity
|> Encode.object
encodeMaybeOptions: Maybe Options -> Encode.Value
encodeMaybeOptions =
(Maybe.map encodeOptions) >> (Maybe.withDefault Encode.null)