IsValidIntegerIndex.js 887 B

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