All files main.js

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

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 1553x 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 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 20x 20x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 47x 25x 25x 15x 15x 25x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 47x 10x 47x 2x 2x 12x 12x 12x 12x 47x 2x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 19x 19x 47x 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 spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var indicesComplement = require( '@stdlib/array/base/indices-complement' );
var takeIndexed = require( '@stdlib/array/base/take-indexed' );
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 empty = require( '@stdlib/ndarray/empty' );
var format = require( '@stdlib/string/format' );
var validate = require( './validate.js' );
 
 
// MAIN //
 
/**
* Returns an ndarray created by joining elements using specified separators for each pair of consecutive elements along one or more ndarray dimensions.
*
* @param {ndarrayLike} x - input 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
* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions
* @throws {TypeError} first 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' );
*
* // 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 ]
*
* // Perform operation:
* var out = joinBetween( x );
* // returns <ndarray>[ '1,2,3,4,5,6' ]
*/
function joinBetween( x, options ) {
	var separators;
	var prefix;
	var suffix;
	var order;
	var opts;
	var err;
	var idx;
	var shy;
	var shx;
	var N;
	var y;
 
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
	}
	shx = getShape( x );
	order = getOrder( x );
	N = shx.length;
 
	// Resolve options:
	opts = {
		'prefix': '',
		'suffix': '',
		'separators': [ ',' ],
		'dims': zeroTo( N ),
		'keepdims': false
	};
	if ( arguments.length > 1 ) {
		err = validate( opts, N, options );
		if ( err ) {
			throw err;
		}
	}
	// Resolve the list of non-reduced dimensions and the output shape:
	idx = indicesComplement( N, opts.dims );
	shy = takeIndexed( shx, idx );
 
	// Create the output ndarray:
	y = empty( shy, {
		'dtype': 'generic',
		'order': order
	});
 
	// 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, y ], opts.dims );
 
	// Check whether we need to reinsert singleton dimensions...
	if ( opts.keepdims ) {
		y = spreadDimensions( N, y, idx, true );
	}
	return y;
 
	/**
	* 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 = joinBetween;