UNPKG

ubique

Version:

A mathematical and quantitative library for Javascript and Node.js

51 lines (48 loc) 1.13 kB
/** * Indexing */ module.exports = function($u) { /** * @method end * @summary Last index in array or matrix * @description Last index in array or matrix. Indexing is in the range [0...N-1] * * @param {array|matrix} x values * @param {number} dim (only for matrix) -1: [rows,columns], 0: rows, 1: column (def: -1) * @return {number|array} * * @example * ubique.end([5,6,3]); * // 2 * * ubique.end([[4,5,0],[-1,2,-3]]); * // [1, 2] * * ubique.end([[4,5,0],[-1,2,-3]],0); * // 1 */ $u.end = function(x,dim) { if (arguments.length === 0) { throw new Error('not enough input arguments'); } dim = dim == null ? -1 : dim; if ($u.isnumber(x)) { return x; } else if ($u.isarray(x)) { return x.length - 1; } else if ($u.ismatrix(x)) { if (!$u.isinteger(dim) || (dim < -1 || dim > 1)) { throw new Error('dimension must be -1,0,1'); } var idx = [$u.nrows(x) - 1,$u.ncols(x) - 1]; if (dim === -1) { return idx; } else { return idx[dim]; } } else throw new Error('unkown input arguments'); } }