All files / slice/multi/lib main.js

99.28% Statements 279/281
97.14% Branches 34/35
100% Functions 6/6
99.28% Lines 279/281

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 2821x 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 621x 621x 621x 621x 621x 244x 621x 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 393x 393x 393x 393x 393x 393x 393x 393x 393x 125x 4x 4x 125x 20x 20x 125x 26x 26x 125x 33x 33x 125x 41x 41x 1x 125x     1x 1x 1x 1x 268x 393x 621x 621x 232x 232x 621x 621x 36x 393x 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 8x 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 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 3x 3x 2x 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 2x 1x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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.
*/
 
/* eslint-disable no-restricted-syntax, no-invalid-this */
 
'use strict';
 
// MODULES //
 
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isNull = require( '@stdlib/assert/is-null' );
var isUndefined = require( '@stdlib/assert/is-undefined' );
var isSlice = require( '@stdlib/assert/is-slice' );
var format = require( '@stdlib/string/format' );
 
 
// FUNCTIONS //
 
/**
* Tests whether an input argument is valid.
*
* @private
* @param {*} value - value to test
* @returns {boolean} boolean indicating whether the argument is valid
*
* @example
* var bool = isValid( 3 );
* // returns true
*
* bool = isValid( null );
* // returns true
*
* bool = isValid( void 0 );
* // returns true
*
* bool = isValid( '3' );
* // returns false
*/
function isValid( value ) {
	return (
		isInteger( value ) ||
		isNull( value ) ||
		isUndefined( value ) ||
		isSlice( value )
	);
}
 
 
// MAIN //
 
/**
* Multi-slice constructor.
*
* @constructor
* @param {...(Slice|integer|null)} slice - slice
* @throws {TypeError} a provided argument must be either a Slice, integer, null, or undefined
* @returns {MultiSlice} MultiSlice instance
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
*
* var s1 = new Slice( 0, 10, 1 );
* var s2 = new Slice( 2, 12, 2 );
*
* var ms = new MultiSlice( null, s1, s2, 2 );
* // returns <MultiSlice>
*/
function MultiSlice() {
	var nargs;
	var proxy;
	var args;
	var v;
	var i;
 
	nargs = arguments.length;
	if ( !( this instanceof MultiSlice ) ) {
		if ( nargs === 1 ) {
			return new MultiSlice( arguments[ 0 ] );
		}
		if ( nargs === 2 ) {
			return new MultiSlice( arguments[ 0 ], arguments[ 1 ] );
		}
		if ( nargs === 3 ) {
			return new MultiSlice( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ] ); // eslint-disable-line max-len
		}
		if ( nargs === 4 ) {
			return new MultiSlice( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ], arguments[ 3 ] ); // eslint-disable-line max-len
		}
		if ( nargs === 5 ) {
			return new MultiSlice( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ], arguments[ 3 ], arguments[ 4 ] ); // eslint-disable-line max-len
		}
		args = [];
		for ( i = 0; i < nargs; i++ ) {
			args.push( arguments[ i ] );
		}
		// Use a workaround for being unable to combine `.apply` with the `new` operator:
		proxy = Object.create( MultiSlice.prototype );
		return MultiSlice.apply( proxy, args );
	}
	this._data = [];
	for ( i = 0; i < nargs; i++ ) {
		v = arguments[ i ];
		if ( !isValid( v ) ) {
			throw new TypeError( format( 'invalid argument. Provided arguments must be either a Slice, integer, null, or undefined. Argument: `%d`. Value: `%s`.', i, String( v ) ) );
		}
		this._data.push( ( v === void 0 ) ? null : v );
	}
	return this;
}
 
/**
* Constructor name.
*
* @name name
* @memberof MultiSlice
* @readonly
* @type {string}
* @default 'MultiSlice'
*
* @example
* var str = MultiSlice.name;
* // returns 'MultiSlice'
*/
setReadOnly( MultiSlice, 'name', 'MultiSlice' );
 
/**
* Returns the number of slice dimensions.
*
* @name ndims
* @memberof MultiSlice.prototype
* @readonly
* @type {NonNegativeInteger}
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
*
* var s1 = new Slice( 0, 10, 1 );
* var s2 = new Slice( 2, 12, 2 );
*
* var ms = new MultiSlice( null, s1, s2, 2 );
* // returns <MultiSlice>
*
* var ndims = ms.ndims;
* // returns 4
*/
setReadOnlyAccessor( MultiSlice.prototype, 'ndims', function get() {
	return this._data.length;
});
 
/**
* Returns multi-slice data.
*
* @name data
* @memberof MultiSlice.prototype
* @readonly
* @type {Array<Slice|null|integer>}
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
*
* var s1 = new Slice( 0, 10, 1 );
* var s2 = new Slice( 2, 12, 2 );
*
* var ms = new MultiSlice( null, s1, s2, 2 );
* // returns <MultiSlice>
*
* var data = ms.data;
* // returns [...]
*
* var v = data[ 0 ];
* // returns null
*
* v = data[ 1 ];
* // returns <Slice>
*
* v = data[ 2 ];
* // returns <Slice>
*
* v = data[ 3 ];
* // returns 2
*/
setReadOnlyAccessor( MultiSlice.prototype, 'data', function get() {
	return this._data.slice();
});
 
/**
* Serializes a multi-slice to a string.
*
* @name toString
* @memberof MultiSlice.prototype
* @type {Function}
* @returns {string} serialized MultiSlice
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
*
* var s1 = new Slice( 0, 10, 1 );
* var s2 = new Slice( 2, 12, 2 );
*
* var ms = new MultiSlice( null, s1, s2, 2 );
* // returns <MultiSlice>
*
* var str = ms.toString();
* // returns 'MultiSlice(null,Slice(0,10,1),Slice(2,12,2),2)'
*/
setReadOnly( MultiSlice.prototype, 'toString', function toString() {
	var data;
	var out;
	var i;
 
	data = this._data;
	out = [];
	for ( i = 0; i < data.length; i++ ) {
		out.push( String( data[ i ] ) );
	}
	return 'MultiSlice('+out.join( ',' )+')';
});
 
/**
* Serializes a multi-slice as a JSON object.
*
* ## Notes
*
* -   `JSON.stringify()` implicitly calls this method when stringifying a `MultiSlice` instance.
*
* @name toString
* @memberof MultiSlice.prototype
* @type {Function}
* @returns {Object} serialized MultiSlice
*
* @example
* var Slice = require( '@stdlib/slice/ctor' );
*
* var s1 = new Slice( 0, 10, 1 );
* var s2 = new Slice( 2, 12, 2 );
*
* var ms = new MultiSlice( null, s1, s2, 2 );
* // returns <MultiSlice>
*
* var o = ms.toJSON();
* // returns { 'type': 'MultiSlice', 'data': [ null, { 'type': 'Slice', 'data': [ 0, 10, 1 ] }, { 'type': 'Slice', 'data': [ 2, 12, 2 ] }, 2 ] }
*/
setReadOnly( MultiSlice.prototype, 'toJSON', function toJSON() {
	var data;
	var out;
	var v;
	var i;
 
	data = this._data;
	out = {
		'type': 'MultiSlice',
		'data': []
	};
	for ( i = 0; i < data.length; i++ ) {
		v = data[ i ];
		out.data.push( ( v && typeof v.toJSON === 'function' ) ? v.toJSON() : v );
	}
	return out;
});
 
 
// EXPORTS //
 
module.exports = MultiSlice;