Skip to content

Add Dict.getUnsafe #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- Add `Dict.getUnsafe` https://github.com/rescript-association/rescript-core/pull/167

## 0.6.0

### API changes
Expand Down
4 changes: 4 additions & 0 deletions migration/migration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ rewrite="Dict.make"
match="Js.Dict.keys"
rewrite="Dict.keysToArray"

[js-dict-a-get-unsafe]
match="Js.Dict.unsafeGet"
rewrite="Dict.getUnsafe"

[js-dict-z]
match="Js.Dict"
rewrite="Dict"
Expand Down
1 change: 1 addition & 0 deletions src/Core__Dict.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type t<'a> = Js.Dict.t<'a>

@get_index external getUnsafe: (t<'a>, string) => 'a = ""
@get_index external get: (t<'a>, string) => option<'a> = ""
@set_index external set: (t<'a>, string, 'a) => unit = ""
@val external delete: 'a => unit = "delete"
Expand Down
17 changes: 17 additions & 0 deletions src/Core__Dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ Type representing a dictionary of value `'a`.
*/
type t<'a> = Js.Dict.t<'a>

/**
Returns the value at the provided key. Assumes the value always exists.
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).

## Examples
```rescript
let keys = dict->Dict.keys

keys->Array.forEach(key => {
let value = dict->Dict.getUnsafe(key)
Console.log(value)
})
```
*/
@get_index
external getUnsafe: (t<'a>, string) => 'a = ""

/**
Returns the value at the provided key, if it exists. Returns an option.

Expand Down
57 changes: 57 additions & 0 deletions test/DictTests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";

var eq = Caml_obj.equal;

Test.run([
[
"DictTests.res",
5,
20,
26
],
"make"
], {}, eq, {});

Test.run([
[
"DictTests.res",
7,
20,
31
],
"fromArray"
], Object.fromEntries([[
"foo",
"bar"
]]), eq, {foo: "bar"});

Test.run([
[
"DictTests.res",
10,
13,
35
],
"getUnsafe - existing"
], Object.fromEntries([[
"foo",
"bar"
]])["foo"], eq, "bar");

Test.run([
[
"DictTests.res",
16,
13,
34
],
"getUnsafe - missing"
], ({})["foo"], eq, undefined);

export {
eq ,
}
/* Not a pure module */
20 changes: 20 additions & 0 deletions test/DictTests.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open RescriptCore

let eq = (a, b) => a == b

Test.run(__POS_OF__("make"), Dict.make(), eq, %raw(`{}`))

Test.run(__POS_OF__("fromArray"), Dict.fromArray([("foo", "bar")]), eq, %raw(`{foo: "bar"}`))

Test.run(
__POS_OF__("getUnsafe - existing"),
Dict.fromArray([("foo", "bar")])->Dict.getUnsafe("foo"),
eq,
"bar",
)
Test.run(
__POS_OF__("getUnsafe - missing"),
Dict.make()->Dict.getUnsafe("foo"),
eq,
%raw(`undefined`),
)