esnext.map.key-by.js 554 B

123456789101112131415161718
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var iterate = require('../internals/iterate');
  4. var aFunction = require('../internals/a-function');
  5. // `Map.keyBy` method
  6. // https://github.com/tc39/proposal-collection-methods
  7. $({ target: 'Map', stat: true }, {
  8. keyBy: function keyBy(iterable, keyDerivative) {
  9. var newMap = new this();
  10. aFunction(keyDerivative);
  11. var setter = aFunction(newMap.set);
  12. iterate(iterable, function (element) {
  13. setter.call(newMap, keyDerivative(element), element);
  14. });
  15. return newMap;
  16. }
  17. });