-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] update implementation to match latest spec
Closes #3
- Loading branch information
Showing
2 changed files
with
24 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |