Skip to content

Commit

Permalink
extract aSet from Set helpers for better tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 15, 2022
1 parent f66a90c commit 8c85784
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 69 deletions.
8 changes: 8 additions & 0 deletions packages/core-js-pure/override/internals/a-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var anObject = require('../internals/an-object');
var tryToString = require('../internals/try-to-string');

module.exports = function (it) {
anObject(it);
if ('size' in it && 'has' in it && 'add' in it && 'delete' in it && 'keys' in it) return it;
throw TypeError(tryToString(it) + ' is not a set');
};
9 changes: 0 additions & 9 deletions packages/core-js-pure/override/internals/set-helpers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
var getBuiltIn = require('../internals/get-built-in');
var anObject = require('../internals/an-object');
var tryToString = require('../internals/try-to-string');
var caller = require('../internals/caller');

var Set = getBuiltIn('Set');
var SetPrototype = Set.prototype;

var aSet = function (it) {
anObject(it);
if ('size' in it && 'has' in it && 'add' in it && 'delete' in it && 'keys' in it) return it;
throw TypeError(tryToString(it) + ' is not a set');
};

module.exports = {
Set: Set,
aSet: aSet,
add: caller('add', 1),
has: caller('has', 1),
remove: caller('delete', 1),
Expand Down
6 changes: 6 additions & 0 deletions packages/core-js/internals/a-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var has = require('../internals/set-helpers').has;

module.exports = function (it) {
has(it);
return it;
};
6 changes: 3 additions & 3 deletions packages/core-js/internals/set-difference.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
var aSet = require('../internals/a-set');
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');
var clone = require('../internals/set-clone');
var size = require('../internals/set-size');
var getSetRecord = require('../internals/get-set-record');
var iterateSet = require('../internals/set-iterate');
var iterateSimple = require('../internals/iterate-simple');

var aSet = SetHelpers.aSet;
var has = SetHelpers.has;
var remove = SetHelpers.remove;

Expand All @@ -16,7 +16,7 @@ module.exports = function difference(other) {
var O = aSet(this);
var otherRec = getSetRecord(other);
var result = clone(O);
if (size(O) <= otherRec.size) iterate(O, function (e) {
if (size(O) <= otherRec.size) iterateSet(O, function (e) {
if (otherRec.includes(e)) remove(result, e);
});
else iterateSimple(otherRec.getIterator(), function (e) {
Expand Down
12 changes: 2 additions & 10 deletions packages/core-js/internals/set-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ var uncurryThis = require('../internals/function-uncurry-this');

// eslint-disable-next-line es/no-set -- safe
var SetPrototype = Set.prototype;
var $has = SetPrototype.has;
var has = uncurryThis($has);

var aSet = function (it) {
has(it);
return it;
};

module.exports = {
// eslint-disable-next-line es/no-set -- safe
Set: Set,
aSet: aSet,
add: uncurryThis(SetPrototype.add),
has: has,
has: uncurryThis(SetPrototype.has),
remove: uncurryThis(SetPrototype['delete']),
proto: SetPrototype,
$has: $has,
$has: SetPrototype.has,
$keys: SetPrototype.keys
};
8 changes: 4 additions & 4 deletions packages/core-js/internals/set-intersection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
var aSet = require('../internals/a-set');
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');
var size = require('../internals/set-size');
var getSetRecord = require('../internals/get-set-record');
var iterateSet = require('../internals/set-iterate');
var iterateSimple = require('../internals/iterate-simple');

var Set = SetHelpers.Set;
var aSet = SetHelpers.aSet;
var add = SetHelpers.add;
var has = SetHelpers.has;
var nativeHas = SetHelpers.$has;
Expand All @@ -33,11 +33,11 @@ module.exports = function intersection(other) {

var disordered = result;
result = new Set();
iterate(O, function (e) {
iterateSet(O, function (e) {
if (has(disordered, e)) add(result, e);
});
} else {
iterate(O, function (e) {
iterateSet(O, function (e) {
if (otherRec.includes(e)) add(result, e);
});
}
Expand Down
10 changes: 4 additions & 6 deletions packages/core-js/internals/set-is-disjoint-from.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict';
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');
var aSet = require('../internals/a-set');
var has = require('../internals/set-helpers').has;
var size = require('../internals/set-size');
var getSetRecord = require('../internals/get-set-record');
var iterateSet = require('../internals/set-iterate');
var iterateSimple = require('../internals/iterate-simple');

var aSet = SetHelpers.aSet;
var has = SetHelpers.has;

// `Set.prototype.isDisjointFrom` method
// https://tc39.github.io/proposal-set-methods/#Set.prototype.isDisjointFrom
module.exports = function isDisjointFrom(other) {
var O = aSet(this);
var otherRec = getSetRecord(other);
return false !== (size(O) <= otherRec.size
? iterate(O, function (e) {
? iterateSet(O, function (e) {
if (otherRec.includes(e)) return false;
}, true)
: iterateSimple(otherRec.getIterator(), function (e) {
Expand Down
6 changes: 2 additions & 4 deletions packages/core-js/internals/set-is-subset-of.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');
var aSet = require('../internals/a-set');
var size = require('../internals/set-size');
var iterate = require('../internals/set-iterate');
var getSetRecord = require('../internals/get-set-record');

var aSet = SetHelpers.aSet;

// `Set.prototype.isSubsetOf` method
// https://tc39.github.io/proposal-set-methods/#Set.prototype.isSubsetOf
module.exports = function isSubsetOf(other) {
Expand Down
6 changes: 2 additions & 4 deletions packages/core-js/internals/set-is-superset-of.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var has = require('../internals/set-helpers').has;
var size = require('../internals/set-size');
var getSetRecord = require('../internals/get-set-record');
var iterateSimple = require('../internals/iterate-simple');

var aSet = SetHelpers.aSet;
var has = SetHelpers.has;

// `Set.prototype.isSupersetOf` method
// https://tc39.github.io/proposal-set-methods/#Set.prototype.isSupersetOf
module.exports = function isSupersetOf(other) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/set-symmetric-difference.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
var aSet = require('../internals/a-set');
var SetHelpers = require('../internals/set-helpers');
var clone = require('../internals/set-clone');
var getSetRecord = require('../internals/get-set-record');
var iterateSimple = require('../internals/iterate-simple');

var aSet = SetHelpers.aSet;
var add = SetHelpers.add;
var has = SetHelpers.has;
var remove = SetHelpers.remove;
Expand Down
6 changes: 2 additions & 4 deletions packages/core-js/internals/set-union.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var add = require('../internals/set-helpers').add;
var clone = require('../internals/set-clone');
var getSetRecord = require('../internals/get-set-record');
var iterateSimple = require('../internals/iterate-simple');

var aSet = SetHelpers.aSet;
var add = SetHelpers.add;

// `Set.prototype.union` method
// https://github.com/tc39/proposal-set-methods
module.exports = function union(other) {
Expand Down
6 changes: 2 additions & 4 deletions packages/core-js/modules/esnext.set.add-all.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
var $ = require('../internals/export');
var SetHelpers = require('../internals/set-helpers');

var aSet = SetHelpers.aSet;
var add = SetHelpers.add;
var aSet = require('../internals/a-set');
var add = require('../internals/set-helpers').add;

// `Set.prototype.addAll` method
// https://github.com/tc39/proposal-collection-methods
Expand Down
6 changes: 2 additions & 4 deletions packages/core-js/modules/esnext.set.delete-all.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
var $ = require('../internals/export');
var SetHelpers = require('../internals/set-helpers');

var aSet = SetHelpers.aSet;
var remove = SetHelpers.remove;
var aSet = require('../internals/a-set');
var remove = require('../internals/set-helpers').remove;

// `Set.prototype.deleteAll` method
// https://github.com/tc39/proposal-collection-methods
Expand Down
4 changes: 1 addition & 3 deletions packages/core-js/modules/esnext.set.every.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
var $ = require('../internals/export');
var bind = require('../internals/function-bind-context');
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var iterate = require('../internals/set-iterate');

var aSet = SetHelpers.aSet;

// `Set.prototype.every` method
// https://github.com/tc39/proposal-collection-methods
$({ target: 'Set', proto: true, real: true, forced: true }, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.set.filter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
var $ = require('../internals/export');
var bind = require('../internals/function-bind-context');
var aSet = require('../internals/a-set');
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');

var Set = SetHelpers.Set;
var aSet = SetHelpers.aSet;
var add = SetHelpers.add;

// `Set.prototype.filter` method
Expand Down
4 changes: 1 addition & 3 deletions packages/core-js/modules/esnext.set.find.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
var $ = require('../internals/export');
var bind = require('../internals/function-bind-context');
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var iterate = require('../internals/set-iterate');

var aSet = SetHelpers.aSet;

// `Set.prototype.find` method
// https://github.com/tc39/proposal-collection-methods
$({ target: 'Set', proto: true, real: true, forced: true }, {
Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/modules/esnext.set.join.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var toString = require('../internals/to-string');
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var iterate = require('../internals/set-iterate');
var toString = require('../internals/to-string');

var aSet = SetHelpers.aSet;
var arrayJoin = uncurryThis([].join);
var push = uncurryThis([].push);

Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.set.map.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
var $ = require('../internals/export');
var bind = require('../internals/function-bind-context');
var aSet = require('../internals/a-set');
var SetHelpers = require('../internals/set-helpers');
var iterate = require('../internals/set-iterate');

var Set = SetHelpers.Set;
var aSet = SetHelpers.aSet;
var add = SetHelpers.add;

// `Set.prototype.map` method
Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/modules/esnext.set.reduce.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
var $ = require('../internals/export');
var aCallable = require('../internals/a-callable');
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var iterate = require('../internals/set-iterate');

var $TypeError = TypeError;
var aSet = SetHelpers.aSet;

// `Set.prototype.reduce` method
// https://github.com/tc39/proposal-collection-methods
Expand Down
4 changes: 1 addition & 3 deletions packages/core-js/modules/esnext.set.some.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
var $ = require('../internals/export');
var bind = require('../internals/function-bind-context');
var SetHelpers = require('../internals/set-helpers');
var aSet = require('../internals/a-set');
var iterate = require('../internals/set-iterate');

var aSet = SetHelpers.aSet;

// `Set.prototype.some` method
// https://github.com/tc39/proposal-collection-methods
$({ target: 'Set', proto: true, real: true, forced: true }, {
Expand Down

0 comments on commit 8c85784

Please sign in to comment.