@colisweb/rescript-toolkit 4.17.1 → 4.18.0

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.18.0",
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
@@ -18,11 +19,42 @@ module MakeString = () => {
18
19
  let fromList: list<(key, 'a)> => t<'a>
19
20
  let fromArray: array<(key, 'a)> => t<'a>
20
21
  let map: ((. 'a) => 'b, t<'a>) => t<'b>
22
+ let deleteKey: (t<'a>, key) => t<'a>
21
23
  }
22
24
  } = {
23
25
  @decco
24
26
  type t = string
25
- module Dict = Js.Dict
27
+ module Dict = {
28
+ include Js.Dict
29
+
30
+ let deleteKey: (t<'a>, string) => t<'a> = %raw("function (dict, key) {
31
+ const newDict = Object.assign({},dict);
32
+ delete newDict[key];
33
+ return newDict;
34
+ }")
35
+ let t_encode = (encoder, dict) => dict->Js.Dict.map((. a) => encoder(a), _)->Js.Json.object_
36
+
37
+ let t_decode = (decoder, json) => {
38
+ open Decco
39
+ switch Js.Json.decodeObject(json) {
40
+ | Some(dict) =>
41
+ dict
42
+ ->Js.Dict.entries
43
+ ->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
44
+ switch (acc, decoder(value)) {
45
+ | (Error(_), _) => acc
46
+
47
+ | (_, Error({path} as error)) => Error({...error, path: "." ++ key ++ path})
48
+
49
+ | (Ok(prev), Ok(newVal)) =>
50
+ let () = prev->Js.Dict.set(key, newVal)
51
+ Ok(prev)
52
+ }
53
+ )
54
+ | None => Error({path: "", message: "Not a dict", value: json})
55
+ }
56
+ }
57
+ }
26
58
  }
27
59
 
28
60
  @decco
@@ -52,3 +84,72 @@ module MakeInt = () => {
52
84
  let toString = t => t->toInt->Int.toString
53
85
  let castAsString = t => t->toInt->Int.toString->magic
54
86
  }
87
+
88
+ module MakeStringOrInt = () => {
89
+ module Id: {
90
+ @decco
91
+ type t
92
+ module Dict: {
93
+ type key = t
94
+ @decco
95
+ type t<'a>
96
+ let get: (t<'a>, key) => option<'a>
97
+ @set_index
98
+ external set: (t<'a>, key, 'a) => unit = ""
99
+ @val
100
+ external keys: t<'a> => array<string> = "Object.keys"
101
+ @obj /** Returns an empty dictionary. */
102
+ external empty: unit => t<'a> = ""
103
+ let unsafeDeleteKey: (. t<string>, string) => unit
104
+ let entries: t<'a> => array<(key, 'a)>
105
+ let values: t<'a> => array<'a>
106
+ let fromList: list<(key, 'a)> => t<'a>
107
+ let fromArray: array<(key, 'a)> => t<'a>
108
+ let map: ((. 'a) => 'b, t<'a>) => t<'b>
109
+ let deleteKey: (t<'a>, key) => t<'a>
110
+ }
111
+ } = {
112
+ @decco
113
+ type t = string
114
+ module Dict = {
115
+ include Js.Dict
116
+
117
+ let deleteKey: (t<'a>, string) => t<'a> = %raw("function (dict, key) {
118
+ const newDict = Object.assign({},dict);
119
+ delete newDict[key];
120
+ return newDict;
121
+ }")
122
+
123
+ let t_encode = (encoder, dict) => dict->Js.Dict.map((. a) => encoder(a), _)->Js.Json.object_
124
+
125
+ let t_decode = (decoder, json) => {
126
+ open Decco
127
+ switch Js.Json.decodeObject(json) {
128
+ | Some(dict) =>
129
+ dict
130
+ ->Js.Dict.entries
131
+ ->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
132
+ switch (acc, decoder(value)) {
133
+ | (Error(_), _) => acc
134
+
135
+ | (_, Error({path} as error)) => Error({...error, path: "." ++ key ++ path})
136
+
137
+ | (Ok(prev), Ok(newVal)) =>
138
+ let () = prev->Js.Dict.set(key, newVal)
139
+ Ok(prev)
140
+ }
141
+ )
142
+ | None => Error({path: "", message: "Not a dict", value: json})
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ @decco
149
+ type t = Id.t
150
+
151
+ module Dict = Id.Dict
152
+
153
+ external make: string => t = "%identity"
154
+ external toString: t => string = "%identity"
155
+ }