[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var callBound = require('call-bind/callBound');
|
---|
| 4 |
|
---|
| 5 | var $TypeError = require('es-errors/type');
|
---|
| 6 |
|
---|
| 7 | var DeletePropertyOrThrow = require('./DeletePropertyOrThrow');
|
---|
| 8 | var Get = require('./Get');
|
---|
| 9 | var HasProperty = require('./HasProperty');
|
---|
| 10 | var Set = require('./Set');
|
---|
| 11 | var ToString = require('./ToString');
|
---|
| 12 | var Type = require('./Type');
|
---|
| 13 |
|
---|
| 14 | var isAbstractClosure = require('../helpers/isAbstractClosure');
|
---|
| 15 | var isInteger = require('../helpers/isInteger');
|
---|
| 16 |
|
---|
| 17 | var $push = callBound('Array.prototype.push');
|
---|
| 18 | var $sort = callBound('Array.prototype.sort');
|
---|
| 19 |
|
---|
| 20 | // https://262.ecma-international.org/13.0/#sec-sortindexedproperties
|
---|
| 21 |
|
---|
| 22 | module.exports = function SortIndexedProperties(obj, len, SortCompare) {
|
---|
| 23 | if (Type(obj) !== 'Object') {
|
---|
| 24 | throw new $TypeError('Assertion failed: Type(obj) is not Object');
|
---|
| 25 | }
|
---|
| 26 | if (!isInteger(len) || len < 0) {
|
---|
| 27 | throw new $TypeError('Assertion failed: `len` must be an integer >= 0');
|
---|
| 28 | }
|
---|
| 29 | if (!isAbstractClosure(SortCompare) || SortCompare.length !== 2) {
|
---|
| 30 | throw new $TypeError('Assertion failed: `SortCompare` must be an abstract closure taking 2 arguments');
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | var items = []; // step 1
|
---|
| 34 |
|
---|
| 35 | var k = 0; // step 2
|
---|
| 36 |
|
---|
| 37 | while (k < len) { // step 3
|
---|
| 38 | var Pk = ToString(k);
|
---|
| 39 | var kPresent = HasProperty(obj, Pk);
|
---|
| 40 | if (kPresent) {
|
---|
| 41 | var kValue = Get(obj, Pk);
|
---|
| 42 | $push(items, kValue);
|
---|
| 43 | }
|
---|
| 44 | k += 1;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | var itemCount = items.length; // step 4
|
---|
| 48 |
|
---|
| 49 | $sort(items, SortCompare); // step 5
|
---|
| 50 |
|
---|
| 51 | var j = 0; // step 6
|
---|
| 52 |
|
---|
| 53 | while (j < itemCount) { // step 7
|
---|
| 54 | Set(obj, ToString(j), items[j], true);
|
---|
| 55 | j += 1;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | while (j < len) { // step 8
|
---|
| 59 | DeletePropertyOrThrow(obj, ToString(j));
|
---|
| 60 | j += 1;
|
---|
| 61 | }
|
---|
| 62 | return obj; // step 9
|
---|
| 63 | };
|
---|