@colisweb/rescript-toolkit 4.17.2 → 4.18.1

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.2",
3
+ "version": "4.18.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "clean": "rescript clean",
@@ -19,6 +19,7 @@ module MakeString = () => {
19
19
  let fromList: list<(key, 'a)> => t<'a>
20
20
  let fromArray: array<(key, 'a)> => t<'a>
21
21
  let map: ((. 'a) => 'b, t<'a>) => t<'b>
22
+ let deleteKey: (t<'a>, key) => t<'a>
22
23
  }
23
24
  } = {
24
25
  @decco
@@ -26,6 +27,11 @@ module MakeString = () => {
26
27
  module Dict = {
27
28
  include Js.Dict
28
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
+ }")
29
35
  let t_encode = (encoder, dict) => dict->Js.Dict.map((. a) => encoder(a), _)->Js.Json.object_
30
36
 
31
37
  let t_decode = (decoder, json) => {
@@ -78,3 +84,83 @@ module MakeInt = () => {
78
84
  let toString = t => t->toInt->Int.toString
79
85
  let castAsString = t => t->toInt->Int.toString->magic
80
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
+ let encoder: Decco.encoder<string> = id => id->Decco.stringToJson
113
+
114
+ let decoder: Decco.decoder<string> = json =>
115
+ switch (Decco.stringFromJson(json), Decco.intFromJson(json)) {
116
+ | (Ok(v), _) => Ok(v)
117
+ | (_, Ok(v)) => Ok(v->Int.toString)
118
+ | (Error(err), _) => Error(err)
119
+ }
120
+
121
+ let codec: Decco.codec<string> = (encoder, decoder)
122
+
123
+ @decco
124
+ type t = @decco.codec(codec) string
125
+ module Dict = {
126
+ include Js.Dict
127
+
128
+ let deleteKey: (t<'a>, string) => t<'a> = %raw("function (dict, key) {
129
+ const newDict = Object.assign({},dict);
130
+ delete newDict[key];
131
+ return newDict;
132
+ }")
133
+
134
+ let t_encode = (encoder, dict) => dict->Js.Dict.map((. a) => encoder(a), _)->Js.Json.object_
135
+
136
+ let t_decode = (decoder, json) => {
137
+ open Decco
138
+ switch Js.Json.decodeObject(json) {
139
+ | Some(dict) =>
140
+ dict
141
+ ->Js.Dict.entries
142
+ ->Belt.Array.reduce(Ok(Js.Dict.empty()), (acc, (key, value)) =>
143
+ switch (acc, decoder(value)) {
144
+ | (Error(_), _) => acc
145
+
146
+ | (_, Error({path} as error)) => Error({...error, path: "." ++ key ++ path})
147
+
148
+ | (Ok(prev), Ok(newVal)) =>
149
+ let () = prev->Js.Dict.set(key, newVal)
150
+ Ok(prev)
151
+ }
152
+ )
153
+ | None => Error({path: "", message: "Not a dict", value: json})
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ @decco
160
+ type t = Id.t
161
+
162
+ module Dict = Id.Dict
163
+
164
+ external make: string => t = "%identity"
165
+ external toString: t => string = "%identity"
166
+ }