|
| 1 | +/*** |
| 2 | +Bindings to the mutable JavaScript `Map`. |
| 3 | + |
| 4 | +See [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) on MDN. |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +Type representing an instance of `Map`. |
| 9 | +*/ |
| 10 | +type t<'k, 'v> |
| 11 | + |
| 12 | +/** |
| 13 | +Creates a new, mutable JavaScript `Map`. A `Map` can have any values as both keys and values. |
| 14 | + |
| 15 | +See [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) on MDN. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Examples |
| 20 | +```rescript |
| 21 | +`make()` |
| 22 | +// You can annotate the type of your map if you want to |
| 23 | +let myMap: Map.t<string, int> = Map.make() |
| 24 | + |
| 25 | +// Or you can let ReScript infer what's in your map |
| 26 | +let map = Map.make() |
| 27 | +map->Map.set("lang", "ReScript") // Inferred as Map.t<string, string> |
| 28 | +``` |
| 29 | + |
| 30 | +## Alternatives |
| 31 | +A JavaScript `Map` is mutable. If you're looking for an immutable alternative, check out`Belt.Map`. |
| 32 | +*/ |
| 33 | +@new |
| 34 | +external make: unit => t<'k, 'v> = "Map" |
| 35 | + |
| 36 | +/** |
| 37 | +Turns an array of key/value pairs into a Map. |
| 38 | + |
| 39 | +## Examples |
| 40 | +```rescript |
| 41 | +type languages = ReScript | JavaScript | TypeScript |
| 42 | +let languageRank = [(ReScript, 1), (JavaScript, 2), (TypeScript, 3)] |
| 43 | + |
| 44 | +let map = Map.fromArray(languageRank) // Map.t<languages, int> |
| 45 | + |
| 46 | +switch map->Map.get(ReScript) { |
| 47 | +| Some(1) => Console.log("Yay, ReScript is #1!") |
| 48 | +| _ => Console.log("Uh-oh, something is _terribly_ wrong with this program... abort.") |
| 49 | +} |
| 50 | +``` |
| 51 | +*/ |
| 52 | +@new |
| 53 | +external fromArray: array<('k, 'v)> => t<'k, 'v> = "Map" |
| 54 | + |
| 55 | +/** |
| 56 | +Turns an iterator in the shape of `('key, 'value)` into a `Map`. |
| 57 | + |
| 58 | +## Examples |
| 59 | +```rescript |
| 60 | +// Let's pretend we have an interator in the correct shape |
| 61 | +@val external someIterator: Iterator.t<(string, int)> = "someIterator" |
| 62 | + |
| 63 | +let map = Map.fromIterator(someIterator) // Map.t<string, int> |
| 64 | +``` |
| 65 | +*/ |
| 66 | +@new |
| 67 | +external fromIterator: Core__Iterator.t<('k, 'v)> => t<'k, 'v> = "Map" |
| 68 | + |
| 69 | +/** |
| 70 | +Returns the size, the number of key/value pairs, of the map. |
| 71 | + |
| 72 | +## Examples |
| 73 | +```rescript |
| 74 | +let map = Map.make() |
| 75 | + |
| 76 | +map->Map.set("someKey", "someValue") |
| 77 | + |
| 78 | +let size = map->Map.size // 1 |
| 79 | +``` |
| 80 | +*/ |
| 81 | +@get |
| 82 | +external size: t<'k, 'v> => int = "size" |
| 83 | + |
| 84 | +/** |
| 85 | +Clears all entries in the map. |
| 86 | + |
| 87 | +## Examples |
| 88 | +```rescript |
| 89 | +let map = Map.make() |
| 90 | + |
| 91 | +map->Map.set("someKey", "someValue") |
| 92 | +let size = map->Map.size // 1 |
| 93 | + |
| 94 | +map->Map.clear |
| 95 | +let size = map->Map.size // 0 |
| 96 | +``` |
| 97 | +*/ |
| 98 | +@send |
| 99 | +external clear: t<'k, 'v> => unit = "clear" |
| 100 | + |
| 101 | +/** |
| 102 | +Iterates through all values of the map. |
| 103 | + |
| 104 | +> Please note that this is *without the keys*, just the values. If you need the key as well, use `Map.forEachWithKey`. |
| 105 | + |
| 106 | +## Examples |
| 107 | +```rescript |
| 108 | +let map = Map.make() |
| 109 | +map->Map.set("someKey", "someValue") |
| 110 | +map->Map.set("someKey2", "someValue2") |
| 111 | + |
| 112 | +map->Map.forEach(value => { |
| 113 | + Console.log(value) |
| 114 | +}) |
| 115 | +``` |
| 116 | +*/ |
| 117 | +@send |
| 118 | +external forEach: (t<'k, 'v>, 'v => unit) => unit = "forEach" |
| 119 | + |
| 120 | +/** |
| 121 | +Iterates through all values of the map, including the key for each value. |
| 122 | + |
| 123 | +## Examples |
| 124 | +```rescript |
| 125 | +let map = Map.make() |
| 126 | +map->Map.set("someKey", "someValue") |
| 127 | +map->Map.set("someKey2", "someValue2") |
| 128 | + |
| 129 | +map->Map.forEachWithKey((value, key) => { |
| 130 | + Console.log2(value, key) |
| 131 | +}) |
| 132 | +``` |
| 133 | +*/ |
| 134 | +@send |
| 135 | +external forEachWithKey: (t<'k, 'v>, ('v, 'k) => unit) => unit = "forEach" |
| 136 | + |
| 137 | +/** |
| 138 | +Returns the value for a key, if a value exists at that key. |
| 139 | + |
| 140 | +## Examples |
| 141 | +```rescript |
| 142 | +let map = Map.make() |
| 143 | +map->Map.set("someKey", "someValue") |
| 144 | + |
| 145 | +switch map->Map.get("someKey") { |
| 146 | +| None => Console.log("Nope, didn't have it.") |
| 147 | +| Some(value) => Console.log2("Yay, had the value, and it's:", value) |
| 148 | +} |
| 149 | +``` |
| 150 | +*/ |
| 151 | +@send |
| 152 | +external get: (t<'k, 'v>, 'k) => option<'v> = "get" |
| 153 | + |
| 154 | +/** |
| 155 | +Checks whether the map has a specific key. |
| 156 | + |
| 157 | +## Examples |
| 158 | +```rescript |
| 159 | +let map = Map.make() |
| 160 | +map->Map.set("someKey", "someValue") |
| 161 | + |
| 162 | +switch map->Map.has("someKey") { |
| 163 | +| false => Console.log("Nope, didn't have it.") |
| 164 | +| true => Console.log("Yay, we have the value!") |
| 165 | +} |
| 166 | +``` |
| 167 | +*/ |
| 168 | +@send |
| 169 | +external has: (t<'k, 'v>, 'k) => bool = "has" |
| 170 | + |
| 171 | +/** |
| 172 | +Sets the provided `value` to the provided `key`. |
| 173 | + |
| 174 | +## Examples |
| 175 | +```rescript |
| 176 | +let map = Map.make() |
| 177 | +map->Map.set("someKey", "someValue") |
| 178 | +``` |
| 179 | +*/ |
| 180 | +@send |
| 181 | +external set: (t<'k, 'v>, 'k, 'v) => unit = "set" |
| 182 | + |
| 183 | +/** |
| 184 | +Deletes the provided `key` and its value from the map. Returns a `bool` for whether the key existed, and was deleted. |
| 185 | + |
| 186 | +## Examples |
| 187 | +```rescript |
| 188 | +let map = Map.make() |
| 189 | +map->Map.set("someKey", "someValue") |
| 190 | +let didDeleteKey = map->Map.delete("someKey") |
| 191 | +Console.log(didDeleteKey) // Logs `true` to the console, becuase the map had the key, so it was successfully deleted |
| 192 | + |
| 193 | +let didDeleteKey = map->Map.delete("someNonExistantKey") |
| 194 | +Console.log(didDeleteKey) // Logs `false` to the console, becuase the key did not exist |
| 195 | +``` |
| 196 | +*/ |
| 197 | +@send |
| 198 | +external delete: (t<'k, 'v>, 'k) => bool = "delete" |
| 199 | + |
| 200 | +/** |
| 201 | +Returns an iterator that holds all keys of the map. |
| 202 | + |
| 203 | +## Examples |
| 204 | +```rescript |
| 205 | +let map = Map.make() |
| 206 | +map->Map.set("someKey", "someValue") |
| 207 | +map->Map.set("anotherKey", "anotherValue") |
| 208 | + |
| 209 | +let keys = map->Map.keys |
| 210 | + |
| 211 | +// Logs the first key |
| 212 | +Console.log(Iterator.next(keys).value) |
| 213 | + |
| 214 | +// You can also turn the iterator into an array. |
| 215 | +// Remember that an iterator consumes values. We'll need a fresh keys iterator to get an array of all keys, since we consumed a value via `next` above already. |
| 216 | +Console.log(map->Map.keys->Iterator.toArray) |
| 217 | +``` |
| 218 | +*/ |
| 219 | +@send |
| 220 | +external keys: t<'k, 'v> => Core__Iterator.t<'k> = "keys" |
| 221 | + |
| 222 | +/** |
| 223 | +Returns an iterator that holds all values of the map. |
| 224 | + |
| 225 | +## Examples |
| 226 | +```rescript |
| 227 | +let map = Map.make() |
| 228 | +map->Map.set("someKey", "someValue") |
| 229 | +map->Map.set("anotherKey", "anotherValue") |
| 230 | + |
| 231 | +let values = map->Map.values |
| 232 | + |
| 233 | +// Logs the first value |
| 234 | +Console.log(Iterator.next(values).value) |
| 235 | + |
| 236 | +// You can also turn the iterator into an array. |
| 237 | +// Remember that an iterator consumes values. We'll need a fresh values iterator to get an array of all values, since we consumed a value via `next` above already. |
| 238 | +Console.log(map->Map.values->Iterator.toArray) |
| 239 | +``` |
| 240 | +*/ |
| 241 | +@send |
| 242 | +external values: t<'k, 'v> => Core__Iterator.t<'v> = "values" |
| 243 | + |
| 244 | +/** |
| 245 | +Returns an iterator that holds all entries of the map. |
| 246 | +An entry is represented as a tuple of `('key, 'value)`, |
| 247 | + |
| 248 | +## Examples |
| 249 | +```rescript |
| 250 | +let map = Map.make() |
| 251 | +map->Map.set("someKey", "someValue") |
| 252 | +map->Map.set("anotherKey", "anotherValue") |
| 253 | + |
| 254 | +let entries = map->Map.entries |
| 255 | + |
| 256 | +// Logs the first value |
| 257 | +Console.log(Iterator.next(entries).value) |
| 258 | + |
| 259 | +// You can also turn the iterator into an array. |
| 260 | +// Remember that an iterator consumes entries. We'll need a fresh entries iterator to get an array of all entries, since we consumed a value via `next` above already. |
| 261 | +Console.log(map->Map.entries->Iterator.toArray) |
| 262 | +``` |
| 263 | +*/ |
| 264 | +@send |
| 265 | +external entries: t<'k, 'v> => Core__Iterator.t<('k, 'v)> = "entries" |
0 commit comments