putIfAbsent.cjs 638 B

1234567891011121314151617181920212223242526
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. /**
  5. * If `map` already has the given `key`, returns its value. Otherwise, calls
  6. * `callback`, adds the result to `map` at `key`, and then returns it.
  7. *
  8. * @template K
  9. * @template V
  10. * @param {Map<K, V>} map
  11. * @param {K} key
  12. * @param {() => V} callback
  13. * @returns {V}
  14. */
  15. function putIfAbsent(map, key, callback) {
  16. if (map.has(key)) return /** @type {V} */ (map.get(key));
  17. const value = callback();
  18. map.set(key, value);
  19. return value;
  20. }
  21. module.exports = putIfAbsent;