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