Skip to content
This repository was archived by the owner on Feb 23, 2019. It is now read-only.

Commit c78a97a

Browse files
authored
Merge pull request #2 from mint-lang/decode
Use decoders from runtime.
2 parents 9a806a4 + 01cb317 commit c78a97a

File tree

2 files changed

+31
-80
lines changed

2 files changed

+31
-80
lines changed

source/Object/Decode.mint

Lines changed: 9 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,101 +11,30 @@ enum Object.Error {
1111

1212
module Object.Decode {
1313
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)`
3015
}
3116

3217
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)`
4219
}
4320

4421
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)`
5623
}
5724

5825
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)`
7427
}
7528

7629
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)`
8631
}
8732

8833
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+
}
10636

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)`
11039
}
11140
}

tests/Object/Decode.mint

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,25 @@ suite "Object.Decode.array" {
8989
|> Result.withDefault([])) == ["asd"]
9090
}
9191
}
92+
93+
suite "Object.Decode.maybe" {
94+
test "it returns an error if it's not a valid string" {
95+
(Object.Decode.maybe(Object.Decode.string, `0`)
96+
|> Result.withError(Object.Error::Unkown)) == Object.Error::NotAString
97+
}
98+
99+
test "it returns nothing for null" {
100+
(Object.Decode.maybe(Object.Decode.string, `null`)
101+
|> Result.withDefault(Maybe.just("A"))) == Maybe.nothing()
102+
}
103+
104+
test "it returns nothing for undefined" {
105+
(Object.Decode.maybe(Object.Decode.string, `undefined`)
106+
|> Result.withDefault(Maybe.just("A"))) == Maybe.nothing()
107+
}
108+
109+
test "it returns value if ok" {
110+
(Object.Decode.maybe(Object.Decode.string, `"A"`)
111+
|> Result.withDefault(Maybe.nothing())) == Maybe.just("A")
112+
}
113+
}

0 commit comments

Comments
 (0)