Skip to content

Commit

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

module.exports = require('../../modules/_entry-unbind')('Set', 'find');
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,6 +5,7 @@ require('../../modules/es.set');
require('../../modules/esnext.set.of');
require('../../modules/esnext.set.from');
require('../../modules/esnext.set.filter');
require('../../modules/esnext.set.find');
require('../../modules/esnext.set.map');

module.exports = require('../../modules/_path').Set;
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.find');
require('./modules/esnext.set.map');
require('./modules/esnext.global');
require('./modules/esnext.math.clamp');
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.set.filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var path = require('./_path');
var anObject = require('core-js-internals/an-object');
var aFunction = require('core-js-internals/a-function');
var bind = require('core-js-internals/bind-context');
var speciesConstructor = require('core-js-internals/species-constructor');
var Set = path.Set;
Expand All @@ -13,9 +14,10 @@ require('./_export')({ target: 'Set', proto: true, real: true, forced: require('
var iterator = values.call(set);
var boundFn = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3);
var newSet = new (speciesConstructor(this, Set))();
var adder = aFunction(newSet.add);
var step, value;
while (!(step = iterator.next()).done) {
if (boundFn(value = step.value, value, set)) newSet.add(value);
if (boundFn(value = step.value, value, set)) adder.call(newSet, value);
}
return newSet;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/core-js/modules/esnext.set.find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var path = require('./_path');
var anObject = require('core-js-internals/an-object');
var bind = require('core-js-internals/bind-context');
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') }, {
find: function find(callbackfn /* , thisArg */) {
var set = anObject(this);
var iterator = values.call(set);
var boundFn = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3);
var step, value;
while (!(step = iterator.next()).done) {
if (boundFn(value = step.value, value, set)) return value;
}
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.set.map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var path = require('./_path');
var anObject = require('core-js-internals/an-object');
var aFunction = require('core-js-internals/a-function');
var bind = require('core-js-internals/bind-context');
var speciesConstructor = require('core-js-internals/species-constructor');
var Set = path.Set;
Expand All @@ -13,9 +14,10 @@ require('./_export')({ target: 'Set', proto: true, real: true, forced: require('
var iterator = values.call(set);
var boundFn = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3);
var newSet = new (speciesConstructor(this, Set))();
var adder = aFunction(newSet.add);
var step, value;
while (!(step = iterator.next()).done) {
newSet.add(boundFn(value = step.value, value, set));
adder.call(newSet, boundFn(value = step.value, value, set));
}
return newSet;
}
Expand Down
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.find');
require('../modules/esnext.set.map');
require('../modules/esnext.reflect.define-metadata');
require('../modules/esnext.reflect.delete-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/find`)(new (require(`${ PATH }/fn/set`))([2, 3, 4]), it => it % 2) === 3);
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');
Expand Down
31 changes: 31 additions & 0 deletions tests/pure/esnext.set.find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { STRICT } from '../helpers/constants';

import Set from 'core-js-pure/fn/set';

QUnit.test('Set#find', assert => {
const { find } = Set.prototype;

assert.isFunction(find);
assert.arity(find, 1);
if ('name' in find) assert.name(find, 'find');
assert.nonEnumerable(Set.prototype, 'find');

const set = new Set([1]);
const context = {};
set.find(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.same(new Set([2, 3, 4]).find(it => it % 2), 3);

assert.throws(() => find.call({}, () => { /* empty */ }), TypeError);

if (STRICT) {
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.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 @@ -168,6 +168,7 @@ import './esnext.reflect.has-metadata';
import './esnext.reflect.has-own-metadata';
import './esnext.reflect.metadata';
import './esnext.set.filter';
import './esnext.set.find';
import './esnext.set.from';
import './esnext.set.map';
import './esnext.set.of';
Expand Down
30 changes: 30 additions & 0 deletions tests/tests/esnext.set.find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { STRICT } from '../helpers/constants';

QUnit.test('Set#find', assert => {
const { find } = Set.prototype;

assert.isFunction(find);
assert.arity(find, 1);
assert.name(find, 'find');
assert.looksNative(find);
assert.nonEnumerable(Set.prototype, 'find');

const set = new Set([1]);
const context = {};
set.find(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.same(new Set([2, 3, 4]).find(it => it % 2), 3);

assert.throws(() => find.call({}, () => { /* empty */ }), TypeError);

if (STRICT) {
assert.throws(() => find.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => find.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 @@ -205,6 +205,7 @@ import './esnext.reflect.has-metadata';
import './esnext.reflect.has-own-metadata';
import './esnext.reflect.metadata';
import './esnext.set.filter';
import './esnext.set.find';
import './esnext.set.from';
import './esnext.set.map';
import './esnext.set.of';
Expand Down

0 comments on commit b01cce5

Please sign in to comment.