All files assign.js

100% Statements 145/145
100% Branches 13/13
100% Functions 2/2
100% Lines 145/145

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 1463x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 10x 10x 33x 10x 10x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 33x 12x 12x 8x 8x 12x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 33x 4x 33x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 33x 3x 3x 3x 3x 3x  
/**
* @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 unaryReduceStrided1d = require( '@stdlib/ndarray/base/unary-reduce-strided1d' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var gjoinBetween = require( '@stdlib/blas/ext/base/ndarray/gjoin-between' );
var getShape = require( '@stdlib/ndarray/shape' );
var getOrder = require( '@stdlib/ndarray/order' );
var format = require( '@stdlib/string/format' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Joins elements of an input ndarray using specified separators for each pair of consecutive elements along one or more ndarray dimensions and assigns the results to a provided output ndarray.
*
* @param {ndarrayLike} x - input ndarray
* @param {ndarrayLike} out - output ndarray
* @param {Options} [options] - function options
* @param {string} [options.prefix=''] - prefix to prepend to each joined string
* @param {string} [options.suffix=''] - suffix to append to each joined string
* @param {ArrayLikeObject} [options.separators=[',']] - separators
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} second argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
* @throws {Error} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
*
* // Create an input ndarray:
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
* // returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
*
* // Create an output ndarray:
* var y = scalar2ndarray( '', {
*     'dtype': 'generic'
* });
*
* // Perform operation:
* var out = assign( x, y );
* // returns <ndarray>[ '1,2,3,4,5,6' ]
*
* var bool = ( out === y );
* // returns true
*/
function assign( x, out, options ) {
	var separators;
	var prefix;
	var suffix;
	var order;
	var opts;
	var err;
	var shx;
	var N;
 
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
	}
	if ( !isndarrayLike( out ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', out ) );
	}
	shx = getShape( x );
	order = getOrder( x );
	N = shx.length;
 
	// Resolve options:
	opts = {
		'prefix': '',
		'suffix': '',
		'separators': [ ',' ],
		'dims': zeroTo( N )
	};
	if ( arguments.length > 2 ) {
		err = validate( opts, N, options );
		if ( err ) {
			throw err;
		}
	}
	// Resolve prefix and suffix:
	prefix = scalar2ndarray( opts.prefix, {
		'dtype': 'generic',
		'order': order
	});
	suffix = scalar2ndarray( opts.suffix, {
		'dtype': 'generic',
		'order': order
	});
 
	// Resolve separators as a one-dimensional ndarray. When the separators array has a single element, the same separator is used for all consecutive pairs regardless of core size:
	if ( opts.separators.length === 1 ) {
		separators = broadcastScalar( opts.separators[ 0 ], 'generic', [ 1 ], order );
	} else {
		separators = new ndarray( 'generic', opts.separators, [ opts.separators.length ], [ 1 ], 0, order );
	}
	// Perform the reduction:
	unaryReduceStrided1d( dispatch, [ x, out ], opts.dims );
 
	return out;
 
	/**
	* Dispatch function which joins elements of a one-dimensional ndarray slice.
	*
	* @private
	* @param {Array<Object>} arrays - ndarrays
	* @returns {string} joined string
	*/
	function dispatch( arrays ) {
		return gjoinBetween( [ arrays[ 0 ], prefix, suffix, separators ] );
	}
}
 
 
// EXPORTS //
 
module.exports = assign;