diff --git a/packages/core-js-builder/config.js b/packages/core-js-builder/config.js index 9db3bfc076db..2469dab69b98 100644 --- a/packages/core-js-builder/config.js +++ b/packages/core-js-builder/config.js @@ -168,6 +168,7 @@ module.exports = { 'esnext.weak-map.from', 'esnext.weak-set.from', 'esnext.set.filter', + 'esnext.set.map', 'esnext.global', 'esnext.math.clamp', 'esnext.math.deg-per-rad', diff --git a/packages/core-js/fn/set/index.js b/packages/core-js/fn/set/index.js index ec736de2f7a6..87370f96ae22 100644 --- a/packages/core-js/fn/set/index.js +++ b/packages/core-js/fn/set/index.js @@ -5,5 +5,6 @@ require('../../modules/es.set'); require('../../modules/esnext.set.of'); require('../../modules/esnext.set.from'); require('../../modules/esnext.set.filter'); +require('../../modules/esnext.set.map'); module.exports = require('../../modules/_path').Set; diff --git a/packages/core-js/fn/set/map.js b/packages/core-js/fn/set/map.js new file mode 100644 index 000000000000..d3297c2be57d --- /dev/null +++ b/packages/core-js/fn/set/map.js @@ -0,0 +1,4 @@ +require('../../modules/es.set'); +require('../../modules/esnext.set.map'); + +module.exports = require('../../modules/_entry-unbind')('Set', 'map'); diff --git a/packages/core-js/index.js b/packages/core-js/index.js index e2e1bc8adfd4..5e3af839cd95 100644 --- a/packages/core-js/index.js +++ b/packages/core-js/index.js @@ -165,6 +165,7 @@ require('./modules/esnext.set.from'); require('./modules/esnext.weak-map.from'); require('./modules/esnext.weak-set.from'); require('./modules/esnext.set.filter'); +require('./modules/esnext.set.map'); require('./modules/esnext.global'); require('./modules/esnext.math.clamp'); require('./modules/esnext.math.deg-per-rad'); diff --git a/packages/core-js/modules/esnext.set.map.js b/packages/core-js/modules/esnext.set.map.js new file mode 100644 index 000000000000..0ca4ab0407e2 --- /dev/null +++ b/packages/core-js/modules/esnext.set.map.js @@ -0,0 +1,22 @@ +'use strict'; +var path = require('./_path'); +var anObject = require('core-js-internals/an-object'); +var bind = require('core-js-internals/bind-context'); +var speciesConstructor = require('core-js-internals/species-constructor'); +var Set = path.Set; +var values = Set.prototype.values; + +// https://github.com/Ginden/collection-methods +require('./_export')({ target: 'Set', proto: true, real: true, forced: require('./_is-pure') }, { + map: function map(callbackfn /* , thisArg */) { + var set = anObject(this); + var iterator = values.call(set); + var boundFn = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3); + var newSet = new (speciesConstructor(this, Set))(); + var step, value; + while (!(step = iterator.next()).done) { + newSet.add(boundFn(value = step.value, value, set)); + } + return newSet; + } +}); diff --git a/packages/core-js/stage/pre.js b/packages/core-js/stage/pre.js index f0be6b8f77ec..28bf72e9e575 100644 --- a/packages/core-js/stage/pre.js +++ b/packages/core-js/stage/pre.js @@ -1,4 +1,5 @@ require('../modules/esnext.set.filter'); +require('../modules/esnext.set.map'); require('../modules/esnext.reflect.define-metadata'); require('../modules/esnext.reflect.delete-metadata'); require('../modules/esnext.reflect.get-metadata'); diff --git a/tests/commonjs.js b/tests/commonjs.js index d8fb34693174..261b91c45de9 100644 --- a/tests/commonjs.js +++ b/tests/commonjs.js @@ -278,6 +278,7 @@ for (const PATH of ['../packages/core-js', '../packages/core-js-pure']) { ok(require(`${ PATH }/fn/weak-map/from`)([[{}, 1], [[], 2]]) instanceof require(`${ PATH }/fn/weak-map`)); ok(require(`${ PATH }/fn/weak-set/from`)([{}, []]) instanceof require(`${ PATH }/fn/weak-set`)); ok(require(`${ PATH }/fn/set/filter`)(new (require(`${ PATH }/fn/set`))([1, 2, 3]), it => it % 2).size === 2); + ok(require(`${ PATH }/fn/set/map`)(new (require(`${ PATH }/fn/set`))([1, 2, 3]), it => it % 2).size === 2); ok(typeof require(`${ PATH }/fn/typed/array-buffer`) === 'function'); ok(typeof require(`${ PATH }/fn/typed/data-view`) === 'function'); ok(typeof require(`${ PATH }/fn/typed/int8-array`) === 'function'); diff --git a/tests/pure/esnext.set.map.js b/tests/pure/esnext.set.map.js new file mode 100644 index 000000000000..5c41e1577fab --- /dev/null +++ b/tests/pure/esnext.set.map.js @@ -0,0 +1,33 @@ +import { STRICT } from '../helpers/constants'; + +import Set from 'core-js-pure/fn/set'; +import from from 'core-js-pure/fn/array/from'; + +QUnit.test('Set#map', assert => { + const { map } = Set.prototype; + + assert.isFunction(map); + assert.arity(map, 1); + if ('name' in map) assert.name(map, 'map'); + assert.nonEnumerable(Set.prototype, 'map'); + + const set = new Set([1]); + const context = {}; + set.map(function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 1, 'correct key in callback'); + assert.same(that, set, 'correct link to set in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + + assert.deepEqual(from(new Set([1, 2, 3]).map(it => it ** 2)), [1, 4, 9]); + assert.deepEqual(from(new Set([1, 2, 3]).map(it => it % 2)), [1, 0]); + + assert.throws(() => map.call({}, () => { /* empty */ }), TypeError); + + if (STRICT) { + assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError); + assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError); + } +}); diff --git a/tests/pure/index.js b/tests/pure/index.js index 52efa60dce2f..a163e14cf040 100644 --- a/tests/pure/index.js +++ b/tests/pure/index.js @@ -169,6 +169,7 @@ import './esnext.reflect.has-own-metadata'; import './esnext.reflect.metadata'; import './esnext.set.filter'; import './esnext.set.from'; +import './esnext.set.map'; import './esnext.set.of'; import './esnext.string.at'; import './esnext.string.code-points'; diff --git a/tests/tests/esnext.set.map.js b/tests/tests/esnext.set.map.js new file mode 100644 index 000000000000..771570809e4b --- /dev/null +++ b/tests/tests/esnext.set.map.js @@ -0,0 +1,32 @@ +import { STRICT } from '../helpers/constants'; + +QUnit.test('Set#map', assert => { + const { map } = Set.prototype; + const { from } = Array; + + assert.isFunction(map); + assert.arity(map, 1); + assert.name(map, 'map'); + assert.looksNative(map); + assert.nonEnumerable(Set.prototype, 'map'); + + const set = new Set([1]); + const context = {}; + set.map(function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 1, 'correct key in callback'); + assert.same(that, set, 'correct link to set in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + + assert.deepEqual(from(new Set([1, 2, 3]).map(it => it ** 2)), [1, 4, 9]); + assert.deepEqual(from(new Set([1, 2, 3]).map(it => it % 2)), [1, 0]); + + assert.throws(() => map.call({}, () => { /* empty */ }), TypeError); + + if (STRICT) { + assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError); + assert.throws(() => map.call(undefined, () => { /* empty */ }), TypeError); + } +}); diff --git a/tests/tests/index.js b/tests/tests/index.js index 62f804c37ec1..ff6db48eca74 100644 --- a/tests/tests/index.js +++ b/tests/tests/index.js @@ -206,6 +206,7 @@ import './esnext.reflect.has-own-metadata'; import './esnext.reflect.metadata'; import './esnext.set.filter'; import './esnext.set.from'; +import './esnext.set.map'; import './esnext.set.of'; import './esnext.string.at'; import './esnext.string.code-points';