|
| 1 | +/*** |
| 2 | +A mutable dictionary with string keys. |
| 3 | + |
| 4 | +Compiles to a regular JavaScript object.*/ |
| 5 | + |
| 6 | +/** |
| 7 | +Type representing a dictionary of value `'a`. |
| 8 | +*/ |
| 9 | +type t<'a> = Js.Dict.t<'a> |
| 10 | + |
| 11 | +/** |
| 12 | +Returns the value at the provided key, if it exists. Returns an option. |
| 13 | + |
| 14 | +## Examples |
| 15 | +```rescript |
| 16 | +let dict = Dict.fromArray([("someKey", "someValue")]) |
| 17 | + |
| 18 | +switch dict->Dict.get("someKey") { |
| 19 | +| None => Console.log("Nope, didn't have the key.") |
| 20 | +| Some(value) => Console.log(value) |
| 21 | +} |
| 22 | +``` |
| 23 | +*/ |
| 24 | +@get_index |
| 25 | +external get: (t<'a>, string) => option<'a> = "" |
| 26 | + |
| 27 | +/** |
| 28 | +`set(dictionary, key, value)` sets the value at the provided key to the provided value. |
| 29 | + |
| 30 | +## Examples |
| 31 | +```rescript |
| 32 | +let dict = Dict.make() |
| 33 | + |
| 34 | +dict->Dict.set("someKey", "someValue") |
| 35 | +``` |
| 36 | +*/ |
| 37 | +@set_index |
| 38 | +external set: (t<'a>, string, 'a) => unit = "" |
| 39 | + |
| 40 | +/** |
| 41 | +`delete(dictionary, key)` deletes the value at `key`, if it exists. |
| 42 | + |
| 43 | +## Examples |
| 44 | +```rescript |
| 45 | +let dict = Dict.fromArray([("someKey", "someValue")]) |
| 46 | + |
| 47 | +dict->Dict.delete("someKey") |
| 48 | +``` |
| 49 | +*/ |
| 50 | +let delete: (t<'a>, string) => unit |
| 51 | + |
| 52 | +/** |
| 53 | +`make()` creates a new, empty dictionary. |
| 54 | + |
| 55 | +## Examples |
| 56 | +```rescript |
| 57 | +let dict1: Dict.t<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want |
| 58 | + |
| 59 | +let dict2 = Dict.make() // Or you can let ReScript infer it via usage. |
| 60 | +dict2->Dict.set("someKey", 12) |
| 61 | +``` |
| 62 | +*/ |
| 63 | +@obj |
| 64 | +external make: unit => t<'a> = "" |
| 65 | + |
| 66 | +/** |
| 67 | +`fromArray(entries)` creates a new dictionary from the provided array of key/value pairs. |
| 68 | + |
| 69 | +## Examples |
| 70 | +```rescript |
| 71 | +let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")]) |
| 72 | +``` |
| 73 | +*/ |
| 74 | +@val |
| 75 | +external fromArray: array<(string, 'a)> => t<'a> = "Object.fromEntries" |
| 76 | + |
| 77 | +/** |
| 78 | +`fromIterator(entries)` creates a new dictionary from the provided iterator of key/value pairs. |
| 79 | + |
| 80 | +## Examples |
| 81 | +```rescript |
| 82 | +// Pretend we have an iterator of the correct shape |
| 83 | +@val external someIterator: Iterator.t<(string, int)> = "someIterator" |
| 84 | + |
| 85 | +let dict = Dict.fromIterator(someIterator) // Dict.t<int> |
| 86 | +``` |
| 87 | +*/ |
| 88 | +@val |
| 89 | +external fromIterator: Core__Iterator.t<(string, 'a)> => t<'a> = "Object.fromEntries" |
| 90 | + |
| 91 | +/** |
| 92 | +`toArray(dictionary)` returns an array of all the key/value pairs of the dictionary. |
| 93 | + |
| 94 | +## Examples |
| 95 | +```rescript |
| 96 | +let dict = Dict.make() |
| 97 | +dict->Dict.set("someKey", 1) |
| 98 | +dict->Dict.set("someKey2", 2) |
| 99 | +let asArray = dict->Dict.toArray |
| 100 | +Console.log(asArray) // Logs `[["someKey", 1], ["someKey2", 2]]` to the console |
| 101 | +``` |
| 102 | +*/ |
| 103 | +@val |
| 104 | +external toArray: t<'a> => array<(string, 'a)> = "Object.entries" |
| 105 | + |
| 106 | +/** |
| 107 | +`keysToArray(dictionary)` returns an array of all the keys of the dictionary. |
| 108 | + |
| 109 | +## Examples |
| 110 | +```rescript |
| 111 | +let dict = Dict.make() |
| 112 | +dict->Dict.set("someKey", 1) |
| 113 | +dict->Dict.set("someKey2", 2) |
| 114 | +let keys = dict->Dict.keysToArray |
| 115 | +Console.log(keys) // Logs `["someKey", "someKey2"]` to the console |
| 116 | +``` |
| 117 | +*/ |
| 118 | +@val |
| 119 | +external keysToArray: t<'a> => array<string> = "Object.keys" |
| 120 | + |
| 121 | +/** |
| 122 | +`valuesToArray(dictionary)` returns an array of all the values of the dictionary. |
| 123 | + |
| 124 | +## Examples |
| 125 | +```rescript |
| 126 | +let dict = Dict.make() |
| 127 | +dict->Dict.set("someKey", 1) |
| 128 | +dict->Dict.set("someKey2", 2) |
| 129 | +let values = dict->Dict.valuesToArray |
| 130 | +Console.log(values) // Logs `[1, 2]` to the console |
| 131 | +``` |
| 132 | +*/ |
| 133 | +@val |
| 134 | +external valuesToArray: t<'a> => array<'a> = "Object.values" |
| 135 | + |
| 136 | +/** |
| 137 | +`assign(dictionary1, dictionary2)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges dictionary2 into dictionary1, and returns dictionary1. |
| 138 | + |
| 139 | +Beware this will *mutate* dictionary1. If you're looking for a way to copy a dictionary, check out `Dict.copy`. |
| 140 | + |
| 141 | +## Examples |
| 142 | +```rescript |
| 143 | +let dict1 = Dict.make() |
| 144 | +dict1->Dict.set("firstKey", 1) |
| 145 | +Console.log(dict1->Dict.keysToArray) // Logs `["firstKey"]` |
| 146 | + |
| 147 | +let dict2 = Dict.make() |
| 148 | +dict2->Dict.set("someKey", 2) |
| 149 | +dict2->Dict.set("someKey2", 3) |
| 150 | + |
| 151 | +let dict1 = dict1->Dict.assign(dict2) |
| 152 | + |
| 153 | +Console.log(dict1->Dict.keysToArray) // Logs `["firstKey", "someKey", "someKey2"]` |
| 154 | + |
| 155 | +``` |
| 156 | +*/ |
| 157 | +@val |
| 158 | +external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign" |
| 159 | + |
| 160 | +/** |
| 161 | +`copy(dictionary)` [shallowly copies](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) the provided dictionary to a new dictionary. |
| 162 | + |
| 163 | +## Examples |
| 164 | +```rescript |
| 165 | +let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")]) |
| 166 | +let dict2 = dict->Dict.copy |
| 167 | + |
| 168 | +// Both log `["key1", "key2"]` here. |
| 169 | +Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray) |
| 170 | +``` |
| 171 | +*/ |
| 172 | +@val |
| 173 | +external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign" |
0 commit comments