Skip to content

Commit

Permalink
[Refactor] update implementation to match latest spec
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
ljharb committed Jun 3, 2024
1 parent 7d32e67 commit 3810f00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"new-cap": ["error", {
"capIsNewExceptions": [
"ArrayCreate",
"CompareArrayElements",
"CreateDataPropertyOrThrow",
"GetIntrinsic",
"IsCallable",
"LengthOfArrayLike",
"SortIndexedProperties",
"ToObject",
"ToString",
],
Expand Down
40 changes: 22 additions & 18 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
'use strict';

var callBound = require('call-bind/callBound');

var ArrayCreate = require('es-abstract/2023/ArrayCreate');
var CreateDataPropertyOrThrow = require('es-abstract/2023/CreateDataPropertyOrThrow');
var IsCallable = require('es-abstract/2023/IsCallable');
var LengthOfArrayLike = require('es-abstract/2023/LengthOfArrayLike');
var ToObject = require('es-abstract/2023/ToObject');
var ToString = require('es-abstract/2023/ToString');
var ArrayCreate = require('es-abstract/2024/ArrayCreate');
var CompareArrayElements = require('es-abstract/2024/CompareArrayElements');
var CreateDataPropertyOrThrow = require('es-abstract/2024/CreateDataPropertyOrThrow');
var IsCallable = require('es-abstract/2024/IsCallable');
var LengthOfArrayLike = require('es-abstract/2024/LengthOfArrayLike');
var SortIndexedProperties = require('es-abstract/2024/SortIndexedProperties');
var ToObject = require('es-abstract/2024/ToObject');
var ToString = require('es-abstract/2024/ToString');

var $TypeError = require('es-errors/type');

var $sort = callBound('Array.prototype.sort');

module.exports = function toSorted(comparefn) {
if (typeof comparefn !== 'undefined' && !IsCallable(comparefn)) {
throw new $TypeError('`comparefn` must be a function');
throw new $TypeError('`comparefn` must be a function'); // step 1
}

var O = ToObject(this); // step 2
var len = LengthOfArrayLike(O); // step 3
var A = ArrayCreate(len); // step 4
var j = 0;
while (j < len) { // steps 5-7, 9-10
CreateDataPropertyOrThrow(A, ToString(j), O[j]);
j += 1;
}

$sort(A, comparefn); // step 8
// eslint-disable-next-line no-sequences
var SortCompare = (0, function (x, y) { // step 5
return CompareArrayElements(x, y, comparefn); // step 5.a
});

var sortedList = SortIndexedProperties(O, len, SortCompare, 'read-through-holes'); // step 6

var j = 0; // step 7
while (j < len) { // step 8
CreateDataPropertyOrThrow(A, ToString(j), sortedList[j]); // step 8.a
j += 1; // step 8.b
}

return A; // step 11
return A; // step 9
};

0 comments on commit 3810f00

Please sign in to comment.