es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
23 lines (20 loc) • 607 B
JavaScript
import { isArrayLike } from '../predicate/isArrayLike.mjs';
function indexOf(array, searchElement, fromIndex) {
if (!isArrayLike(array)) {
return -1;
}
if (Number.isNaN(searchElement)) {
fromIndex = fromIndex ?? 0;
if (fromIndex < 0) {
fromIndex = Math.max(0, array.length + fromIndex);
}
for (let i = fromIndex; i < array.length; i++) {
if (Number.isNaN(array[i])) {
return i;
}
}
return -1;
}
return Array.from(array).indexOf(searchElement, fromIndex);
}
export { indexOf };