UNPKG

1.11 kBJavaScriptView Raw
1'use strict';
2var toIndexedObject = require('../internals/to-indexed-object');
3var toInteger = require('../internals/to-integer');
4var toLength = require('../internals/to-length');
5var sloppyArrayMethod = require('../internals/sloppy-array-method');
6
7var min = Math.min;
8var nativeLastIndexOf = [].lastIndexOf;
9var NEGATIVE_ZERO = !!nativeLastIndexOf && 1 / [1].lastIndexOf(1, -0) < 0;
10var SLOPPY_METHOD = sloppyArrayMethod('lastIndexOf');
11
12// `Array.prototype.lastIndexOf` method implementation
13// https://tc39.github.io/ecma262/#sec-array.prototype.lastindexof
14module.exports = (NEGATIVE_ZERO || SLOPPY_METHOD) ? function lastIndexOf(searchElement /* , fromIndex = @[*-1] */) {
15 // convert -0 to +0
16 if (NEGATIVE_ZERO) return nativeLastIndexOf.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} : nativeLastIndexOf;