IsValidIntegerIndex.js 797 B

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