Skip to content

Commit 0418c32

Browse files
committed
refactor: apply suggestions from code review
--- 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: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 991888e commit 0418c32

File tree

8 files changed

+351
-96
lines changed

8 files changed

+351
-96
lines changed

lib/node_modules/@stdlib/ndarray/pop/README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ limitations under the License.
4040
var pop = require( '@stdlib/ndarray/pop' );
4141
```
4242

43-
#### pop( x, dim )
43+
#### pop( x\[, options] )
4444

4545
Returns an array containing a **read-only** truncated view of an input [`ndarray`][@stdlib/ndarray/ctor] and a **read-only** view of the last element(s) along a specified dimension.
4646

@@ -60,20 +60,56 @@ var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
6060
var arr = ndarray2array( x );
6161
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
6262

63-
var y = pop( x, 0 );
63+
var y = pop( x );
6464
// returns [ <ndarray>, <ndarray> ]
6565

6666
arr = ndarray2array( y[ 0 ] );
67-
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
67+
// returns [ [ 1.0 ], [ 3.0 ], [ 5.0 ] ]
6868

6969
arr = ndarray2array( y[ 1 ] );
70-
// returns [ [ 5.0, 6.0 ] ]
70+
// returns [ [ 2.0 ], [ 4.0 ], [ 6.0 ] ]
7171
```
7272

7373
The function accepts the following arguments:
7474

7575
- **x**: input ndarray.
76-
- **dim**: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
76+
- **options**: function options. (_optional_)
77+
78+
The function supports the following `options`:
79+
80+
- **dim**: dimension along which to perform the operation. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `-1`.
81+
82+
By default, the function performs the operation along the last dimension of the input [ndarray][@stdlib/ndarray/ctor]. To perform the operation on a desired dimension, specify the `dim` option.
83+
84+
```javascript
85+
var ndarray = require( '@stdlib/ndarray/ctor' );
86+
var getShape = require( '@stdlib/ndarray/shape' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
89+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
90+
var shape = [ 3, 2 ];
91+
var strides = [ 2, 1 ];
92+
var offset = 0;
93+
94+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
95+
// returns <ndarray>
96+
97+
var arr = ndarray2array( x );
98+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
99+
100+
var opts = {
101+
'dim': 0
102+
};
103+
104+
var y = pop( x, opts );
105+
// returns [ <ndarray>, <ndarray> ]
106+
107+
arr = ndarray2array( y[ 0 ] );
108+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
109+
110+
arr = ndarray2array( y[ 1 ] );
111+
// returns [ [ 5.0, 6.0 ] ]
112+
```
77113

78114
</section>
79115

