diff --git a/CHANGELOG.md b/CHANGELOG.md index de05e192..e7c4d996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Next version +- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181 + ## 1.0.0 - Up ReScript dependency to 11+. diff --git a/src/Core__Dict.mjs b/src/Core__Dict.mjs index 8ac967f1..7ff37132 100644 --- a/src/Core__Dict.mjs +++ b/src/Core__Dict.mjs @@ -5,7 +5,30 @@ function $$delete$1(dict, string) { delete(dict[string]); } +function forEach(dict, f) { + Object.values(dict).forEach(function (value) { + f(value); + }); +} + +function forEachWithKey(dict, f) { + Object.entries(dict).forEach(function (param) { + f(param[1], param[0]); + }); +} + +function mapValues(dict, f) { + var target = {}; + forEachWithKey(dict, (function (value, key) { + target[key] = f(value); + })); + return target; +} + export { $$delete$1 as $$delete, + forEach , + forEachWithKey , + mapValues , } /* No side effect */ diff --git a/src/Core__Dict.res b/src/Core__Dict.res index 3623a827..1ea4bab7 100644 --- a/src/Core__Dict.res +++ b/src/Core__Dict.res @@ -23,3 +23,19 @@ let delete = (dict, string) => { @val external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign" @val external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign" + +let forEach = (dict, f) => { + dict->valuesToArray->Core__Array.forEach(value => f(value)) +} + +let forEachWithKey = (dict, f) => { + dict->toArray->Core__Array.forEach(((key, value)) => f(value, key)) +} + +let mapValues = (dict, f) => { + let target = make() + dict->forEachWithKey((value, key) => { + target->set(key, f(value)) + }) + target +} diff --git a/src/Core__Dict.resi b/src/Core__Dict.resi index e3ac21f4..5ca553e1 100644 --- a/src/Core__Dict.resi +++ b/src/Core__Dict.resi @@ -190,3 +190,47 @@ Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray) */ @val external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign" + +/** +`forEach(dictionary, f)` iterates through all values of the dict. + +> Please note that this is *without the keys*, just the values. If you need the key as well, use `Dict.forEachWithKey`. + +## Examples +```rescript +let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")]) + +dict->Dict.forEach(value => { + Console.log(value) +}) +``` +*/ +let forEach: (t<'a>, 'a => unit) => unit + +/** +`forEachWithKey(dictionary, f)` iterates through all values of the dict, including the key for each value. + +## Examples +```rescript +let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")]) + +dict->Dict.forEachWithKey((value, key) => { + Console.log2(value, key) +}) +``` +*/ +let forEachWithKey: (t<'a>, ('a, string) => unit) => unit + +/** +`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary. + +## Examples + +```rescript +let dict = Dict.fromArray([("key1", 1), ("key2", 2)]) + +dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)] +dict->Dict.mapValues(Int.toString)->Dict.toArray // [("key1", "1"), ("key2", "2")] +``` +*/ +let mapValues: (t<'a>, 'a => 'b) => t<'b>