MapHelpers.js 472 B

12345678910111213141516171819202122
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @template K
  8. * @template V
  9. * @param {Map<K, V>} map a map
  10. * @param {K} key the key
  11. * @param {function(): V} computer compute value
  12. * @returns {V} value
  13. */
  14. exports.provide = (map, key, computer) => {
  15. const value = map.get(key);
  16. if (value !== undefined) return value;
  17. const newValue = computer();
  18. map.set(key, newValue);
  19. return newValue;
  20. };