Skip to content

Commit

Permalink
[Refactor] add SetDataSize
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 6, 2024
1 parent 8fe0b4a commit 714b7a1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"IteratorValue",
"RequireObjectCoercible",
"SetDataHas",
"SetDataSize",
"ToBoolean",

// GetSetRecord
Expand Down
8 changes: 8 additions & 0 deletions aos/SetDataSize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

var tools = require('es-set/tools');
var $setSize = tools.size;

module.exports = function SetDataSize(setData) {
return $setSize(setData);
};
57 changes: 28 additions & 29 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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
};

0 comments on commit 714b7a1

Please sign in to comment.