133 lines
3.1 KiB
JavaScript
133 lines
3.1 KiB
JavaScript
|
/**
|
||
|
* @license Apache-2.0
|
||
|
*
|
||
|
* Copyright (c) 2018 The Stdlib Authors.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
// MODULES //
|
||
|
|
||
|
var isnan = require( '@stdlib/assert/is-nan' );
|
||
|
var isCollection = require( '@stdlib/assert/is-collection' );
|
||
|
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
|
||
|
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
|
||
|
|
||
|
|
||
|
// MAIN //
|
||
|
|
||
|
/**
|
||
|
* Returns the first index at which a given element can be found.
|
||
|
*
|
||
|
* @param {ArrayLike} arr - array-like object
|
||
|
* @param {*} searchElement - element to find
|
||
|
* @param {integer} [fromIndex] - starting index (if negative, the start index is determined relative to last element)
|
||
|
* @throws {TypeError} must provide an array-like object
|
||
|
* @throws {TypeError} `fromIndex` must be an integer
|
||
|
* @returns {integer} index or -1
|
||
|
*
|
||
|
* @example
|
||
|
* var arr = [ 4, 3, 2, 1 ];
|
||
|
* var idx = indexOf( arr, 3 );
|
||
|
* // returns 1
|
||
|
*
|
||
|
* @example
|
||
|
* var arr = [ 4, 3, 2, 1 ];
|
||
|
* var idx = indexOf( arr, 5 );
|
||
|
* // returns -1
|
||
|
*
|
||
|
* @example
|
||
|
* // Using a `fromIndex`:
|
||
|
* var arr = [ 1, 2, 3, 4, 5, 2, 6 ];
|
||
|
* var idx = indexOf( arr, 2, 3 );
|
||
|
* // returns 5
|
||
|
*
|
||
|
* @example
|
||
|
* // `fromIndex` which exceeds `array` length:
|
||
|
* var arr = [ 1, 2, 3, 4, 2, 5 ];
|
||
|
* var idx = indexOf( arr, 2, 10 );
|
||
|
* // returns -1
|
||
|
*
|
||
|
* @example
|
||
|
* // Negative `fromIndex`:
|
||
|
* var arr = [ 1, 2, 3, 4, 5, 2, 6, 2 ];
|
||
|
* var idx = indexOf( arr, 2, -4 );
|
||
|
* // returns 5
|
||
|
*
|
||
|
* idx = indexOf( arr, 2, -1 );
|
||
|
* // returns 7
|
||
|
*
|
||
|
* @example
|
||
|
* // Negative `fromIndex` exceeding input `array` length:
|
||
|
* var arr = [ 1, 2, 3, 4, 5, 2, 6 ];
|
||
|
* var idx = indexOf( arr, 2, -10 );
|
||
|
* // returns 1
|
||
|
*
|
||
|
* @example
|
||
|
* // Array-like objects:
|
||
|
* var str = 'bebop';
|
||
|
* var idx = indexOf( str, 'o' );
|
||
|
* // returns 3
|
||
|
*/
|
||
|
function indexOf( arr, searchElement, fromIndex ) {
|
||
|
var len;
|
||
|
var i;
|
||
|
if ( !isCollection( arr ) && !isString( arr ) ) {
|
||
|
throw new TypeError( 'invalid argument. First argument must be an array-like object. Value: `' + arr + '`.' );
|
||
|
}
|
||
|
len = arr.length;
|
||
|
if ( len === 0 ) {
|
||
|
return -1;
|
||
|
}
|
||
|
if ( arguments.length === 3 ) {
|
||
|
if ( !isInteger( fromIndex ) ) {
|
||
|
throw new TypeError( 'invalid argument. `fromIndex` must be an integer. Value: `' + fromIndex + '`.' );
|
||
|
}
|
||
|
if ( fromIndex >= 0 ) {
|
||
|
if ( fromIndex >= len ) {
|
||
|
return -1;
|
||
|
}
|
||
|
i = fromIndex;
|
||
|
} else {
|
||
|
i = len + fromIndex;
|
||
|
if ( i < 0 ) {
|
||
|
i = 0;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
i = 0;
|
||
|
}
|
||
|
// Check for `NaN`...
|
||
|
if ( isnan( searchElement ) ) {
|
||
|
for ( ; i < len; i++ ) {
|
||
|
if ( isnan( arr[i] ) ) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
for ( ; i < len; i++ ) {
|
||
|
if ( arr[ i ] === searchElement ) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
|
||
|
// EXPORTS //
|
||
|
|
||
|
module.exports = indexOf;
|