index_of.js (3171B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isnan = require( '@stdlib/assert/is-nan' ); 24 var isCollection = require( '@stdlib/assert/is-collection' ); 25 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 26 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; 27 28 29 // MAIN // 30 31 /** 32 * Returns the first index at which a given element can be found. 33 * 34 * @param {ArrayLike} arr - array-like object 35 * @param {*} searchElement - element to find 36 * @param {integer} [fromIndex] - starting index (if negative, the start index is determined relative to last element) 37 * @throws {TypeError} must provide an array-like object 38 * @throws {TypeError} `fromIndex` must be an integer 39 * @returns {integer} index or -1 40 * 41 * @example 42 * var arr = [ 4, 3, 2, 1 ]; 43 * var idx = indexOf( arr, 3 ); 44 * // returns 1 45 * 46 * @example 47 * var arr = [ 4, 3, 2, 1 ]; 48 * var idx = indexOf( arr, 5 ); 49 * // returns -1 50 * 51 * @example 52 * // Using a `fromIndex`: 53 * var arr = [ 1, 2, 3, 4, 5, 2, 6 ]; 54 * var idx = indexOf( arr, 2, 3 ); 55 * // returns 5 56 * 57 * @example 58 * // `fromIndex` which exceeds `array` length: 59 * var arr = [ 1, 2, 3, 4, 2, 5 ]; 60 * var idx = indexOf( arr, 2, 10 ); 61 * // returns -1 62 * 63 * @example 64 * // Negative `fromIndex`: 65 * var arr = [ 1, 2, 3, 4, 5, 2, 6, 2 ]; 66 * var idx = indexOf( arr, 2, -4 ); 67 * // returns 5 68 * 69 * idx = indexOf( arr, 2, -1 ); 70 * // returns 7 71 * 72 * @example 73 * // Negative `fromIndex` exceeding input `array` length: 74 * var arr = [ 1, 2, 3, 4, 5, 2, 6 ]; 75 * var idx = indexOf( arr, 2, -10 ); 76 * // returns 1 77 * 78 * @example 79 * // Array-like objects: 80 * var str = 'bebop'; 81 * var idx = indexOf( str, 'o' ); 82 * // returns 3 83 */ 84 function indexOf( arr, searchElement, fromIndex ) { 85 var len; 86 var i; 87 if ( !isCollection( arr ) && !isString( arr ) ) { 88 throw new TypeError( 'invalid argument. First argument must be an array-like object. Value: `' + arr + '`.' ); 89 } 90 len = arr.length; 91 if ( len === 0 ) { 92 return -1; 93 } 94 if ( arguments.length === 3 ) { 95 if ( !isInteger( fromIndex ) ) { 96 throw new TypeError( 'invalid argument. `fromIndex` must be an integer. Value: `' + fromIndex + '`.' ); 97 } 98 if ( fromIndex >= 0 ) { 99 if ( fromIndex >= len ) { 100 return -1; 101 } 102 i = fromIndex; 103 } else { 104 i = len + fromIndex; 105 if ( i < 0 ) { 106 i = 0; 107 } 108 } 109 } else { 110 i = 0; 111 } 112 // Check for `NaN`... 113 if ( isnan( searchElement ) ) { 114 for ( ; i < len; i++ ) { 115 if ( isnan( arr[i] ) ) { 116 return i; 117 } 118 } 119 } else { 120 for ( ; i < len; i++ ) { 121 if ( arr[ i ] === searchElement ) { 122 return i; 123 } 124 } 125 } 126 return -1; 127 } 128 129 130 // EXPORTS // 131 132 module.exports = indexOf;