All files main.js

97.26% Statements 213/219
95.12% Branches 39/41
100% Functions 1/1
97.26% Lines 213/219

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 2201x 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 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 192x 40x 40x 152x 152x 152x 152x 152x 192x 2x 2x 150x 150x 150x 150x 150x 150x 150x 192x 53x 53x 9x 9x 44x 44x 53x 9x 9x 35x 192x 77x 77x 77x 38x 38x 18x 18x 20x 20x 20x 77x 114x 114x 192x 74x 44x 44x 26x 26x 44x 74x 88x 88x 192x 4x 4x 84x 84x 84x 84x 84x 84x 84x 84x 192x 20x 20x 32x 192x 82x 8x 8x 82x 24x 24x 24x 24x 24x 24x 24x 192x 14x 14x 14x 14x 14x 14x 192x 14x 14x 14x 14x 14x 14x 192x         24x 192x 12x     12x 2x 2x 12x 5x 5x 5x 5x 12x 192x 1x 1x 1x 1x 1x  
/**
* @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 isNegativeInteger = require( '@stdlib/assert/is-negative-integer' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var args2multislice = require( '@stdlib/slice/base/args2multislice' );
var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' );
var Slice = require( '@stdlib/slice/ctor' );
var slice = require( '@stdlib/ndarray/slice' );
var maybeBroadcastArray = require( '@stdlib/ndarray/maybe-broadcast-array' );
var base = require( '@stdlib/ndarray/concat' );
var getShape = require( '@stdlib/ndarray/shape' );
var ndims = require( '@stdlib/ndarray/ndims' );
var nulls = require( '@stdlib/array/base/nulls' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Returns an ndarray where the elements of an input ndarray are replaced or removed along a specific dimension.
*
* @param {ndarray} x - input array
* @param {(Slice|integer)} s - slice object or an integer
* @param {ndarray} values - an ndarray containing the elements to insert
* @param {Options} [options] - options
* @param {integer} [options.dim=-1] - dimension along which to perform the operation
* @throws {TypeError} first argument must be an ndarray
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
* @throws {RangeError} dimension index exceeds the number of dimensions
* @throws {RangeError} slice exceeds array bounds
* @throws {TypeError} values argument must be an ndarray
* @throws {Error} insufficient arguments
* @throws {Error} too many arguments
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
* var array = require( '@stdlib/ndarray/array' );
*
* 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 ] ]
*
* var y = array( [ [ 10.0, 20.0 ] ] );
* // returns <ndarray>[ [ 10.0, 20.0 ] ]
*
* var s = new Slice( 1, 2, 1 );
* // returns <Slice>
*
* var out = splice( x, s, y, {
*    'dim': -2
* });
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 10.0, 20.0 ], [ 5.0, 6.0 ] ]
*/
function splice( x, s ) {
	var hasValues;
	var options;
	var hasOpts;
	var slices;
	var nargs;
	var views;
	var spdim;
	var args;
	var opts;
	var lvf;
	var rvf;
	var sh;
	var ms;
	var st;
	var ed;
	var v;
	var N;
	var S;
	var i;
 
	nargs = arguments.length;
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
	}
 
	// Retrieve array meta data:
	N = ndims( x );
 
	// Check whether we were provided a zero-dimensional array...
	if ( N === 0 ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) );
	}
 
	// Resolve function arguments:
	hasOpts = false;
	hasValues = false;
	opts = {
		'dim': -1
	};
	if ( nargs === 4 ) { // Case: splice( x, s, values, options )
		options = arguments[ nargs - 1 ];
		if ( !isPlainObject( options ) ) {
			throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
		}
		hasOpts = true;
		v = arguments[ nargs - 2 ];
		if ( !isndarrayLike( v ) ) {
			throw new TypeError( format( 'invalid argument. Values argument must be an ndarray. Value: `%s`.', v ) );
		}
		hasValues = true;
	} else if ( nargs === 3 ) { // Case: splice( x, s, ???)
		options = arguments[ nargs - 1 ];
		hasOpts = true;
		if ( !isPlainObject( options ) ) {
			v = options;
			if ( !isndarrayLike( v ) ) {
				throw new TypeError( format( 'invalid argument. Values argument must be an ndarray. Value: `%s`.', v ) );
			}
			hasOpts = false;
			hasValues = true;
		}
	}
 
	// Validate options:
	if ( hasOpts ) {
		if ( hasOwnProp( options, 'dim') ) {
			opts.dim = options.dim;
			if ( !isNegativeInteger( opts.dim ) ) {
				throw new TypeError( format( 'invalid option. `%s` option must be a negative integer. Option: `%s`.', 'dim', opts.dim ) );
			}
		}
	}
 
	spdim = normalizeIndex( opts.dim, N-1 );
	if ( spdim === -1 ) {
		throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, spdim ) );
	}
	// Define a list of MultiSlice constructor arguments:
	args = nulls( N );
	args[ spdim ] = s;
	ms = args2multislice( args );
 
	// Validate slices:
	sh = getShape( x );
	S = normalizeMultiSlice( ms, sh, true );
	if ( S.code ) {
		throw new RangeError( format( 'invalid argument. Slice exceeds array bounds. Array shape: (%s).', sh.join( ',' ) ) );
	}
	slices = S.data;
	for ( i = 0; i < slices.length; i++ ) {
		if ( slices[ i ].step !== 1 ) {
			throw new RangeError( format( 'invalid argument. Slice arguments must have an index increment of `1` . Value: `%d`.', slices[ i ].step ) );
		}
	}
 
	// Create input array views:
	views = [];
	st = slices[ spdim ].start;
	ed = slices[ spdim ].stop;
	lvf = false;
	rvf = false;
	if ( st > 0 ) {
		slices[ spdim ] = new Slice( 0, st, 1 );
		views.push( slice( x, slices, {
			'strict': true
		}));
		lvf = true;
	}
	if ( ed < sh[ spdim ] ) {
		slices[ spdim ] = new Slice( ed, sh[ spdim ], 1 );
		views.push( slice( x, slices, {
			'strict': true
		}));
		rvf = true;
	}
	if ( !lvf && !rvf ) {
		views.push( slice( x, slices, {
			'strict': true
		}));
	}
 
	if ( hasValues ) {
		if ( ndims( v ) !== N ) {
			v = maybeBroadcastArray( v, getShape( views[ 0 ] ) );
		}
		if ( lvf && rvf ) {
			return base( [ views[ 0 ], v, views[ 1 ] ], opts );
		}
		if ( lvf ) {
			return base( [ views[ 0 ], v ], opts );
		}
		return base( [ v, views[ 0 ] ], opts );
	}
	return base( views, opts );
}
 
 
// EXPORTS //
 
module.exports = splice;