IsValidIntegerIndex.js 935 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var IsDetachedBuffer = require('./IsDetachedBuffer');
  5. var IsIntegralNumber = require('./IsIntegralNumber');
  6. var isNegativeZero = require('../helpers/isNegativeZero');
  7. var typedArrayBuffer = require('typed-array-buffer');
  8. // https://262.ecma-international.org/12.0/#sec-isvalidintegerindex
  9. module.exports = function IsValidIntegerIndex(O, index) {
  10. // Assert: O is an Integer-Indexed exotic object.
  11. var buffer = typedArrayBuffer(O); // step 1
  12. if (typeof index !== 'number') {
  13. throw new $TypeError('Assertion failed: Type(index) is not Number');
  14. }
  15. if (IsDetachedBuffer(buffer)) { return false; } // step 2
  16. if (!IsIntegralNumber(index)) { return false; } // step 3
  17. if (isNegativeZero(index)) { return false; } // step 4
  18. if (index < 0 || index >= O.length) { return false; } // step 5
  19. return true; // step 6
  20. };