IsValidIntegerIndex.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var IsDetachedBuffer = require('./IsDetachedBuffer');
  4. var IsIntegralNumber = require('./IsIntegralNumber');
  5. var IsTypedArrayOutOfBounds = require('./IsTypedArrayOutOfBounds');
  6. var MakeTypedArrayWithBufferWitnessRecord = require('./MakeTypedArrayWithBufferWitnessRecord');
  7. var TypedArrayLength = require('./TypedArrayLength');
  8. var isNegativeZero = require('../helpers/isNegativeZero');
  9. var isTypedArray = require('is-typed-array');
  10. var typedArrayBuffer = require('typed-array-buffer');
  11. // https://262.ecma-international.org/15.0/#sec-isvalidintegerindex
  12. module.exports = function IsValidIntegerIndex(O, index) {
  13. if (!isTypedArray(O)) {
  14. throw new $TypeError('Assertion failed: `O` is not a TypedArray object');
  15. }
  16. if (typeof index !== 'number') {
  17. throw new $TypeError('Assertion failed: `index` is not a Number');
  18. }
  19. var buffer = typedArrayBuffer(O);
  20. if (IsDetachedBuffer(buffer)) { return false; } // step 1
  21. if (!IsIntegralNumber(index)) { return false; } // step 2
  22. if (isNegativeZero(index)) { return false; } // step 3
  23. var taRecord = MakeTypedArrayWithBufferWitnessRecord(O, 'UNORDERED'); // step 4
  24. if (IsTypedArrayOutOfBounds(taRecord)) {
  25. return false; // step 6
  26. }
  27. var length = TypedArrayLength(taRecord); // step 7
  28. if (index < 0 || index >= length) { return false; } // step 8
  29. return true; // step 9
  30. };