UNPKG

890 BJavaScriptView Raw
1'use strict';
2var aSet = require('../internals/a-set');
3var SetHelpers = require('../internals/set-helpers');
4var size = require('../internals/set-size');
5var getSetRecord = require('../internals/get-set-record');
6var iterateSet = require('../internals/set-iterate');
7var iterateSimple = require('../internals/iterate-simple');
8
9var Set = SetHelpers.Set;
10var add = SetHelpers.add;
11var has = SetHelpers.has;
12
13// `Set.prototype.intersection` method
14// https://github.com/tc39/proposal-set-methods
15module.exports = function intersection(other) {
16 var O = aSet(this);
17 var otherRec = getSetRecord(other);
18 var result = new Set();
19
20 if (size(O) > otherRec.size) {
21 iterateSimple(otherRec.getIterator(), function (e) {
22 if (has(O, e)) add(result, e);
23 });
24 } else {
25 iterateSet(O, function (e) {
26 if (otherRec.includes(e)) add(result, e);
27 });
28 }
29
30 return result;
31};