esnext.map.key-by.js 764 B

12345678910111213141516171819202122
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var iterate = require('../internals/iterate');
  5. var isCallable = require('../internals/is-callable');
  6. var aCallable = require('../internals/a-callable');
  7. var Map = require('../internals/map-helpers').Map;
  8. // `Map.keyBy` method
  9. // https://github.com/tc39/proposal-collection-methods
  10. $({ target: 'Map', stat: true, forced: true }, {
  11. keyBy: function keyBy(iterable, keyDerivative) {
  12. var C = isCallable(this) ? this : Map;
  13. var newMap = new C();
  14. aCallable(keyDerivative);
  15. var setter = aCallable(newMap.set);
  16. iterate(iterable, function (element) {
  17. call(setter, newMap, keyDerivative(element), element);
  18. });
  19. return newMap;
  20. }
  21. });