ToIndex.js 638 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var $RangeError = require('es-errors/range');
  3. var ToIntegerOrInfinity = require('./ToIntegerOrInfinity');
  4. var ToLength = require('./ToLength');
  5. var SameValue = require('./SameValue');
  6. // https://262.ecma-international.org/8.0/#sec-toindex
  7. module.exports = function ToIndex(value) {
  8. if (typeof value === 'undefined') {
  9. return 0;
  10. }
  11. var integerIndex = ToIntegerOrInfinity(value);
  12. if (integerIndex < 0) {
  13. throw new $RangeError('index must be >= 0');
  14. }
  15. var index = ToLength(integerIndex);
  16. if (!SameValue(integerIndex, index)) {
  17. throw new $RangeError('index must be >= 0 and < 2 ** 53 - 1');
  18. }
  19. return index;
  20. };