diff --git a/CHANGELOG.md b/CHANGELOG.md index 673798f4..28b4893b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Next version +- Add `Dict.getUnsafe` https://github.com/rescript-association/rescript-core/pull/167 + ## 0.6.0 ### API changes diff --git a/migration/migration.toml b/migration/migration.toml index a5af1a2b..59e23609 100644 --- a/migration/migration.toml +++ b/migration/migration.toml @@ -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" diff --git a/src/Core__Dict.res b/src/Core__Dict.res index c4558bd6..3623a827 100644 --- a/src/Core__Dict.res +++ b/src/Core__Dict.res @@ -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" diff --git a/src/Core__Dict.resi b/src/Core__Dict.resi index 54fef2ad..ec5f1132 100644 --- a/src/Core__Dict.resi +++ b/src/Core__Dict.resi @@ -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. diff --git a/test/DictTests.mjs b/test/DictTests.mjs new file mode 100644 index 00000000..3847e1e3 --- /dev/null +++ b/test/DictTests.mjs @@ -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 */ diff --git a/test/DictTests.res b/test/DictTests.res new file mode 100644 index 00000000..1a66d504 --- /dev/null +++ b/test/DictTests.res @@ -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`), +)