Skip to content

Commit 27586b1

Browse files
committed
feat: add more encoders
1 parent 941bd23 commit 27586b1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/Encode.res

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,61 @@
1+
type t<'a> = 'a => Js.Json.t
2+
13
external int: int => Js.Json.t = "%identity"
24
external float: float => Js.Json.t = "%identity"
35
external bool: bool => Js.Json.t = "%identity"
46
external string: string => Js.Json.t = "%identity"
57

8+
module Composite = {
9+
external jsonDict: Js.Dict.t<Js.Json.t> => Js.Json.t = "%identity"
10+
external jsonArray: array<Js.Json.t> => Js.Json.t = "%identity"
11+
external stringArray: array<string> => Js.Json.t = "%identity"
12+
external intArray: array<int> => Js.Json.t = "%identity"
13+
external floatArray: array<float> => Js.Json.t = "%identity"
14+
external boolArray: array<bool> => Js.Json.t = "%identity"
15+
}
16+
17+
@val external null: Js.Json.t = "null"
18+
19+
let array = (encode, arr) => arr->Js.Array2.map(encode)->Composite.jsonArray
20+
21+
let list = (encode, l) =>
22+
switch l {
23+
| list{} => Composite.jsonArray([])
24+
| list{hd, ...tl} =>
25+
let arr = Array.make(l->List.length, hd->encode)
26+
let rec fill = (i, l) =>
27+
switch l {
28+
| list{} => arr
29+
| list{hd, ...tl} =>
30+
Array.unsafe_set(arr, i, hd->encode)
31+
fill(i + 1, tl)
32+
}
33+
fill(1, tl)->Composite.jsonArray
34+
}
35+
36+
let option = (encode, opt) =>
37+
switch opt {
38+
| None => null
39+
| Some(v) => v->encode
40+
}
41+
42+
let withDefault = (default, encode, opt) =>
43+
switch opt {
44+
| None => default
45+
| Some(v) => v->encode
46+
}
47+
48+
let date = date => date->Js.Date.toJSONUnsafe->string
49+
50+
let pair = (encodeA, encodeB, (a, b)) => [a->encodeA, b->encodeB]->Composite.jsonArray
51+
let tuple2 = (encodeA, encodeB, (a, b)) => [a->encodeA, b->encodeB]->Composite.jsonArray
52+
let tuple3 = (encodeA, encodeB, encodeC, (a, b, c)) =>
53+
[a->encodeA, b->encodeB, c->encodeC]->Composite.jsonArray
54+
let tuple4 = (encodeA, encodeB, encodeC, encodeD, (a, b, c, d)) =>
55+
[a->encodeA, b->encodeB, c->encodeC, d->encodeD]->Composite.jsonArray
56+
57+
let dict = (encode, dict) => Js.Dict.map((. v) => encode(v), dict)->Composite.jsonDict
58+
659
module Unsafe = {
760
external object: {..} => Js.Json.t = "%identity"
861
}

src/Encode.resi

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type t<'a> = 'a => Js.Json.t
2+
3+
external int: int => Js.Json.t = "%identity"
4+
external float: float => Js.Json.t = "%identity"
5+
external bool: bool => Js.Json.t = "%identity"
6+
external string: string => Js.Json.t = "%identity"
7+
8+
@val external null: Js.Json.t = "null"
9+
let array: t<'a> => t<array<'a>>
10+
let list: t<'a> => t<list<'a>>
11+
let option: t<'a> => t<option<'a>>
12+
let withDefault: (Js.Json.t, t<'a>, option<'a>) => Js.Json.t
13+
14+
let date: Js.Date.t => Js.Json.t
15+
let pair: (t<'a>, t<'b>, ('a, 'b)) => Js.Json.t
16+
let tuple2: (t<'a>, t<'b>, ('a, 'b)) => Js.Json.t
17+
let tuple3: (t<'a>, t<'b>, t<'c>, ('a, 'b, 'c)) => Js.Json.t
18+
let tuple4: (t<'a>, t<'b>, t<'c>, t<'d>, ('a, 'b, 'c, 'd)) => Js.Json.t
19+
let dict: (t<'a>, Js.Dict.t<'a>) => Js.Json.t
20+
21+
module Composite: {
22+
external jsonDict: Js.Dict.t<Js.Json.t> => Js.Json.t = "%identity"
23+
external jsonArray: array<Js.Json.t> => Js.Json.t = "%identity"
24+
external stringArray: array<string> => Js.Json.t = "%identity"
25+
external intArray: array<int> => Js.Json.t = "%identity"
26+
external floatArray: array<float> => Js.Json.t = "%identity"
27+
external boolArray: array<bool> => Js.Json.t = "%identity"
28+
}
29+
30+
module Unsafe: {
31+
external object: {..} => Js.Json.t = "%identity"
32+
}

0 commit comments

Comments
 (0)