array-last-index-of.js 1.2 KB

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. /* eslint-disable es/no-array-prototype-lastindexof -- safe */
  3. var toIndexedObject = require('../internals/to-indexed-object');
  4. var toInteger = require('../internals/to-integer');
  5. var toLength = require('../internals/to-length');
  6. var arrayMethodIsStrict = require('../internals/array-method-is-strict');
  7. var min = Math.min;
  8. var $lastIndexOf = [].lastIndexOf;
  9. var NEGATIVE_ZERO = !!$lastIndexOf && 1 / [1].lastIndexOf(1, -0) < 0;
  10. var STRICT_METHOD = arrayMethodIsStrict('lastIndexOf');
  11. var FORCED = NEGATIVE_ZERO || !STRICT_METHOD;
  12. // `Array.prototype.lastIndexOf` method implementation
  13. // https://tc39.es/ecma262/#sec-array.prototype.lastindexof
  14. module.exports = FORCED ? function lastIndexOf(searchElement /* , fromIndex = @[*-1] */) {
  15. // convert -0 to +0
  16. if (NEGATIVE_ZERO) return $lastIndexOf.apply(this, arguments) || 0;
  17. var O = toIndexedObject(this);
  18. var length = toLength(O.length);
  19. var index = length - 1;
  20. if (arguments.length > 1) index = min(index, toInteger(arguments[1]));
  21. if (index < 0) index = length + index;
  22. for (;index >= 0; index--) if (index in O && O[index] === searchElement) return index || 0;
  23. return -1;
  24. } : $lastIndexOf;