@colisweb/rescript-toolkit 4.17.1 → 4.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colisweb/rescript-toolkit",
3
- "version": "4.17.1",
3
+ "version": "4.17.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -4,6 +4,7 @@ module MakeString = () => {
4
4
  type t
5
5
  module Dict: {
6
6
  type key = t
7
+ @decco
7
8
  type t<'a>
8
9
  let get: (t<'a>, key) => option<'a>
9
10
  @set_index
@@ -22,7 +23,32 @@ module MakeString = () => {
22
23
  } = {
23
24
  @decco
24
25
  type t = string
25
- module Dict = Js.Dict
26
+ module Dict = {
27
+ include Js.Dict
28
+
29
+ let t_encode = (encoder, dict) => dict->Js.Dict.map((. a) => encoder(a), _)->Js.Json.object_
30
+
31
+ let t_decode = (decoder, json) => {
32
+ open Decco
33
+ switch Js.Json.decodeObject(json) {
34
+ | Some(dict) =>
35
+ dict
36
+ ->Js.Dict.entries
37
+ ->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
38
+ switch (acc, decoder(value)) {
39
+ | (Error(_), _) => acc
40
+
41
+ | (_, Error({path} as error)) => Error({...error, path: "." ++ key ++ path})
42
+
43
+ | (Ok(prev), Ok(newVal)) =>
44
+ let () = prev->Js.Dict.set(key, newVal)
45
+ Ok(prev)
46
+ }
47
+ )
48
+ | None => Error({path: "", message: "Not a dict", value: json})
49
+ }
50
+ }
51
+ }
26
52
  }
27
53
 
28
54
  @decco