Skip to content

Commit

Permalink
remove sort from Set.prototype.intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Mar 23, 2023
1 parent e4a5bc9 commit 1fd8c86
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Changelog
##### Unreleased
- [`Set` methods proposal](https://github.com/tc39/proposal-set-methods):
- Removed sort from `Set.prototype.intersection`, [March 2023 TC39 meeting](https://github.com/babel/proposals/issues/87#issuecomment-1478610425), [proposal-set-methods/94](https://github.com/tc39/proposal-set-methods/pull/94)
- [`Iterator.range` proposal](https://github.com/tc39/proposal-Number.range) moved to Stage 2, [March 2023 TC39 meeting](https://github.com/babel/proposals/issues/87#issuecomment-1480266760)
- (Async) Explicit Resource Management proposals:
- `(Async)DisposableStack.prototype.move` marks the original stack as disposed, [#1226](https://github.com/zloirock/core-js/issues/1226)
Expand Down
4 changes: 1 addition & 3 deletions packages/core-js-pure/override/internals/set-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ module.exports = {
add: caller('add', 1),
has: caller('has', 1),
remove: caller('delete', 1),
proto: SetPrototype,
$has: SetPrototype.has,
$keys: SetPrototype.keys
proto: SetPrototype
};
4 changes: 1 addition & 3 deletions packages/core-js/internals/set-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ module.exports = {
add: uncurryThis(SetPrototype.add),
has: uncurryThis(SetPrototype.has),
remove: uncurryThis(SetPrototype['delete']),
proto: SetPrototype,
$has: SetPrototype.has,
$keys: SetPrototype.keys
proto: SetPrototype
};
17 changes: 1 addition & 16 deletions packages/core-js/internals/set-intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ var iterateSimple = require('../internals/iterate-simple');
var Set = SetHelpers.Set;
var add = SetHelpers.add;
var has = SetHelpers.has;
var nativeHas = SetHelpers.$has;
var nativeKeys = SetHelpers.$keys;

var isNativeSetRecord = function (record) {
return record.has === nativeHas && record.keys === nativeKeys;
};

// `Set.prototype.intersection` method
// https://github.com/tc39/proposal-set-methods
Expand All @@ -23,19 +17,10 @@ module.exports = function intersection(other) {
var otherRec = getSetRecord(other);
var result = new Set();

// observable side effects
if (!isNativeSetRecord(otherRec) && size(O) > otherRec.size) {
if (size(O) > otherRec.size) {
iterateSimple(otherRec.getIterator(), function (e) {
if (has(O, e)) add(result, e);
});

if (size(result) < 2) return result;

var disordered = result;
result = new Set();
iterateSet(O, function (e) {
if (has(disordered, e)) add(result, e);
});
} else {
iterateSet(O, function (e) {
if (otherRec.includes(e)) add(result, e);
Expand Down
8 changes: 7 additions & 1 deletion packages/core-js/modules/esnext.set.intersection.v2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
var $ = require('../internals/export');
var fails = require('../internals/fails');
var intersection = require('../internals/set-intersection');
var setMethodAcceptSetLike = require('../internals/set-method-accept-set-like');

var INCORRECT = !setMethodAcceptSetLike('intersection') || fails(function () {
// eslint-disable-next-line es/no-array-from, es/no-set -- testing
return Array.from(new Set([1, 2, 3]).intersection(new Set([3, 2]))) != '3,2';
});

// `Set.prototype.intersection` method
// https://github.com/tc39/proposal-set-methods
$({ target: 'Set', proto: true, real: true, forced: !setMethodAcceptSetLike('intersection') }, {
$({ target: 'Set', proto: true, real: true, forced: INCORRECT }, {
intersection: intersection
});
2 changes: 1 addition & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ GLOBAL.tests = {
return Set.from;
},
'esnext.set.intersection.v2': function () {
return Set.prototype.intersection;
return Array.from(new Set([1, 2, 3]).intersection(new Set([3, 2]))) == '3,2';
},
'esnext.set.is-disjoint-from.v2': function () {
return Set.prototype.isDisjointFrom;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-global/esnext.set.intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ QUnit.test('Set#intersection', assert => {
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([4, 5]))), []);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([2, 3, 4]))), [2, 3]);

assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1, 0]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1, 0]))), [1, 2, 3]);

Expand Down
4 changes: 2 additions & 2 deletions tests/unit-pure/esnext.set.intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ QUnit.test('Set#intersection', assert => {
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([4, 5]))), []);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([2, 3, 4]))), [2, 3]);

assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(new Set([3, 2, 1, 0]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2]))), [2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2]))), [3, 2]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1]))), [1, 2, 3]);
assert.deepEqual(from(new Set([1, 2, 3]).intersection(createSetLike([3, 2, 1, 0]))), [1, 2, 3]);

Expand Down

0 comments on commit 1fd8c86

Please sign in to comment.