-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathStdlib_Dict.resi
More file actions
410 lines (324 loc) · 10.8 KB
/
Stdlib_Dict.resi
File metadata and controls
410 lines (324 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***
A mutable dictionary with string keys.
Compiles to a regular JavaScript object.*/
/**
Type representing a dictionary of value `'a`.
*/
type t<'a> = dict<'a>
/**
`getUnsafe(dict, key)` Returns the `value` at the provided `key`.
This is _unsafe_, meaning it will return `undefined` value if `key` does not exist in `dict`.
Use `Dict.getUnsafe` only when you are sure the key exists (i.e. when iterating `Dict.keys` result).
## Examples
```rescript
let dict = dict{"key1": "value1", "key2": "value2"}
let value = dict->Dict.getUnsafe("key1")
Console.log(value) // value1
```
*/
@get_index
external getUnsafe: (dict<'a>, string) => 'a = ""
/**
Returns the value at the provided key, if it exists. Returns an option.
## Examples
```rescript
let dict = dict{"someKey": "someValue"}
switch dict->Dict.get("someKey") {
| None => Console.log("Nope, didn't have the key.")
| Some(value) => Console.log(value)
}
```
## Alternative: Pattern Matching with `dict{}`
For nested dictionary access, consider using the more concise `dict{}` pattern matching syntax:
```rescript
// More concise approach using pattern matching
let decodeImageUrl = (json: JSON.t) => {
switch json {
| JSON.Object(dict{"data": JSON.Object(dict{"image_url": JSON.String(url)})}) => Some(url)
| _ => None
}
}
// Alternative using Dict.get
let decodeImageUrl = (json: JSON.t) => {
switch json {
| JSON.Object(obj) =>
switch Dict.get(obj, "data") {
| Some(JSON.Object(data)) =>
switch Dict.get(data, "image_url") {
| Some(JSON.String(url)) => Some(url)
| _ => None
}
| _ => None
}
| _ => None
}
}
```
*/
@get_index
external get: (dict<'a>, string) => option<'a> = ""
/**
`set(dictionary, key, value)` sets the value at the provided key to the provided value.
## Examples
```rescript
let dict = Dict.make()
dict->Dict.set("someKey", "someValue")
```
*/
@set_index
external set: (dict<'a>, string, 'a) => unit = ""
/**
`delete(dictionary, key)` deletes the value at `key`, if it exists.
## Examples
```rescript
let dict = dict{"someKey": "someValue"}
dict->Dict.delete("someKey")
```
*/
let delete: (dict<'a>, string) => unit
/**
`make()` creates a new, empty dictionary.
## Examples
```rescript
let dict1: dict<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want
let dict2 = Dict.make() // Or you can let ReScript infer it via usage.
dict2->Dict.set("someKey", 12)
```
*/
@obj
external make: unit => dict<'a> = ""
/**
`fromArray(entries)` creates a new dictionary from the provided array of key/value pairs.
## Examples
```rescript
let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
```
*/
@val
external fromArray: array<(string, 'a)> => dict<'a> = "Object.fromEntries"
/**
`fromIterable(entries)` creates a new dictionary from the provided iterable of key/value pairs.
## Examples
```rescript
let iterable: Iterable.t<(string, int)> = [("first", 1), ("second", 2)]->Array.asIterable
iterable
->Dict.fromIterable
->Dict.valuesToArray == [1, 2]
```
*/
@val
external fromIterable: Stdlib_Iterable.t<(string, 'a)> => dict<'a> = "Object.fromEntries"
/**
`toArray(dictionary)` returns an array of all the key/value pairs of the dictionary.
## Examples
```rescript
let dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let asArray = dict->Dict.toArray
Console.log(asArray) // Logs `[["someKey", 1], ["someKey2", 2]]` to the console
```
*/
@val
external toArray: dict<'a> => array<(string, 'a)> = "Object.entries"
/**
`keysToArray(dictionary)` returns an array of all the keys of the dictionary.
## Examples
```rescript
let dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let keys = dict->Dict.keysToArray
Console.log(keys) // Logs `["someKey", "someKey2"]` to the console
```
*/
@val
external keysToArray: dict<'a> => array<string> = "Object.keys"
/**
`size(dictionary)` returns the number of key/value pairs in the dictionary.
## Examples
```rescript
let dict = Dict.make()
dict->Dict.size->assertEqual(0)
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
dict->Dict.size->assertEqual(2)
// After deleting a key
dict->Dict.delete("someKey")
dict->Dict.size->assertEqual(1)
```
*/
let size: dict<'a> => int
/**
`isEmpty(dictionary)` returns `true` if the dictionary is empty (has no key/value pairs), `false` otherwise.
## Examples
```rescript
let emptyDict = Dict.make()
emptyDict->Dict.isEmpty->assertEqual(true)
let dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.isEmpty->assertEqual(false)
// After clearing all keys
dict->Dict.delete("someKey")
dict->Dict.isEmpty->assertEqual(true)
```
*/
let isEmpty: dict<'a> => bool
/**
`valuesToArray(dictionary)` returns an array of all the values of the dictionary.
## Examples
```rescript
let dict = Dict.make()
dict->Dict.set("someKey", 1)
dict->Dict.set("someKey2", 2)
let values = dict->Dict.valuesToArray
Console.log(values) // Logs `[1, 2]` to the console
```
*/
@val
external valuesToArray: dict<'a> => array<'a> = "Object.values"
/**
`assign(dictionary1, dictionary2)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges dictionary2 into dictionary1, and returns dictionary1.
Beware this will *mutate* dictionary1. If you're looking for a fresh merged dictionary instead, check out `Dict.concat`.
## Examples
```rescript
let dict1 = Dict.make()
dict1->Dict.set("firstKey", 1)
Console.log(dict1->Dict.keysToArray) // Logs `["firstKey"]`
let dict2 = Dict.make()
dict2->Dict.set("someKey", 2)
dict2->Dict.set("someKey2", 3)
let dict1 = dict1->Dict.assign(dict2)
Console.log(dict1->Dict.keysToArray) // Logs `["firstKey", "someKey", "someKey2"]`
```
*/
@val
external assign: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign"
/**
`assignMany(target, sources)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges each dictionary in `sources` into `target`, and returns `target`.
Beware this will *mutate* `target`. Later dictionaries overwrite earlier ones. If you're looking for a fresh merged dictionary instead, check out `Dict.concatMany`.
## Examples
```rescript
let target = dict{"firstKey": 1}
let result = target->Dict.assignMany([
dict{"someKey": 2},
dict{"someKey": 3, "someKey2": 4},
])
result == dict{"firstKey": 1, "someKey": 3, "someKey2": 4}
(result === target) == true
```
*/
@variadic @val
external assignMany: (dict<'a>, array<dict<'a>>) => dict<'a> = "Object.assign"
/**
`concat(dictionary1, dictionary2)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges `dictionary1` and `dictionary2` into a fresh dictionary, and returns the new dictionary.
Neither input dictionary is mutated. If both dictionaries contain the same key, the value from `dictionary2` overwrites the one from `dictionary1`.
## Examples
```rescript
let dict1 = dict{"firstKey": 1}
let dict2 = dict{"firstKey": 2, "someKey": 3}
let merged = dict1->Dict.concat(dict2)
dict1 == dict{"firstKey": 1}
merged == dict{"firstKey": 2, "someKey": 3}
(merged === dict1) == false
```
*/
@val
external concat: (@as(json`{}`) _, dict<'a>, dict<'a>) => dict<'a> = "Object.assign"
/**
`concatMany(target, sources)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges `target` and each dictionary in `sources` into a fresh dictionary, and returns the new dictionary.
Neither `target` nor any dictionary in `sources` is mutated. Later dictionaries overwrite earlier ones when they contain the same key.
## Examples
```rescript
let target = dict{"firstKey": 1}
let merged = target->Dict.concatMany([
dict{"someKey": 2},
dict{"someKey": 3, "someKey2": 4},
])
target == dict{"firstKey": 1}
merged == dict{"firstKey": 1, "someKey": 3, "someKey2": 4}
(merged === target) == false
```
*/
@variadic @val
external concatMany: (@as(json`{}`) _, dict<'a>, array<dict<'a>>) => dict<'a> = "Object.assign"
/**
`concatAll(dictionaries)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges all dictionaries in `dictionaries` into a fresh dictionary, and returns the new dictionary.
None of the input dictionaries are mutated. Later dictionaries overwrite earlier ones when they contain the same key. If `dictionaries` is empty, an empty dictionary is returned.
## Examples
```rescript
let dict1 = dict{"firstKey": 1}
let dict2 = dict{"someKey": 2}
let dict3 = dict{"someKey": 3, "someKey2": 4}
let merged = Dict.concatAll([dict1, dict2, dict3])
dict1 == dict{"firstKey": 1}
merged == dict{"firstKey": 1, "someKey": 3, "someKey2": 4}
(merged === dict1) == false
```
*/
@variadic @val
external concatAll: (@as(json`{}`) _, array<dict<'a>>) => dict<'a> = "Object.assign"
/**
`copy(dictionary)` [shallowly copies](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) the provided dictionary to a new dictionary.
## Examples
```rescript
let dict = dict{"key1": "value1", "key2": "value2"}
let dict2 = dict->Dict.copy
// Both log `["key1", "key2"]` here.
Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)
```
*/
@val
external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"
/**
`forEach(dictionary, f)` iterates through all values of the dict.
> Please note that this is *without the keys*, just the values. If you need the key as well, use `Dict.forEachWithKey`.
## Examples
```rescript
let dict = dict{"key1": "value1", "key2": "value2"}
dict->Dict.forEach(value => {
Console.log(value)
})
```
*/
let forEach: (dict<'a>, 'a => unit) => unit
/**
`forEachWithKey(dictionary, f)` iterates through all values of the dict, including the key for each value.
## Examples
```rescript
let dict = dict{"key1": "value1", "key2": "value2"}
dict->Dict.forEachWithKey((value, key) => {
Console.log2(value, key)
})
```
*/
let forEachWithKey: (dict<'a>, ('a, string) => unit) => unit
/**
`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary.
## Examples
```rescript
let dict = dict{"key1": 1, "key2": 2}
dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)]
dict->Dict.mapValues(v => Int.toString(v))->Dict.toArray // [("key1", "1"), ("key2", "2")]
```
*/
let mapValues: (dict<'a>, 'a => 'b) => dict<'b>
/**
`has(dictionary, "key")` returns true if the "key" is present in the dictionary.
Be aware that it uses the JavaScript `in` operator under the hood.
## Examples
```rescript
let dict = dict{"key1": Some(1), "key2": None}
dict->Dict.has("key1") == true
dict->Dict.has("key2") == true
dict->Dict.has("key3") == false
dict->Dict.has("toString") == true
```
*/
external has: (dict<'a>, string) => bool = "%dict_has"
/**
`ignore(dict)` ignores the provided dict and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects)
without having to store or process it further.
*/
external ignore: dict<'a> => unit = "%ignore"