[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
[79a0317] | 4 | var callBound = require('call-bound');
|
---|
| 5 | var isInteger = require('math-intrinsics/isInteger');
|
---|
[d565449] | 6 |
|
---|
| 7 | var Get = require('./Get');
|
---|
| 8 | var HasProperty = require('./HasProperty');
|
---|
| 9 | var ToString = require('./ToString');
|
---|
| 10 |
|
---|
| 11 | var isAbstractClosure = require('../helpers/isAbstractClosure');
|
---|
[79a0317] | 12 | var isObject = require('../helpers/isObject');
|
---|
[d565449] | 13 |
|
---|
| 14 | var $push = callBound('Array.prototype.push');
|
---|
| 15 | var $sort = callBound('Array.prototype.sort');
|
---|
| 16 |
|
---|
| 17 | // https://262.ecma-international.org/14.0/#sec-sortindexedproperties
|
---|
| 18 |
|
---|
| 19 | module.exports = function SortIndexedProperties(obj, len, SortCompare, holes) {
|
---|
[79a0317] | 20 | if (!isObject(obj)) {
|
---|
[d565449] | 21 | throw new $TypeError('Assertion failed: Type(obj) is not Object');
|
---|
| 22 | }
|
---|
| 23 | if (!isInteger(len) || len < 0) {
|
---|
| 24 | throw new $TypeError('Assertion failed: `len` must be an integer >= 0');
|
---|
| 25 | }
|
---|
| 26 | if (!isAbstractClosure(SortCompare) || SortCompare.length !== 2) {
|
---|
| 27 | throw new $TypeError('Assertion failed: `SortCompare` must be an abstract closure taking 2 arguments');
|
---|
| 28 | }
|
---|
| 29 | if (holes !== 'skip-holes' && holes !== 'read-through-holes') {
|
---|
| 30 | throw new $TypeError('Assertion failed: `holes` must be either `skip-holes` or `read-through-holes`');
|
---|
| 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 kRead = holes === 'skip-holes' ? HasProperty(obj, Pk) : true; // step 3.b - 3.c
|
---|
| 40 | if (kRead) { // step 3.d
|
---|
| 41 | var kValue = Get(obj, Pk);
|
---|
| 42 | $push(items, kValue);
|
---|
| 43 | }
|
---|
| 44 | k += 1; // step 3.e
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | $sort(items, SortCompare); // step 4
|
---|
| 48 |
|
---|
| 49 | return items; // step 5
|
---|
| 50 | };
|
---|