to-index.js 507 B

123456789101112131415
  1. var global = require('../internals/global');
  2. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  3. var toLength = require('../internals/to-length');
  4. var RangeError = global.RangeError;
  5. // `ToIndex` abstract operation
  6. // https://tc39.es/ecma262/#sec-toindex
  7. module.exports = function (it) {
  8. if (it === undefined) return 0;
  9. var number = toIntegerOrInfinity(it);
  10. var length = toLength(number);
  11. if (number !== length) throw RangeError('Wrong length or index');
  12. return length;
  13. };