All files main.js

100% Statements 191/191
100% Branches 36/36
100% Functions 1/1
100% Lines 191/191

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 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 1x 1x 1x 1x 1x 1x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 40x 40x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 141x 8x 8x 93x 93x 77x 77x 2x 2x 2x 75x 75x 75x 75x 75x 77x 16x 16x 16x 16x 16x 16x 141x 14x 14x 141x 77x 14x 14x 77x 24x 10x 10x 14x 14x 77x 33x 8x 8x 8x 2x 2x 8x 2x 2x 6x 4x 4x 4x 33x 8x 8x 25x 8x 8x 17x 9x 9x 33x 77x 12x 12x 12x 77x 54x 54x 54x 54x 54x 54x 54x 54x 141x 141x 141x 141x 141x 141x 141x 141x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isFunction = require( '@stdlib/assert/is-function' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
var getShape = require( '@stdlib/ndarray/shape' );
var getOrder = require( '@stdlib/ndarray/order' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' );
var map = require( '@stdlib/ndarray/base/map' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var emptyLike = require( '@stdlib/ndarray/empty-like' );
var format = require( '@stdlib/string/format' );
 
 
// VARIABLES //
 
var ROW_MAJOR = 'row-major';
var COL_MAJOR = 'column-major';
 
 
// MAIN //
 
/**
* Flattens an ndarray according to a callback function.
*
* @param {ndarray} x - input ndarray
* @param {Options} [options] - function options
* @param {NonNegativeInteger} [options.depth] - maximum number of dimensions to flatten
* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened
* @param {*} [options.dtype] - output ndarray data type
* @param {Function} fcn - callback function
* @param {*} [thisArg] - callback execution context
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {TypeError} callback argument must be a function
* @throws {TypeError} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* function scale( value ) {
*     return value * 2.0;
* }
*
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
* // returns <ndarray>
*
* var y = flattenBy( x, scale );
* // returns <ndarray>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
*/
function flattenBy( x, options, fcn, thisArg ) {
	var hasOpts;
	var nargs;
	var view;
	var opts;
	var ctx;
	var xsh;
	var cb;
	var st;
	var y;
	var o;
 
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
	}
	nargs = arguments.length;
	xsh = getShape( x );
	hasOpts = false;
 
	// Define default options:
	opts = {
		'depth': xsh.length,    // by default, flatten to a one-dimensional ndarray
		'order': ROW_MAJOR,     // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row)
		'dtype': getDType( x )
	};
 
	// Case: flattenBy( x, fcn )
	if ( nargs <= 2 ) {
		cb = options;
	}
	// Case: flattenBy( x, ???, ??? )
	else if ( nargs === 3 ) {
		// Case: flattenBy( x, fcn, thisArg )
		if ( isFunction( options ) ) {
			cb = options;
			ctx = fcn;
		}
		// Case: flattenBy( x, options, fcn )
		else {
			hasOpts = true;
			cb = fcn;
		}
	}
	// Case: flattenBy( x, options, fcn, thisArg )
	else {
		hasOpts = true;
		cb = fcn;
		ctx = thisArg;
	}
	if ( !isFunction( cb ) ) {
		throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
	}
	if ( hasOpts ) {
		if ( !isPlainObject( options ) ) {
			throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
		}
		if ( hasOwnProp( options, 'depth' ) ) {
			if ( !isNonNegativeInteger( options.depth ) ) {
				throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'depth', options.depth ) );
			}
			opts.depth = options.depth;
		}
		if ( hasOwnProp( options, 'order' ) ) {
			if ( options.order === 'any' ) {
				// When 'any', we want to flatten according to the physical layout of the data in memory...
				o = strides2order( getStrides( x ) );
				if ( o === 1 ) {
					// Data is currently arranged in row-major order:
					opts.order = ROW_MAJOR;
				} else if ( o === 2 ) {
					// Data is currently arranged in column-major order:
					opts.order = COL_MAJOR;
				} else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major
					// When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray:
					opts.order = getOrder( x );
				}
			} else if ( options.order === 'same' ) {
				// When 'same', we want to flatten according to the stated order of the input ndarray:
				opts.order = getOrder( x );
			} else if ( isOrder( options.order ) ) {
				// When provided a specific order, flatten according to that order regardless of the order of the input ndarray:
				opts.order = options.order;
			} else {
				throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) );
			}
		}
		if ( hasOwnProp( options, 'dtype' ) ) {
			// Delegate `dtype` validation to `emptyLike` during output array creation:
			opts.dtype = options.dtype;
		}
	}
	// Create an output ndarray having contiguous memory:
	y = emptyLike( x, {
		'shape': flattenShape( xsh, opts.depth ),
		'order': opts.order,
		'dtype': opts.dtype
	});
 
	// Create a view on top of the output ndarray having the same shape as the input ndarray:
	st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ];
	view = new ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order );
 
	// Transform and assign elements to the output ndarray:
	map( [ x, view ], cb, ctx );
 
	return y;
}
 
 
// EXPORTS //
 
module.exports = flattenBy;