'use strict';

// MODULES //

var validate = require( './validate.js' );


// MAIN //

/**
* Multidimensional array constructor.
*
* @constructor
* @param {Options} options - function options
* @param {NonNegativeIntegerArray} options.shape - array shape
* @param {ArrayLike} [options.buffer] - underlying array buffer
* @param {string} [options.dtype] - underlying storage data type (if the input data is not of the same type, this option specifies the data type to which to cast the input data)
* @param {NonNegativeInteger} [options.offset] - integer offset specifying where in the array buffer to begin indexing
* @param {string} [options.order] - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
* @param {IntegerArray} [options.strides] - index strides which specify how to access data along particular array dimensions
* @throws {TypeError} `options` argument must be an object
* @throws {TypeError} must provide valid options
* @throws {RangeError} `offset` option must not exceed number of buffer elements
* @returns {ndarray} multidimensional array
*
* @example
* TODO
*/
function ndarray( options ) {
	/* eslint-disable no-invalid-this */
	var opts;
	var err;

	if ( !(this instanceof ndarray) ) {
		if ( arguments.length ) {
			return new ndarray( options ); // eslint-disable-line no-new-cap
		}
		return new ndarray(); // eslint-disable-line no-new-cap
	}
	opts = {};
	if ( arguments.length ) {
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	}
	// Additional option validation...
	if ( opts.offset && opts.buffer ) {
		if ( opts.offset >= opts.buffer.length ) {
			throw new RangeError( 'invalid option. `offset` option exceeds provided buffer length. Option: `' + opts.offset + '`.' );
		}
	}
	return this;
} // end FUNCTION ndarray()


// EXPORTS //

module.exports = ndarray;

'use strict';

// MODULES //

var validate = require( './validate.js' );


// MAIN //

/**
* Multidimensional array constructor.
*
* @constructor
* @param {Options} options - function options
* @param {NonNegativeIntegerArray} options.shape - array shape
* @param {ArrayLike} [options.buffer] - underlying array buffer
* @param {string} [options.dtype] - underlying storage data type (if the input data is not of the same type, this option specifies the data type to which to cast the input data)
* @param {NonNegativeInteger} [options.offset=0] - integer offset specifying where in the array buffer to begin indexing
* @param {string} [options.order] - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
* @param {IntegerArray} [options.strides] - index strides which specify how to access data along particular array dimensions
* @throws {TypeError} `options` argument must be an object
* @throws {TypeError} must provide valid options
* @throws {RangeError} `offset` option must not exceed number of buffer elements
* @returns {ndarray} multidimensional array
*
* @example
* TODO
*/
function ndarray( options ) {
	/* eslint-disable no-invalid-this */
	var opts;
	var err;

	if ( !(this instanceof ndarray) ) {
		if ( arguments.length ) {
			return new ndarray( options ); // eslint-disable-line no-new-cap
		}
		return new ndarray(); // eslint-disable-line no-new-cap
	}
	opts = {};
	if ( arguments.length ) {
		err = validate( opts, options );
		if ( err ) {
			throw err;
		}
	}
	opts.offset = opts.offset || 0;

	// Additional option validation...
	if (
		opts.offset &&
		opts.buffer &&
		opts.offset >= opts.buffer.length
	) {
		throw new RangeError( 'invalid option. `offset` option exceeds provided buffer length. Option: `' + opts.offset + '`.' );
	}
	return this;
} // end FUNCTION ndarray()


// EXPORTS //

module.exports = ndarray;

Consolidate conditionals