SortIndexedProperties.js 1.6 KB

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