ToIndex.js 620 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var $RangeError = require('es-errors/range');
  3. var ToInteger = require('./ToInteger');
  4. var ToLength = require('./ToLength');
  5. var SameValueZero = require('./SameValueZero');
  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 = ToInteger(value);
  12. if (integerIndex < 0) {
  13. throw new $RangeError('index must be >= 0');
  14. }
  15. var index = ToLength(integerIndex);
  16. if (!SameValueZero(integerIndex, index)) {
  17. throw new $RangeError('index must be >= 0 and < 2 ** 53 - 1');
  18. }
  19. return index;
  20. };