Skip to content

Commit 035abe2

Browse files
committed
Refactor to support array-like objects
1 parent 896a9d4 commit 035abe2

File tree

8 files changed

+133
-36
lines changed

8 files changed

+133
-36
lines changed

lib/node_modules/@stdlib/array/shape/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,8 @@ var shape = arrayShape( arr );
9191
```javascript
9292
var arrayShape = require( '@stdlib/array/shape' );
9393

94-
var shape;
95-
var arr;
96-
97-
arr = [ 1, 2, 3 ];
98-
shape = arrayShape( arr );
94+
var arr = [ 1, 2, 3 ];
95+
var shape = arrayShape( arr );
9996
// returns [ 3 ]
10097

10198
arr = [

lib/node_modules/@stdlib/array/shape/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Parameters
66
----------
7-
arr: Array
7+
arr: ArrayLikeObject
88
Input array.
99

1010
Returns

lib/node_modules/@stdlib/array/shape/docs/types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
// TypeScript Version: 2.0
2020

21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
2125
/**
2226
* Determines (nested) array dimensions.
2327
*
@@ -42,7 +46,7 @@
4246
* var shape = arrayShape( arr );
4347
* // returns [ 3 ]
4448
*/
45-
declare function arrayShape( arr: Array<any> ): Array<number>;
49+
declare function arrayShape( arr: ArrayLike<any> ): Array<number>;
4650

4751

4852
// EXPORTS //

lib/node_modules/@stdlib/array/shape/docs/types/test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@
1818

1919
import arrayShape = require( './index' );
2020

21+
2122
// TESTS //
2223

2324
// The function returns an array of numbers..
2425
{
2526
arrayShape( [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ); // $ExpectType number[]
2627
}
2728

29+
// The compiler throws an error if not provided an array-like object...
30+
{
31+
arrayShape( 5 ); // $ExpectError
32+
arrayShape( false ); // $ExpectError
33+
arrayShape( true ); // $ExpectError
34+
arrayShape( null ); // $ExpectError
35+
arrayShape( undefined ); // $ExpectError
36+
arrayShape( {} ); // $ExpectError
37+
}
38+
2839
// The compiler throws an error if the function is provided an unsupported number of arguments...
2940
{
3041
arrayShape(); // $ExpectError

lib/node_modules/@stdlib/array/shape/examples/index.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020

2121
var arrayShape = require( './../lib' );
2222

23-
var shape;
24-
var arr;
25-
26-
arr = [ 1, 2, 3 ];
27-
shape = arrayShape( arr );
23+
var arr = [ 1, 2, 3 ];
24+
var shape = arrayShape( arr );
2825
console.log( shape.join( 'x' ) );
29-
// => 3
26+
// => '3'
3027

3128
arr = [
3229
[ 1 ],
@@ -35,7 +32,7 @@ arr = [
3532
];
3633
shape = arrayShape( arr );
3734
console.log( shape.join( 'x' ) );
38-
// => 3x1
35+
// => '3x1'
3936

4037
arr = [
4138
[],
@@ -44,14 +41,14 @@ arr = [
4441
];
4542
shape = arrayShape( arr );
4643
console.log( shape.join( 'x' ) );
47-
// => 3x0
44+
// => '3x0'
4845

4946
arr = [
5047
[ 1, 2, 3 ]
5148
];
5249
shape = arrayShape( arr );
5350
console.log( shape.join( 'x' ) );
54-
// => 1x3
51+
// => '1x3'
5552

5653
arr = [
5754
[ [ 1 ] ],
@@ -60,20 +57,20 @@ arr = [
6057
];
6158
shape = arrayShape( arr );
6259
console.log( shape.join( 'x' ) );
63-
// => 3x1x1
60+
// => '3x1x1'
6461

6562
arr = [ [ [ [ 1, 2, 3 ] ] ] ];
6663
shape = arrayShape( arr );
6764
console.log( shape.join( 'x' ) );
68-
// => 1x1x1x3
65+
// => '1x1x1x3'
6966

7067
arr = [
7168
[ 1, 2 ],
7269
[ 3, 4 ]
7370
];
7471
shape = arrayShape( arr );
7572
console.log( shape.join( 'x' ) );
76-
// => 2x2
73+
// => '2x2'
7774

7875
arr = [
7976
[ 1, 2, 3 ],
@@ -82,7 +79,7 @@ arr = [
8279
];
8380
shape = arrayShape( arr );
8481
console.log( shape.join( 'x' ) );
85-
// => 3x3
82+
// => '3x3'
8683

8784
arr = [
8885
[ 1, 2, 3 ],
@@ -91,7 +88,7 @@ arr = [
9188
];
9289
shape = arrayShape( arr );
9390
console.log( shape.join( 'x' ) );
94-
// => 3
91+
// => '3'
9592

9693
arr = [
9794
[ 1, 2, 3 ],
@@ -100,7 +97,7 @@ arr = [
10097
];
10198
shape = arrayShape( arr );
10299
console.log( shape.join( 'x' ) );
103-
// => 3
100+
// => '3'
104101

105102
arr = [
106103
[ [ 1, 2, 3 ] ],
@@ -109,7 +106,7 @@ arr = [
109106
];
110107
shape = arrayShape( arr );
111108
console.log( shape.join( 'x' ) );
112-
// => 3
109+
// => '3'
113110

114111
arr = [
115112
[ [ 1, 2, 3 ] ],
@@ -118,7 +115,7 @@ arr = [
118115
];
119116
shape = arrayShape( arr );
120117
console.log( shape.join( 'x' ) );
121-
// => 3
118+
// => '3'
122119

123120
arr = [
124121
[ [ [ 1, 2, 3 ] ] ],
@@ -127,4 +124,4 @@ arr = [
127124
];
128125
shape = arrayShape( arr );
129126
console.log( shape.join( 'x' ) );
130-
// => 3x1
127+
// => '3x1'

lib/node_modules/@stdlib/array/shape/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050

5151
// MODULES //
5252

53-
var arrayShape = require( './main.js' );
53+
var main = require( './main.js' );
5454

5555

5656
// EXPORTS //
5757

58-
module.exports = arrayShape;
58+
module.exports = main;

lib/node_modules/@stdlib/array/shape/lib/main.js

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

2121
// MODULES //
2222

23-
var isArray = require( '@stdlib/assert/is-array' );
23+
var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
2424

2525

2626
// FUNCTIONS //
@@ -30,12 +30,12 @@ var isArray = require( '@stdlib/assert/is-array' );
3030
*
3131
* @private
3232
* @param {Array} shape - output array
33-
* @param {Array} arr - array
33+
* @param {ArrayLikeObject} arr - array
3434
* @returns {Array} shape array
3535
*/
3636
function recurse( shape, arr ) {
3737
var v = arr[ 0 ];
38-
if ( isArray( v ) ) {
38+
if ( isArrayLikeObject( v ) ) {
3939
shape.push( v.length );
4040
recurse( shape, v );
4141
}
@@ -49,7 +49,7 @@ function recurse( shape, arr ) {
4949
* @param {PositiveInteger} ndims - number of dimensions
5050
* @param {Array} shape - shape array
5151
* @param {NonNegativeInteger} d - dimension
52-
* @param {Array} arr - array element to verify
52+
* @param {ArrayLikeObject} arr - array element to verify
5353
* @param {boolean} flg - boolean indicating whether to continue recursing
5454
* @returns {NonNegativeInteger} number of consistent dimensions
5555
*/
@@ -66,7 +66,7 @@ function check( ndims, shape, d, arr, flg ) {
6666
v = arr[ i ];
6767

6868
// If the array element is not an array or is not the same size, we have found an inconsistent dimension:
69-
if ( !isArray( v ) || v.length !== len ) {
69+
if ( !isArrayLikeObject( v ) || v.length !== len ) {
7070
// `d` is one more than the index of the last consistent dimension and thus equal to the number of consistent dimensions:
7171
return d;
7272
}
@@ -88,7 +88,7 @@ function check( ndims, shape, d, arr, flg ) {
8888
/**
8989
* Determines (nested) array dimensions.
9090
*
91-
* @param {Array} arr - array
91+
* @param {ArrayLikeObject} arr - array
9292
* @throws {TypeError} must provide an array
9393
* @returns {Array} array shape
9494
*
@@ -114,8 +114,8 @@ function arrayShape( arr ) {
114114
var shape;
115115
var ndims;
116116

117-
if ( !isArray( arr ) ) {
118-
throw new TypeError( 'invalid argument. Must provide an array. Value: `' + arr + '`.' );
117+
if ( !isArrayLikeObject( arr ) ) {
118+
throw new TypeError( 'invalid argument. Must provide an array-like object. Value: `' + arr + '`.' );
119119
}
120120
// Initialize the shape/dimensions array:
121121
shape = [ arr.length ];

lib/node_modules/@stdlib/array/shape/test/test.js

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var Float64Array = require( '@stdlib/array/float64' );
2425
var arrayShape = require( './../lib' );
2526

2627

@@ -32,7 +33,7 @@ tape( 'main export is a function', function test( t ) {
3233
t.end();
3334
});
3435

35-
tape( 'the function throws an error if not provided an array', function test( t ) {
36+
tape( 'the function throws an error if not provided an array-like object', function test( t ) {
3637
var values;
3738
var i;
3839

@@ -90,6 +91,38 @@ tape( 'the function returns array dimensions (0d)', function test( t ) {
9091
t.end();
9192
});
9293

94+
tape( 'the function returns array dimensions (0d; array-like objects)', function test( t ) {
95+
var arr;
96+
var x;
97+
98+
arr = new Float64Array( [] );
99+
t.deepEqual( arrayShape( arr ), [ 0 ], 'returns expected value' );
100+
101+
x = new Float64Array( [] );
102+
arr = [ x ];
103+
t.deepEqual( arrayShape( arr ), [ 1, 0 ], 'returns expected value' );
104+
105+
arr = [ [ x ] ];
106+
t.deepEqual( arrayShape( arr ), [ 1, 1, 0 ], 'returns expected value' );
107+
108+
arr = [ x, x ];
109+
t.deepEqual( arrayShape( arr ), [ 2, 0 ], 'returns expected value' );
110+
111+
arr = [ [ x ], [ x ] ];
112+
t.deepEqual( arrayShape( arr ), [ 2, 1, 0 ], 'returns expected value' );
113+
114+
arr = [ x, x, x ];
115+
t.deepEqual( arrayShape( arr ), [ 3, 0 ], 'returns expected value' );
116+
117+
arr = [ [ [ x ] ], [ [ x ] ], [ [ x ] ] ];
118+
t.deepEqual( arrayShape( arr ), [ 3, 1, 1, 0 ], 'returns expected value' );
119+
120+
arr = [ [ [ [ x ] ] ], [ [ [ x ] ] ], [ [ [ x ] ] ] ];
121+
t.deepEqual( arrayShape( arr ), [ 3, 1, 1, 1, 0 ], 'returns expected value' );
122+
123+
t.end();
124+
});
125+
93126
tape( 'the function returns array dimensions (1d)', function test( t ) {
94127
var arr;
95128

@@ -117,6 +150,33 @@ tape( 'the function returns array dimensions (1d)', function test( t ) {
117150
t.end();
118151
});
119152

153+
tape( 'the function returns array dimensions (1d; array-like object)', function test( t ) {
154+
var arr;
155+
156+
arr = new Float64Array( [ 1 ] );
157+
t.deepEqual( arrayShape( arr ), [ 1 ], 'returns expected value' );
158+
159+
arr = new Float64Array( [ 'beep' ] );
160+
t.deepEqual( arrayShape( arr ), [ 1 ], 'returns expected value' );
161+
162+
arr = new Float64Array( [ 1, 2 ] );
163+
t.deepEqual( arrayShape( arr ), [ 2 ], 'returns expected value' );
164+
165+
arr = new Float64Array( [ 1, 2, 3 ] );
166+
t.deepEqual( arrayShape( arr ), [ 3 ], 'returns expected value' );
167+
168+
arr = [ new Float64Array( [ 1 ] ), [ 2 ], 3 ];
169+
t.deepEqual( arrayShape( arr ), [ 3 ], 'returns expected value' );
170+
171+
arr = [ new Float64Array( [ 1 ] ), [ 2 ], null ];
172+
t.deepEqual( arrayShape( arr ), [ 3 ], 'returns expected value' );
173+
174+
arr = [ [ [ 1 ] ], [ new Float64Array( [ 2 ] ) ], null ];
175+
t.deepEqual( arrayShape( arr ), [ 3 ], 'returns expected value' );
176+
177+
t.end();
178+
});
179+
120180
tape( 'the function returns array dimensions (2d)', function test( t ) {
121181
var arr;
122182

@@ -145,6 +205,34 @@ tape( 'the function returns array dimensions (2d)', function test( t ) {
145205
t.end();
146206
});
147207

208+
tape( 'the function returns array dimensions (2d; array-like objects)', function test( t ) {
209+
var arr;
210+
211+
arr = [ new Float64Array( [ 1 ] ) ];
212+
t.deepEqual( arrayShape( arr ), [ 1, 1 ], 'returns expected value' );
213+
214+
arr = [ new Float64Array( [ 1 ] ), [ 2 ] ];
215+
t.deepEqual( arrayShape( arr ), [ 2, 1 ], 'returns expected value' );
216+
217+
arr = [ [ 1 ], new Float64Array( [ 2 ] ), [ 3 ] ];
218+
t.deepEqual( arrayShape( arr ), [ 3, 1 ], 'returns expected value' );
219+
220+
arr = [ [ [ 1 ] ], [ new Float64Array( [ 2 ] ) ], [ 3 ] ];
221+
t.deepEqual( arrayShape( arr ), [ 3, 1 ], 'returns expected value' );
222+
223+
arr = [ [ new Float64Array( [ 1 ] ) ], [ [ 2 ] ], [ null ] ];
224+
t.deepEqual( arrayShape( arr ), [ 3, 1 ], 'returns expected value' );
225+
226+
arr = [
227+
new Float64Array( [ 1, 2, 3 ] ),
228+
new Float64Array( [ 4, 5, 6 ] ),
229+
new Float64Array( [ 7, 8, 9 ] )
230+
];
231+
t.deepEqual( arrayShape( arr ), [ 3, 3 ], 'returns expected value' );
232+
233+
t.end();
234+
});
235+
148236
tape( 'the function returns array dimensions (3d)', function test( t ) {
149237
var arr;
150238

0 commit comments

Comments
 (0)