SortIndexedProperties.js 1.5 KB

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