Skip to content

Commit fce33ba

Browse files
committed
fix: apply suggestions from code review
1 parent 94d82c3 commit fce33ba

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/repl.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
Input array.
1010

1111
dims: ArrayLikeObject<integer>
12-
Dimension indices along which to reverse elements. If less than zero,
13-
the index is resolved relative to the last dimension, with the last
14-
dimension corresponding to the value `-1`.
12+
Dimension indices along which to reverse elements. If provided an
13+
integer less than zero, the dimension index is resolved relative to the
14+
last dimension, with the last dimension corresponding to the value `-1`.
1515

1616
writable: boolean
1717
Boolean indicating whether a returned ndarray should be writable. This

lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { ndarray } from '@stdlib/types/ndarray';
4040
* var y = reverseDimensions( x, [ 0, 1 ], false );
4141
* // returns <ndarray>[ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ]
4242
*/
43-
declare function reverseDimensions<T extends ndarray>( x: T, dims: Collection<number>, writable: boolean ): T;
43+
declare function reverseDimensions<T extends ndarray = ndarray>( x: T, dims: Collection<number>, writable: boolean ): T;
4444

4545

4646
// EXPORTS //

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var Slice = require( '@stdlib/slice/ctor' );
2525
var slice = require( '@stdlib/ndarray/base/slice' );
2626
var nulls = require( '@stdlib/array/base/nulls' );
2727
var ndims = require( '@stdlib/ndarray/base/ndims' );
28-
var toNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
28+
var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
2929
var format = require( '@stdlib/string/format' );
3030

3131

@@ -39,6 +39,7 @@ var format = require( '@stdlib/string/format' );
3939
* @param {boolean} writable - boolean indicating whether a returned array should be writable
4040
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
4141
* @throws {RangeError} dimension index exceeds the number of dimensions
42+
* @throws {Error} must provide unique dimension indices
4243
* @returns {ndarray} ndarray view
4344
*
4445
* @example
@@ -64,10 +65,13 @@ function reverseDimensions( x, dims, writable ) {
6465
throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) );
6566
}
6667
// Normalize the dimension indices...
67-
d = toNormalizedIndices( dims, N-1 );
68+
d = toUniqueNormalizedIndices( dims, N-1 );
6869
if ( d === null ) {
6970
throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%s`.', N, dims ) );
7071
}
72+
if ( d.length !== dims.length ) {
73+
throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', dims ) );
74+
}
7175
// Define a list of MultiSlice constructor arguments:
7276
args = nulls( N );
7377
for ( i = 0; i < d.length; i++ ) {

lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ tape( 'the function throws an error if the dimension index exceeds the number of
8787
}
8888
});
8989

90+
tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) {
91+
var values;
92+
var x;
93+
var i;
94+
95+
x = zeros( [ 2, 2, 2 ] );
96+
97+
values = [
98+
[ 0, 0 ],
99+
[ 1, 1 ],
100+
[ 2, 2 ],
101+
[ 0, -3 ],
102+
[ -2, 1 ],
103+
[ -1, 2 ]
104+
];
105+
for ( i = 0; i < values.length; i++ ) {
106+
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided dimension indices ['+values[ i ]+']' );
107+
}
108+
t.end();
109+
110+
function badValue( dims ) {
111+
return function badValue() {
112+
reverseDimensions( x, dims, false );
113+
};
114+
}
115+
});
116+
90117
tape( 'the function returns a view of a provided input array (ndims=1)', function test( t ) {
91118
var expected;
92119
var actual;

0 commit comments

Comments
 (0)