All files main.js

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

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 1733x 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 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 20x 20x 81x 10x 10x 51x 51x 51x 51x 51x 51x 51x 51x 81x 46x 8x 8x 46x 5x 5x 46x 5x 5x 46x 34x 34x 46x 6x 6x 46x 43x 43x 43x 81x 3x 1x 3x 1x 1x 1x 1x 81x 40x 33x 40x 7x 7x 38x 38x 40x 81x 3x 1x 3x 1x 1x 1x 1x 81x 37x 31x 37x 6x 6x 37x 37x 39x 81x 32x 32x 81x 7x 7x 7x 39x 39x 39x 39x 81x 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' );
 
 
// 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 {ndarrayLike} separators - separators
* @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
* @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} second 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'
* });
*
* // Perform operation:
* var out = joinBetween( x, sep );
* // returns <ndarray>[ '1,2,3,4,5,6' ]
*/
function joinBetween( x, separators, 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 ) );
	}
	// Initialize prefix, suffix, and options:
	p = '';
	s = '';
	opts = {
		'dtype': 'generic',
		'keepdims': false
	};
 
	if ( arguments.length > 2 ) {
		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;
		}
		if ( hasOwnProp( options, 'keepdims' ) ) {
			opts.keepdims = options.keepdims;
		}
	}
	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, opts );
}
 
 
// EXPORTS //
 
module.exports = joinBetween;