All files assign.js

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

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 1793x 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 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 20x 20x 97x 10x 10x 97x 20x 20x 47x 47x 47x 47x 47x 97x 42x 8x 8x 42x 5x 5x 42x 5x 5x 42x 30x 30x 42x 39x 39x 39x 97x 3x 1x 3x 1x 1x 1x 1x 97x 36x 29x 36x 7x 7x 34x 34x 36x 97x 3x 1x 3x 1x 1x 1x 1x 97x 33x 27x 33x 6x 6x 33x 33x 35x 97x 28x 28x 97x 7x 7x 7x 35x 35x 35x 35x 97x 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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' );
var getShape = require( '@stdlib/ndarray/shape' );
var getOrder = require( '@stdlib/ndarray/order' );
var ndims = require( '@stdlib/ndarray/base/ndims' );
var numel = require( '@stdlib/ndarray/base/numel' );
var format = require( '@stdlib/string/format' );
var base = require( './base.js' ).assign;
 
 
// 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} separators - separators
* @param {ndarrayLike} out - output ndarray
* @param {Options} [options] - function options
* @param {(ndarrayLike|string)} [options.prefix=''] - prefix to prepend to each joined string
* @param {(ndarrayLike|string)} [options.suffix=''] - suffix to append to each joined string
* @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} third argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {TypeError} a provided `prefix` or `suffix` ndarray must be zero-dimensional when not specifying reduction dimensions
* @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} a provided `prefix` or `suffix` ndarray must be broadcast compatible with the complement of the shape defined by `options.dims`
* @throws {Error} a provided `separators` ndarray must be broadcast compatible with the shape consisting of the complement of the shape defined by `options.dims` followed by `N-1` where `N` is the number of elements along the reduced 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 a separators ndarray:
* var sep = scalar2ndarray( ',', {
*     'dtype': 'generic'
* });
*
* // Create an output ndarray:
* var y = scalar2ndarray( '', {
*     'dtype': 'generic'
* });
*
* // Perform operation:
* var out = assign( x, sep, y );
* // returns <ndarray>[ '1,2,3,4,5,6' ]
*
* var bool = ( out === y );
* // returns true
*/
function assign( x, separators, out, options ) {
	var prefix;
	var suffix;
	var opts;
	var sep;
	var shx;
	var sh;
	var M;
	var p;
	var s;
 
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
	}
	if ( !isndarrayLike( separators ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', separators ) );
	}
	if ( !isndarrayLike( out ) ) {
		throw new TypeError( format( 'invalid argument. Third argument must be an ndarray-like object. Value: `%s`.', out ) );
	}
	// Initialize prefix, suffix, and options:
	p = '';
	s = '';
	opts = {};
 
	if ( arguments.length > 3 ) {
		if ( !isPlainObject( options ) ) {
			throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
		}
		if ( hasOwnProp( options, 'prefix' ) ) {
			p = options.prefix;
		}
		if ( hasOwnProp( options, 'suffix' ) ) {
			s = options.suffix;
		}
		if ( hasOwnProp( options, 'dims' ) ) {
			opts.dims = options.dims;
		}
	}
	shx = getShape( x );
 
	// Broadcast prefix to the complement shape...
	if ( isndarrayLike( p ) ) {
		if ( hasOwnProp( opts, 'dims' ) ) {
			prefix = maybeBroadcastArray( p, nonCoreShape( shx, opts.dims ) );
		} else if ( ndims( p ) === 0 ) {
			prefix = p;
		} else {
			throw new TypeError( format( 'invalid option. `%s` option must be a zero-dimensional ndarray when not specifying dimensions. Value: `%s`.', 'prefix', p ) );
		}
	} else {
		if ( hasOwnProp( opts, 'dims' ) ) {
			sh = nonCoreShape( shx, opts.dims );
		} else {
			sh = [];
		}
		prefix = broadcastScalar( p, 'generic', sh, getOrder( x ) );
	}
	// Broadcast suffix to the complement shape...
	if ( isndarrayLike( s ) ) {
		if ( hasOwnProp( opts, 'dims' ) ) {
			suffix = maybeBroadcastArray( s, nonCoreShape( shx, opts.dims ) );
		} else if ( ndims( s ) === 0 ) {
			suffix = s;
		} else {
			throw new TypeError( format( 'invalid option. `%s` option must be a zero-dimensional ndarray when not specifying dimensions. Value: `%s`.', 'suffix', s ) );
		}
	} else {
		if ( hasOwnProp( opts, 'dims' ) ) {
			sh = nonCoreShape( shx, opts.dims );
		} else {
			sh = [];
		}
		suffix = broadcastScalar( s, 'generic', sh, getOrder( x ) );
	}
	// Broadcast separators to [ ...complementShape, M-1 ] where M is the number of elements along the reduced dimensions...
	if ( hasOwnProp( opts, 'dims' ) ) {
		sh = nonCoreShape( shx, opts.dims );
		M = numel( shx ) / numel( sh );
	} else {
		sh = [];
		M = numel( shx );
	}
	sh.push( M - 1 );
	sep = maybeBroadcastArray( separators, sh );
 
	return base( x, prefix, suffix, sep, out, opts );
}
 
 
// EXPORTS //
 
module.exports = assign;