source: imaps-frontend/node_modules/es-abstract/2022/SortIndexedProperties.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3var callBound = require('call-bind/callBound');
4
5var $TypeError = require('es-errors/type');
6
7var DeletePropertyOrThrow = require('./DeletePropertyOrThrow');
8var Get = require('./Get');
9var HasProperty = require('./HasProperty');
10var Set = require('./Set');
11var ToString = require('./ToString');
12var Type = require('./Type');
13
14var isAbstractClosure = require('../helpers/isAbstractClosure');
15var isInteger = require('../helpers/isInteger');
16
17var $push = callBound('Array.prototype.push');
18var $sort = callBound('Array.prototype.sort');
19
20// https://262.ecma-international.org/13.0/#sec-sortindexedproperties
21
22module.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};
Note: See TracBrowser for help on using the repository browser.