map-fns
Version:
<h1 align="center"> <code>map-fns</code> </h1>
31 lines (29 loc) • 807 B
JavaScript
/**
* Creates a new map populated with every key in `keys` where the value
* behind each `k` in `keys` is `map[k]`.
*
* ```tsx
* partialMap({ a: 1, b: 2, c: 3 }, ["a", "c"]);
* //=> {
* // a: 1,
* // c: 3,
* // }
* ```
*
* @param map The original map.
* @param keys The keys to include in the new map.
* @returns a new `map` populated with every key in `keys` where the value
* behind each `k` in `keys` is `map[k]`.
*/
function partialMap(map, keys) {
var obj = {};
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
if (!map.hasOwnProperty(key)) {
throw new Error("Key '".concat(key, "' does not exist in map."));
}
obj[key] = map[key];
}
return obj;
}
export { partialMap as default };