UNPKG

471 BJavaScriptView Raw
1'use strict';
2
3/**
4 * If `map` already has the given `key`, returns its value. Otherwise, calls
5 * `callback`, adds the result to `map` at `key`, and then returns it.
6 *
7 * @template K
8 * @template V
9 * @param {Map<K, V>} map
10 * @param {K} key
11 * @param {() => V} callback
12 * @returns {V}
13 */
14module.exports = function (map, key, callback) {
15 if (map.has(key)) return /** @type {V} */ (map.get(key));
16
17 const value = callback();
18
19 map.set(key, value);
20
21 return value;
22};