lib/node_modules/@stdlib/ndarray/pop/benchmark/benchmark.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bench( pkg+'::1d', function benchmark( b ) {
4848

4949
b.tic();
5050
for ( i = 0; i < b.iterations; i++ ) {
51-
v = pop( values[ i%values.length ], 0 );
51+
v = pop( values[ i%values.length ] );
5252
if ( typeof v !== 'object' ) {
5353
b.fail( 'should return an array of ndarrays' );
5454
}
@@ -80,7 +80,7 @@ bench( pkg+'::2d', function benchmark( b ) {
8080

8181
b.tic();
8282
for ( i = 0; i < b.iterations; i++ ) {
83-
v = pop( values[ i%values.length ], 0 );
83+
v = pop( values[ i%values.length ] );
8484
if ( typeof v !== 'object' ) {
8585
b.fail( 'should return an array of ndarrays' );
8686
}
@@ -112,7 +112,7 @@ bench( pkg+'::3d', function benchmark( b ) {
112112

113113
b.tic();
114114
for ( i = 0; i < b.iterations; i++ ) {
115-
v = pop( values[ i%values.length ], 0 );
115+
v = pop( values[ i%values.length ] );
116116
if ( typeof v !== 'object' ) {
117117
b.fail( 'should return an array of ndarrays' );
118118
}
@@ -144,7 +144,7 @@ bench( pkg+'::4d', function benchmark( b ) {
144144

145145
b.tic();
146146
for ( i = 0; i < b.iterations; i++ ) {
147-
v = pop( values[ i%values.length ], 0 );
147+
v = pop( values[ i%values.length ] );
148148
if ( typeof v !== 'object' ) {
149149
b.fail( 'should return an array of ndarrays' );
150150
}
@@ -176,7 +176,7 @@ bench( pkg+'::5d', function benchmark( b ) {
176176

177177
b.tic();
178178
for ( i = 0; i < b.iterations; i++ ) {
179-
v = pop( values[ i%values.length ], 0 );
179+
v = pop( values[ i%values.length ] );
180180
if ( typeof v !== 'object' ) {
181181
b.fail( 'should return an array of ndarrays' );
182182
}

lib/node_modules/@stdlib/ndarray/pop/docs/repl.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
{{alias}}( x, dim )
2+
{{alias}}( x[, options] )
33
Returns an array containing a read-only truncated view of an input ndarray
44
and a read-only view of the last element(s) along a specified dimension.
55

@@ -8,10 +8,14 @@
88
x: ndarray
99
Input ndarray.
1010

11-
dim: integer
11+
options: Object (optional)
12+
Options.
13+
14+
options.dim: integer (optional)
1215
Dimension along which to perform the operation. If provided an integer
1316
less than zero, the dimension index is resolved relative to the last
1417
dimension, with the last dimension corresponding to the value `-1`.
18+
Default: `-1`.
1519

1620
Returns
1721
-------
@@ -23,7 +27,7 @@
2327
--------
2428
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
2529
<ndarray>
26-
> var y = {{alias}}( x, 1 )
30+
> var y = {{alias}}( x )
2731
[ <ndarray>, <ndarray> ]
2832
> {{alias:@stdlib/ndarray/to-array}}( y[0] )
2933
[ [ 1 ], [ 3 ] ]

lib/node_modules/@stdlib/ndarray/pop/docs/types/index.d.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@
2222

2323
import { ndarray } from '@stdlib/types/ndarray';
2424

25+
/**
26+
* Interface defining function options.
27+
*/
28+
interface Options {
29+
/**
30+
* Dimension along which to perform the operation.
31+
*/
32+
dim?: number;
33+
}
34+
2535
/**
2636
* Returns an array containing a read-only truncated view of an input ndarray and a read-only view of the last element(s) along a specified dimension.
2737
*
2838
* @param x - input array
29-
* @param dim - dimension along which to perform the operation
39+
* @param options - function options
40+
* @param options.dim - dimension along which to perform the operation
3041
* @returns a list of ndarrays
3142
*
3243
* @example
@@ -45,16 +56,16 @@ import { ndarray } from '@stdlib/types/ndarray';
4556
* var arr = ndarray2array( x );
4657
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
4758
*
48-
* var y = pop( x, 0 );
59+
* var y = pop( x );
4960
* // returns [ <ndarray>, <ndarray> ]
5061
*
5162
* arr = ndarray2array( y[ 0 ] );
52-
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
63+
* // returns [ [ 1.0 ], [ 3.0 ], [ 5.0 ] ]
5364
*
5465
* arr = ndarray2array( y[ 1 ] );
55-
* // returns [ [ 5.0, 6.0 ] ]
66+
* // returns [ [ 2.0 ], [ 4.0 ], [ 6.0 ] ]
5667
*/
57-
declare function pop<T extends ndarray = ndarray>( x: T, dim: number ): [ T, T ];
68+
declare function pop<T extends ndarray = ndarray>( x: T, options?: Options ): [ T, T ];
5869

5970

6071
// EXPORTS //

lib/node_modules/@stdlib/ndarray/pop/docs/types/test.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,63 @@ import pop = require( './index' );
2727
const order = 'row-major';
2828
const sh = [ 2, 2 ];
2929

30-
pop( empty( 'float64', sh, order ), 1 ); // $ExpectType [float64ndarray, float64ndarray]
31-
pop( empty( 'complex64', sh, order ), 1 ); // $ExpectType [complex64ndarray, complex64ndarray]
32-
pop( empty( 'uint8', sh, order ), 1 ); // $ExpectType [uint8ndarray, uint8ndarray]
30+
pop( empty( 'float64', sh, order ) ); // $ExpectType [float64ndarray, float64ndarray]
31+
pop( empty( 'complex64', sh, order ) ); // $ExpectType [complex64ndarray, complex64ndarray]
32+
pop( empty( 'uint8', sh, order ) ); // $ExpectType [uint8ndarray, uint8ndarray]
33+
34+
pop( empty( 'float64', sh, order ), {} ); // $ExpectType [float64ndarray, float64ndarray]
35+
pop( empty( 'complex64', sh, order ), {} ); // $ExpectType [complex64ndarray, complex64ndarray]
36+
pop( empty( 'uint8', sh, order ), {} ); // $ExpectType [uint8ndarray, uint8ndarray]
3337
}
3438

3539
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
3640
{
37-
pop( '10', 1 ); // $ExpectError
38-
pop( 10, 1 ); // $ExpectError
39-
pop( false, 1 ); // $ExpectError
40-
pop( true, 1 ); // $ExpectError
41-
pop( null, 1 ); // $ExpectError
42-
pop( [], 1 ); // $ExpectError
43-
pop( {}, 1 ); // $ExpectError
44-
pop( ( x: number ): number => x, 1 ); // $ExpectError
41+
pop( '10' ); // $ExpectError
42+
pop( 10 ); // $ExpectError
43+
pop( false ); // $ExpectError
44+
pop( true ); // $ExpectError
45+
pop( null ); // $ExpectError
46+
pop( [] ); // $ExpectError
47+
pop( {} ); // $ExpectError
48+
pop( ( x: number ): number => x ); // $ExpectError
49+
50+
pop( '10', {} ); // $ExpectError
51+
pop( 10, {} ); // $ExpectError
52+
pop( false, {} ); // $ExpectError
53+
pop( true, {} ); // $ExpectError
54+
pop( null, {} ); // $ExpectError
55+
pop( [], {} ); // $ExpectError
56+
pop( {}, {} ); // $ExpectError
57+
pop( ( x: number ): number => x, {} ); // $ExpectError
4558
}
4659

47-
// The compiler throws an error if the function is provided a second argument which is not an integer...
60+
// The compiler throws an error if the function is provided a second argument which is not an object...
4861
{
4962
const x = empty( 'float64', [ 2, 2 ], 'row-major' );
5063

5164
pop( x, '5' ); // $ExpectError
5265
pop( x, false ); // $ExpectError
5366
pop( x, true ); // $ExpectError
5467
pop( x, null ); // $ExpectError
55-
pop( x, undefined ); // $ExpectError
5668
pop( x, [ '5' ] ); // $ExpectError
57-
pop( x, {} ); // $ExpectError
5869
pop( x, ( x: number ): number => x ); // $ExpectError
5970
}
6071

72+
// The compiler throws an error if the function is provided a second argument with invalid `dim` option...
73+
{
74+
const x = empty( 'float64', [ 2, 2 ], 'row-major' );
75+
76+
pop( x, { 'dim':'5'} ); // $ExpectError
77+
pop( x, { 'dim': false} ); // $ExpectError
78+
pop( x, { 'dim': true} ); // $ExpectError
79+
pop( x, { 'dim': null} ); // $ExpectError
80+
pop( x, { 'dim': [ '5' ]} ); // $ExpectError
81+
pop( x, { 'dim': ( x: number ): number => x } ); // $ExpectError
82+
}
83+
6184
// The compiler throws an error if the function is provided an unsupported number of arguments...
6285
{
6386
const x = empty( 'float64', [ 2, 2 ], 'row-major' );
6487

65-
pop( x ); // $ExpectError
66-
pop( x, 1, {} ); // $ExpectError
88+
pop( x, {}, {} ); // $ExpectError
6789
}

lib/node_modules/@stdlib/ndarray/pop/lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
* var arr = ndarray2array( x );
4040
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
4141
*
42-
* var y = pop( x, 0 );
42+
* var y = pop( x );
4343
* // returns [ <ndarray>, <ndarray> ]
4444
*
4545
* arr = ndarray2array( y[ 0 ] );
46-
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
46+
* // returns [ [ 1 ], [ 3 ], [ 5 ] ]
4747
*
4848
* arr = ndarray2array( y[ 1 ] );
49-
* // returns [ [ 5.0, 6.0 ] ]
49+
* // returns [ [ 2 ], [ 4 ], [ 6 ] ]
5050
*/
5151

5252
// MODULES //

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
// MODULES //
2222

23+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2325
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
2426
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2527
var base = require( '@stdlib/ndarray/base/pop' );
@@ -32,7 +34,8 @@ var format = require( '@stdlib/string/format' );
3234
* Returns an array containing a read-only truncated view of an input ndarray and a read-only view of the last element(s) along a specified dimension.
3335
*
3436
* @param {ndarray} x - input array
35-
* @param {integer} dim - dimension along which to perform the operation
37+
* @param {Object} options - functions options
38+
* @param {integer} options.dim=-1 - dimension along which to perform the operation
3639
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
3740
* @throws {RangeError} dimension index exceeds the number of dimensions
3841
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
@@ -54,23 +57,38 @@ var format = require( '@stdlib/string/format' );
5457
* var arr = ndarray2array( x );
5558
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
5659
*
57-
* var y = pop( x, 0 );
60+
* var y = pop( x );
5861
* // returns [ <ndarray>, <ndarray> ]
5962
*
6063
* arr = ndarray2array( y[ 0 ] );
61-
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
64+
* // returns [ [ 1.0 ], [ 3.0 ], [ 5.0 ] ]
6265
*
6366
* arr = ndarray2array( y[ 1 ] );
64-
* // returns [ [ 5.0, 6.0 ] ]
67+
* // returns [ [ 2.0 ], [ 4.0 ], [ 6.0 ] ]
6568
*/
66-
function pop( x, dim ) {
69+
function pop( x ) {
70+
var options;
71+
var opts;
72+
73+
opts = {
74+
'dim': -1
75+
};
6776
if ( !isndarrayLike( x ) ) {
6877
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
6978
}
70-
if ( !isInteger( dim ) ) {
71-
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) );
79+
if ( arguments.length > 1 ) {
80+
options = arguments[ 1 ];
81+
if ( !isPlainObject( options ) ) {
82+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
83+
}
84+
if ( hasOwnProp( options, 'dim' ) ) {
85+
if ( !isInteger( options.dim ) ) {
86+
throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'dim', options.dim ) );
87+
}
88+
opts.dim = options.dim;
89+
}
7290
}
73-
return base( x, dim, false );
91+
return base( x, opts.dim, false );
7492
}
7593

7694

0 commit comments

Comments
 (0)