Skip to content

Commit f8b7d1d

Browse files
DZakhzth
authored andcommitted
Add Dict.getUnsafe
1 parent ffca0a6 commit f8b7d1d

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

src/Core__Dict.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type t<'a> = Js.Dict.t<'a>
22

3+
@get_index external getUnsafe: (t<'a>, string) => 'a = ""
34
@get_index external get: (t<'a>, string) => option<'a> = ""
45
@set_index external set: (t<'a>, string, 'a) => unit = ""
56
@val external delete: 'a => unit = "delete"

src/Core__Dict.resi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ Type representing a dictionary of value `'a`.
88
*/
99
type t<'a> = Js.Dict.t<'a>
1010

11+
/**
12+
Returns the value at the provided key. Assumes the value always exists.
13+
Use `Dict.getUnsafe` only when you are sure the key exists (i.e. when having used the `Dict.keys` function to check that the key is valid).
14+
15+
## Examples
16+
```rescript
17+
let keys = dict->Dict.keys
18+
19+
keys->Array.forEach(key => {
20+
let value = dict->Dict.getUnsafe(key)
21+
Console.log(value)
22+
})
23+
```
24+
*/
25+
@get_index
26+
external getUnsafe: (t<'a>, string) => 'a = ""
27+
1128
/**
1229
Returns the value at the provided key, if it exists. Returns an option.
1330

test/DictTests.mjs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
6+
var eq = Caml_obj.equal;
7+
8+
Test.run([
9+
[
10+
"DictTests.res",
11+
5,
12+
20,
13+
26
14+
],
15+
"make"
16+
], {}, eq, {});
17+
18+
Test.run([
19+
[
20+
"DictTests.res",
21+
7,
22+
20,
23+
31
24+
],
25+
"fromArray"
26+
], Object.fromEntries([[
27+
"foo",
28+
"bar"
29+
]]), eq, {foo: "bar"});
30+
31+
Test.run([
32+
[
33+
"DictTests.res",
34+
10,
35+
13,
36+
35
37+
],
38+
"getUnsafe - existing"
39+
], Object.fromEntries([[
40+
"foo",
41+
"bar"
42+
]])["foo"], eq, "bar");
43+
44+
Test.run([
45+
[
46+
"DictTests.res",
47+
16,
48+
13,
49+
34
50+
],
51+
"getUnsafe - missing"
52+
], ({})["foo"], eq, undefined);
53+
54+
export {
55+
eq ,
56+
}
57+
/* Not a pure module */

test/DictTests.res

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
Test.run(__POS_OF__("make"), Dict.make(), eq, %raw(`{}`))
6+
7+
Test.run(__POS_OF__("fromArray"), Dict.fromArray([("foo", "bar")]), eq, %raw(`{foo: "bar"}`))
8+
9+
Test.run(
10+
__POS_OF__("getUnsafe - existing"),
11+
Dict.fromArray([("foo", "bar")])->Dict.getUnsafe("foo"),
12+
eq,
13+
"bar",
14+
)
15+
Test.run(
16+
__POS_OF__("getUnsafe - missing"),
17+
Dict.make()->Dict.getUnsafe("foo"),
18+
eq,
19+
%raw(`undefined`),
20+
)

0 commit comments

Comments
 (0)