SortIndexedProperties.js 1.5 KB

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