diff --git a/.eslintrc b/.eslintrc index 0c4d34b..16ac482 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,6 +14,7 @@ "IteratorValue", "RequireObjectCoercible", "SetDataHas", + "SetDataSize", "ToBoolean", // GetSetRecord diff --git a/aos/SetDataSize.js b/aos/SetDataSize.js new file mode 100644 index 0000000..a99e534 --- /dev/null +++ b/aos/SetDataSize.js @@ -0,0 +1,8 @@ +'use strict'; + +var tools = require('es-set/tools'); +var $setSize = tools.size; + +module.exports = function SetDataSize(setData) { + return $setSize(setData); +}; diff --git a/implementation.js b/implementation.js index ee81c7a..85a8318 100644 --- a/implementation.js +++ b/implementation.js @@ -1,6 +1,6 @@ 'use strict'; -// Note: the commented out code is because there is no performant way in userland to do the optimization in step 7. +// Note: the commented out code is because there is no performant way in userland to do the optimization in step 6. var $TypeError = require('es-errors/type'); @@ -12,6 +12,7 @@ var GetSetRecord = require('./aos/GetSetRecord'); var IteratorStep = require('es-abstract/2023/IteratorStep'); var IteratorValue = require('es-abstract/2023/IteratorValue'); var SetDataHas = require('./aos/SetDataHas'); +var SetDataSize = require('./aos/SetDataSize'); var ToBoolean = require('es-abstract/2023/ToBoolean'); var forEach = require('es-abstract/helpers/forEach'); @@ -23,7 +24,6 @@ var isSet = require('is-set'); var tools = require('es-set/tools'); var $setForEach = tools.forEach; var $setAdd = tools.add; -var $setSize = tools.size; var $push = callBound('Array.prototype.push'); var $setHas = callBind($Set.prototype.has); @@ -40,50 +40,49 @@ module.exports = function intersection(other) { var resultSetData = []; // step 4 - var thisSize = $setSize(O); // step 5 - - if (thisSize <= otherRec['[[Size]]']) { // step 6 - var index = 0; // step 6.a + var thisSize = SetDataSize(O); // step 5.a + if (thisSize <= otherRec['[[Size]]']) { // step 5 + var index = 0; // step 5.b $setForEach(O, function (e) { - if (index < thisSize) { // step 6.b - index += 1; // step 6.b.ii - var inOther = ToBoolean(Call(otherRec['[[Has]]'], otherRec['[[Set]]'], [e])); // step 6.b.iii.1 - if (inOther) { // step 6.b.iii.2 - var alreadyInResult = SetDataHas(resultSetData, e); // step 6.b.iii.2.b - if (!alreadyInResult) { // step 6.b.iii.2.c - $push(resultSetData, e); // step 6.b.iii.c.i + if (index < thisSize) { // step 5.c + index += 1; // step 5.c.ii + var inOther = ToBoolean(Call(otherRec['[[Has]]'], otherRec['[[Set]]'], [e])); // step 5.c.iii.1 + if (inOther) { // step 6.c.iii.2 + var alreadyInResult = SetDataHas(resultSetData, e); // step 5.c.iii.2.b + if (!alreadyInResult) { // step 5.c.iii.2.c + $push(resultSetData, e); // step 5.c.iii.c.i thisSize += 1; } } } }); - } else { // step 7 - var keysIter = GetIteratorFromMethod(otherRec['[[Set]]'], otherRec['[[Keys]]']); // step 7.a - var next = true; // step 7.b - while (next) { // step 7.c - next = IteratorStep(keysIter); // step 7.c.i - if (next) { // step 7.c.ii - var nextValue = IteratorValue(next); // step 7.c.ii.1 - - if (nextValue === 0) { // step 7.c.ii.2 + } else { // step 6 + var keysIter = GetIteratorFromMethod(otherRec['[[Set]]'], otherRec['[[Keys]]']); // step 6.a + var next = true; // step 6.b + while (next) { // step 6.c + next = IteratorStep(keysIter); // step 6.c.i + if (next) { // step 6.c.ii + var nextValue = IteratorValue(next); // step 6.c.ii.1 + + if (nextValue === 0) { // step 6.c.ii.2 nextValue = +0; } - var alreadyInResult = SetDataHas(resultSetData, nextValue); // step 7.c.ii.4 - var inThis = $setHas(O, nextValue); // step 7.c.ii.5 - if (!alreadyInResult && inThis) { // step 7.c.ii.6 - $push(resultSetData, nextValue); // step 7.c.ii.6.a + var alreadyInResult = SetDataHas(resultSetData, nextValue); // step 6.c.ii.4 + var inThis = $setHas(O, nextValue); // step 6.c.ii.5 + if (!alreadyInResult && inThis) { // step 6.c.ii.6 + $push(resultSetData, nextValue); // step 6.c.ii.6.a } } } } - // var result = OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »); // step 8 + // var result = OrdinaryObjectCreate(%Set.prototype%, « [[SetData]] »); // step 7 var result = new $Set(); - // result.[[SetData]] = resultSetData; // step 9 + // result.[[SetData]] = resultSetData; // step 8 forEach(resultSetData, function (e) { $setAdd(result, e); }); - return result; // step 10 + return result; // step 9 };