@@ -11,101 +11,30 @@ enum Object.Error {
11
11
12
12
module Object .Decode {
13
13
fun field (key : String , decoder : Function (Object , Result (Object .Error , a )), input : Object ) : Result (Object .Error , a ) {
14
- `
15
- (() => {
16
- if (input == null ||
17
- input == undefined ||
18
- typeof input !== " object" ||
19
- Array .isArray (input)) {
20
- return new Err ($Object_Error_NotAnObject)
21
- } else {
22
- const actual = input[key]
23
- if (typeof actual === " undefined" ) {
24
- return new Err ($Object_Error_MissingObjectKey)
25
- }
26
- return decoder (actual)
27
- }
28
- })()
29
- `
14
+ `Decoder .field (key, decoder)(input)`
30
15
}
31
16
32
17
fun string (input : Object ) : Result (Object .Error , String ) {
33
- `
34
- (() => {
35
- if (typeof input != " string" ) {
36
- return new Err ($Object_Error_NotAString)
37
- } else {
38
- return new Ok (input)
39
- }
40
- })()
41
- `
18
+ `Decoder .string (input)`
42
19
}
43
20
44
21
fun time (input : Object ) : Result (Object .Error , Time ) {
45
- `
46
- (() => {
47
- const parsed = Date .parse (input)
48
-
49
- if (Number .isNaN (parsed)) {
50
- return new Err ($Object_Error_NotAValidTime)
51
- } else {
52
- return new Ok (new Date (parsed))
53
- }
54
- })()
55
- `
22
+ `Decoder .time (input)`
56
23
}
57
24
58
25
fun number (input : Object ) : Result (Object .Error , Number ) {
59
- `
60
- (() => {
61
- if (typeof input != " number" ) {
62
- let value = parseFloat (input)
63
-
64
- if (isNaN (value)) {
65
- return new Err ($Object_Error_NotANumber)
66
- } else {
67
- return new Ok (value)
68
- }
69
- } else {
70
- return new Ok (input)
71
- }
72
- })()
73
- `
26
+ `Decoder .number (input)`
74
27
}
75
28
76
29
fun boolean (input : Object ) : Result (Object .Error , Bool ) {
77
- `
78
- (() => {
79
- if (typeof input != " boolean" ) {
80
- return new Err ($Object_Error_NotABoolean)
81
- } else {
82
- return new Ok (input)
83
- }
84
- })()
85
- `
30
+ `Decoder .boolean (input)`
86
31
}
87
32
88
33
fun array (decoder : Function (Object , Result (Object .Error , a )), input : Object ) : Result (Object .Error , Array (a )) {
89
- `
90
- (() => {
91
- if (! Array .isArray (input)) {
92
- return new Err ($Object_Error_NotAnArray)
93
- }
94
-
95
- let results = []
96
-
97
- for (let item of input) {
98
- let result = decoder (item)
99
-
100
- if (result instanceof Err) {
101
- return result
102
- } else {
103
- results .push (result .value )
104
- }
105
- }
34
+ `Decoder .array (decoder)(input)`
35
+ }
106
36
107
- return new Ok (results)
108
- })()
109
- `
37
+ fun maybe (decoder : Function (Object , Result (Object .Error , a )), input : Object ) : Result (Object .Error , Maybe (a )) {
38
+ `Decoder .maybe (decoder)(input)`
110
39
}
111
40
}
0 commit comments