All files main.js

100% Statements 116/116
100% Branches 14/14
100% Functions 1/1
100% Lines 116/116

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 11x 11x 55x 55x 55x 55x 55x 66x 48x 48x 31x 31x 48x 66x 1x 1x 66x 6x 66x 17x 17x 23x 23x 66x 30x 30x 23x 66x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var args2multislice = require( '@stdlib/slice/base/args2multislice' );
var base = require( '@stdlib/ndarray/base/slice' );
var getShape = require( '@stdlib/ndarray/shape' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var nulls = require( '@stdlib/array/base/nulls' );
var objectAssign = require( '@stdlib/object/assign' );
var format = require( '@stdlib/string/format' );
var defaults = require( './defaults.json' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Returns a read-only view of the last element (or subarray) along one or more ndarray dimensions.
*
* ## Notes
*
* -   By default, the function performs the operation over all dimensions and thus returns the last element of the input ndarray as a zero-dimensional ndarray.
* -   If provided an empty `dims` array, the function returns a read-only view of the input ndarray.
*
* @param {ndarray} x - input ndarray
* @param {Options} [options] - function options
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform the operation
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {TypeError} `dims` option must be an array of integers
* @throws {RangeError} `dims` option contains an out-of-bounds dimension index
* @throws {Error} `dims` option contains duplicate indices
* @returns {ndarray} ndarray view
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
*
* var v = last( x );
* // returns <ndarray>[ 4.0 ]
*
* v = last( x, {
*     'dims': [ -1 ]
* });
* // returns <ndarray>[ 2.0, 4.0 ]
*
* v = last( x, {
*     'dims': [ -2 ]
* });
* // returns <ndarray>[ 3.0, 4.0 ]
*/
function last( x, options ) {
	var args;
	var opts;
	var dims;
	var err;
	var sh;
	var N;
	var i;
 
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
	}
	sh = getShape( x );
	N = sh.length;
 
	// Resolve options:
	opts = objectAssign( {}, defaults );
	if ( arguments.length > 1 ) {
		err = validate( opts, N, options );
		if ( err ) {
			throw err;
		}
	}
	if ( N === 0 ) {
		return base( x, args2multislice( [] ), true, false );
	}
	if ( opts.dims === null ) {
		dims = zeroTo( N );
	} else {
		dims = opts.dims;
	}
	// Build a list of slice arguments such that each dimension in `dims` resolves to its last index and all other dimensions are kept in full:
	args = nulls( N );
	for ( i = 0; i < dims.length; i++ ) {
		args[ dims[ i ] ] = sh[ dims[ i ] ] - 1;
	}
	return base( x, args2multislice( args ), true, false );
}
 
 
// EXPORTS //
 
module.exports = last;