map-emplace.js 616 B

123456789101112131415161718
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var aCallable = require('../internals/a-callable');
  4. var anObject = require('../internals/an-object');
  5. // `Map.prototype.emplace` method
  6. // https://github.com/thumbsupep/proposal-upsert
  7. module.exports = function emplace(key, handler) {
  8. var map = anObject(this);
  9. var get = aCallable(map.get);
  10. var has = aCallable(map.has);
  11. var set = aCallable(map.set);
  12. var value = (call(has, map, key) && 'update' in handler)
  13. ? handler.update(call(get, map, key), key, map)
  14. : handler.insert(key, map);
  15. call(set, map, key, value);
  16. return value;
  17. };