@colisweb/rescript-toolkit 4.17.2 → 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.2",
3
+ "version": "4.18.0",
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,72 @@ 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
+ @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
+ }