iget.js (1679B)
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 /* eslint-disable no-invalid-this */ 20 21 'use strict'; 22 23 // MODULES // 24 25 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; 26 var getIndex = require( './../../base/ind' ); 27 var parent = require( './../../base/ctor' ); // eslint-disable-line stdlib/no-redeclare 28 29 30 // VARIABLES // 31 32 var base = parent.prototype.iget; 33 34 35 // MAIN // 36 37 /** 38 * Returns an array element located at a specified linear index. 39 * 40 * ## Notes 41 * 42 * - For zero-dimensional arrays, the input argument is ignored and, for clarity, should not be provided. 43 * 44 * @private 45 * @param {integer} [idx] - linear view index 46 * @throws {TypeError} index must be an integer value 47 * @throws {RangeError} index exceeds array dimensions 48 * @returns {*} array element 49 */ 50 function iget( idx ) { 51 if ( this._ndims > 0 ) { 52 if ( !isInteger( idx ) ) { 53 throw new TypeError( 'invalid argument. Index must be an integer value. Value: `'+idx+'`.' ); 54 } 55 idx = getIndex( idx, this._length-1, this._mode ); 56 return base.call( this, idx ); 57 } 58 return base.call( this ); 59 } 60 61 62 // EXPORTS // 63 64 module.exports = iget;