Skip to content

Commit

Permalink
add Set#map
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Feb 16, 2018
1 parent b036828 commit 600cdbe
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core-js-builder/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/fn/set/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions packages/core-js/fn/set/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/es.set');
require('../../modules/esnext.set.map');

module.exports = require('../../modules/_entry-unbind')('Set', 'map');
1 change: 1 addition & 0 deletions packages/core-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 22 additions & 0 deletions packages/core-js/modules/esnext.set.map.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
1 change: 1 addition & 0 deletions packages/core-js/stage/pre.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
1 change: 1 addition & 0 deletions tests/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
33 changes: 33 additions & 0 deletions tests/pure/esnext.set.map.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
1 change: 1 addition & 0 deletions tests/pure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
32 changes: 32 additions & 0 deletions tests/tests/esnext.set.map.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
1 change: 1 addition & 0 deletions tests/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 600cdbe

Please sign in to comment.