All files / ndarray/ndarraylike2ndarray/lib main.js

100% Statements 100/100
85.71% Branches 6/7
100% Functions 1/1
100% Lines 100/100

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 1011x 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 6x 2x 1x 1x 1x 1x 1x 2x 5x 6x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var defaults = require( '@stdlib/ndarray/defaults' );
 
 
// VARIABLES //
 
var DEFAULT_ORDER = defaults( 'order' );
 
 
// MAIN //
 
/**
* Converts an ndarray-like object to an ndarray.
*
* @param {ndarrayLike} x - ndarray-like object
* @param {string} x.dtype - data type
* @param {Collection} x.data - data buffer
* @param {NonNegativeIntegerArray} x.shape - dimensions
* @param {IntegerArray} x.strides - stride lengths
* @param {NonNegativeInteger} x.offset - index offset
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
* @param {Options} [options] - function options
* @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions
* @param {StringArray} [options.submode=["throw"]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis
* @param {boolean} [options.readonly=false] - boolean indicating whether an array should be read-only
* @throws {Error} cannot write to a read-only array
* @returns {ndarray} ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
*
* var out = ndarraylike2ndarray( x );
* // returns <ndarray>
*/
function ndarraylike2ndarray( x ) {
	var strides;
	var offset;
	var order;
	var shape;
	var data;
	var opts;
	var dt;
 
	dt = getDType( x );
	data = getData( x );
	shape = getShape( x );
	strides = getStrides( x );
	offset = getOffset( x );
	order = getOrder( x ) || DEFAULT_ORDER;
	if ( arguments.length > 1 ) {
		opts = arguments[ 1 ];
	} else {
		opts = {};
	}
	if ( isReadOnly( x ) ) {
		if ( opts.readonly === void 0 ) {
			// Preserve read-only behavior...
			opts.readonly = true;
		} else if ( opts.readonly === false ) {
			throw new Error( 'invalid argument. Cannot write to a read-only array.' );
		}
	}
	return new ndarray( dt, data, shape, strides, offset, order, opts );
}
 
 
// EXPORTS //
 
module.exports = ndarraylike2ndarray;