Skip to content

Commit c634089

Browse files
committed
refactor: use ndarray utility to normalize an index
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 34fcc71 commit c634089

File tree

1 file changed

+10
-12
lines changed
  • lib/node_modules/@stdlib/ndarray/base/expand-dimensions/lib

1 file changed

+10
-12
lines changed

lib/node_modules/@stdlib/ndarray/base/expand-dimensions/lib/main.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
24+
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
2425
var getDType = require( '@stdlib/ndarray/base/dtype' );
2526
var getShape = require( '@stdlib/ndarray/base/shape' );
2627
var getStrides = require( '@stdlib/ndarray/base/strides' );
@@ -77,6 +78,7 @@ function expandDimensions( x, axis ) {
7778
var ord;
7879
var sh;
7980
var st;
81+
var d;
8082
var N;
8183
var i;
8284

@@ -88,15 +90,11 @@ function expandDimensions( x, axis ) {
8890
strides = [];
8991
shape = [];
9092

91-
if ( axis < 0 ) {
92-
if ( axis < -N-1 ) {
93-
throw new RangeError( format( 'invalid argument. Specified axis is out-of-bounds. Must be on the interval: [-%u-1, %u]. Value: `%d`.', N, N, axis ) );
94-
}
95-
axis += N + 1;
96-
} else if ( axis > N ) {
97-
throw new RangeError( format( 'invalid argument. Specified axis is out-of-bounds. Must be on the interval: [-%u-1, %u]. Value: `%d`.', N, N, axis ) );
93+
d = normalizeIndex( axis, N );
94+
if ( d === -1 ) {
95+
throw new RangeError( format( 'invalid argument. Specified axis is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `%d`.', N+1, N, axis ) );
9896
}
99-
if ( axis === 0 ) {
97+
if ( d === 0 ) {
10098
// Prepend singleton dimension...
10199
shape.push( 1 );
102100
strides.push( st[ 0 ] );
@@ -106,7 +104,7 @@ function expandDimensions( x, axis ) {
106104
shape.push( sh[ i ] );
107105
strides.push( st[ i ] );
108106
}
109-
} else if ( axis === N ) {
107+
} else if ( d === N ) {
110108
// Copy dimensions...
111109
for ( i = 0; i < N; i++ ) {
112110
shape.push( sh[ i ] );
@@ -118,17 +116,17 @@ function expandDimensions( x, axis ) {
118116
} else {
119117
// Insert a singleton dimension...
120118
for ( i = 0; i < N+1; i++ ) {
121-
if ( i === axis ) {
119+
if ( i === d ) {
122120
shape.push( 1 );
123121
if ( ord === 'row-major' ) {
124122
strides.push( st[ i-1 ] );
125123
} else { // ord === 'column-major'
126124
strides.push( st[ i ] );
127125
}
128-
} else if ( i < axis ) {
126+
} else if ( i < d ) {
129127
shape.push( sh[ i ] );
130128
strides.push( st[ i ] );
131-
} else { // i > axis
129+
} else { // i > d
132130
shape.push( sh[ i-1 ] );
133131
strides.push( st[ i-1 ] );
134132
}

0 commit comments

Comments
 (0)