Skip to content

Commit b2210ca

Browse files
committed
Simplify conditionals
1 parent 7d749de commit b2210ca

File tree

1 file changed

+27
-30
lines changed
  • lib/node_modules/@stdlib/ndarray/lib

1 file changed

+27
-30
lines changed

lib/node_modules/@stdlib/ndarray/lib/main.js

+27-30
Original file line numberDiff line numberDiff line change
@@ -236,41 +236,38 @@ function array() {
236236
} else {
237237
throw new Error( 'invalid input arguments. Must provide either a data source, array shape, or both.' );
238238
}
239-
// For non-ndarray data source inputs, we assume we've been provided a contiguous data source for which we can simply compute the strides and index offset pointing to the first indexed element solely based on the shape and specified memory layout order...
240-
if ( !FLG ) {
241-
strides = shape2strides( shape, order );
242-
offset = strides2offset( shape, strides );
243-
}
244239
// If not provided a data buffer, create it; otherwise, see if we need to cast a provided data buffer to another data type or perform a copy...
245-
if ( buffer ) {
246-
if ( FLG ) {
247-
btype = buffer.dtype;
248-
if ( buffer.length !== len ) {
249-
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
250-
}
251-
if ( btype !== dtype || opts.copy ) {
252-
buffer = copyView( buffer, dtype );
253-
strides = shape2strides( shape, order ); // computed, as we copied elements based on the ndarray view
254-
offset = strides2offset( shape, strides );
255-
} else {
256-
strides = buffer.strides;
257-
offset = buffer.offset;
258-
buffer = buffer.data;
259-
}
240+
if ( FLG ) {
241+
btype = buffer.dtype;
242+
if ( buffer.length !== len ) {
243+
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
244+
}
245+
if ( btype !== dtype || opts.copy ) {
246+
buffer = copyView( buffer, dtype );
247+
strides = shape2strides( shape, order );
248+
offset = strides2offset( shape, strides );
260249
} else {
261-
btype = getType( buffer );
262-
if ( btype === 'generic' && opts.flatten ) {
263-
buffer = flattenArray( buffer );
264-
}
265-
if ( buffer.length !== len ) {
266-
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
267-
}
268-
if ( btype !== dtype || opts.copy ) {
269-
buffer = castBuffer( buffer, len, dtype );
270-
}
250+
strides = buffer.strides;
251+
offset = buffer.offset;
252+
buffer = buffer.data;
253+
}
254+
} else if ( buffer ) {
255+
btype = getType( buffer );
256+
if ( btype === 'generic' && opts.flatten ) {
257+
buffer = flattenArray( buffer );
258+
}
259+
if ( buffer.length !== len ) {
260+
throw new RangeError( 'invalid input arguments. Array shape is incompatible with provided data source. Number of data source elements does not match array shape.' );
261+
}
262+
if ( btype !== dtype || opts.copy ) {
263+
buffer = castBuffer( buffer, len, dtype );
271264
}
265+
strides = shape2strides( shape, order );
266+
offset = strides2offset( shape, strides );
272267
} else {
273268
buffer = createBuffer( dtype, len );
269+
strides = shape2strides( shape, order );
270+
offset = strides2offset( shape, strides );
274271
}
275272
// Return a new ndarray:
276273
return ctor( dtype, ndims, nopts )( buffer, shape, strides, offset, order );

0 commit comments

Comments
 (0)