-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicUtils.res
164 lines (136 loc) · 4.05 KB
/
LogicUtils.res
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
let getOptionString = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.flatMap(Js.Json.decodeString)
}
let getString = (dict, key, default) => {
getOptionString(dict, key)->Belt.Option.getWithDefault(default)
}
let getIntFromString = (str, default) => {
switch str->Belt.Int.fromString {
| Some(int) => int
| None => default
}
}
let getIntFromJson = (json, default) => {
switch json->Js.Json.classify {
| JSONString(str) => getIntFromString(str, default)
| JSONNumber(floatValue) => floatValue->Belt.Float.toInt
| _ => default
}
}
let getInt = (dict, key, default) => {
switch Js.Dict.get(dict, key) {
| Some(value) => getIntFromJson(value, default)
| None => default
}
}
let getFloatFromString = (str, default) => {
switch str->Belt.Float.fromString {
| Some(floatVal) => floatVal
| None => default
}
}
let getOptionFloatFromString = str => {
str->Belt.Float.fromString
}
let getOptionFloatFromJson = json => {
switch json->Js.Json.classify {
| JSONString(str) => getOptionFloatFromString(str)
| JSONNumber(floatValue) => Some(floatValue)
| _ => None
}
}
let getFloatFromJson = (json, default) => {
switch json->Js.Json.classify {
| JSONString(str) => getFloatFromString(str, default)
| JSONNumber(floatValue) => floatValue
| _ => default
}
}
let getOptionFloat = (dict, key) => {
switch Js.Dict.get(dict, key) {
| Some(value) => getOptionFloatFromJson(value)
| None => None
}
}
let getFloat = (dict, key, default) => {
dict
->Js.Dict.get(key)
->Belt.Option.map(json => getFloatFromJson(json, default))
->Belt.Option.getWithDefault(default)
}
let getOptionBool = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.flatMap(Js.Json.decodeBoolean)
}
let getBool = (dict, key, default) => {
getOptionBool(dict, key)->Belt.Option.getWithDefault(default)
}
let getOptionIntFromString = str => {
str->Belt.Int.fromString
}
let getOptionIntFromJson = json => {
switch json->Js.Json.classify {
| JSONString(str) => getOptionIntFromString(str)
| JSONNumber(floatValue) => Some(floatValue->Belt.Float.toInt)
| _ => None
}
}
let getOptionInt = (dict, key) => {
switch Js.Dict.get(dict, key) {
| Some(value) => getOptionIntFromJson(value)
| None => None
}
}
let getOptionBool = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.flatMap(Js.Json.decodeBoolean)
}
let getOptionalJsonFromDict = (dict: Js.Dict.t<Js.Json.t>, key: Js.Dict.key) => {
dict->Js.Dict.get(key)
}
let getJsonObjectFromDict = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.getWithDefault(Js.Json.object_(Js.Dict.empty()))
}
let getStrArrayFromJsonArray = jsonArr => {
jsonArr->Belt.Array.keepMap(Js.Json.decodeString)
}
let getOptionStrArrayFromJson = json => {
json->Js.Json.decodeArray->Belt.Option.map(getStrArrayFromJsonArray)
}
let getStrArrayFromDict = (dict, key, default) => {
dict
->Js.Dict.get(key)
->Belt.Option.flatMap(getOptionStrArrayFromJson)
->Belt.Option.getWithDefault(default)
}
let getFloatArrayFromDict = (dict, key) => {
dict
->Js.Dict.get(key)
->Belt.Option.flatMap(json =>
json->Js.Json.decodeArray->Belt.Option.map(arr => arr->Belt.Array.keepMap(Js.Json.decodeNumber))
)
->Belt.Option.getWithDefault([])
}
let getIntArrayFromJsonArray = jsonArr => {
jsonArr->Belt.Array.keepMap(Js.Json.decodeNumber)->Js.Array2.map(Belt.Float.toInt)
}
let getOptionIntArrayFromJson = json => {
json->Js.Json.decodeArray->Belt.Option.map(getIntArrayFromJsonArray)
}
let getIntArrayFromDict = (dict, key, default) => {
dict
->Js.Dict.get(key)
->Belt.Option.flatMap(getOptionIntArrayFromJson)
->Belt.Option.getWithDefault(default)
}
let getOptionFloatArrayFromDict = (dict, key) => {
dict
->Js.Dict.get(key)
->Belt.Option.flatMap(json =>
json->Js.Json.decodeArray->Belt.Option.map(arr => arr->Belt.Array.keepMap(Js.Json.decodeNumber))
)
}
let getOptionStrArrayFromDict = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.flatMap(getOptionStrArrayFromJson)
}
let getOptionIntArrayFromDict = (dict, key) => {
dict->Js.Dict.get(key)->Belt.Option.flatMap(getOptionIntArrayFromJson)
}