Skip to content

Commit 26a8f56

Browse files
committed
Refactor to allow nested array-like objects
1 parent 035abe2 commit 26a8f56

19 files changed

Lines changed: 183 additions & 212 deletions

File tree

lib/node_modules/@stdlib/utils/flatten-array/README.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Flatten Array
21+
# flattenArray
2222

2323
> Flatten an array.
2424
@@ -32,7 +32,7 @@ var flattenArray = require( '@stdlib/utils/flatten-array' );
3232

3333
#### flattenArray( arr\[, options] )
3434

35-
Flattens an `array`.
35+
Flattens an array.
3636

3737
```javascript
3838
var arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
@@ -44,7 +44,7 @@ var out = flattenArray( arr );
4444
The function accepts the following `options`:
4545

4646
- **depth**: maximum depth to flatten.
47-
- **copy**: `boolean` indicating whether to deep [copy][@stdlib/utils/copy] `array` elements. Default: `false`.
47+
- **copy**: `boolean` indicating whether to deep [copy][@stdlib/utils/copy] array elements. Default: `false`.
4848

4949
To flatten to a specified depth, set the `depth` option.
5050

@@ -60,7 +60,7 @@ var bool = ( arr[1][1][1] === out[3] );
6060
// returns true
6161
```
6262

63-
To deep [copy][@stdlib/utils/copy] `array` elements, set the `copy` option to `true`.
63+
To deep [copy][@stdlib/utils/copy] array elements, set the `copy` option to `true`.
6464

6565
```javascript
6666
var arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
@@ -77,7 +77,7 @@ var bool = ( arr[1][1][1] === out[3] );
7777

7878
#### flattenArray.factory( dims\[, options] )
7979

80-
Returns a `function` optimized for flattening `arrays` having specified dimensions.
80+
Returns a `function` optimized for flattening arrays having specified dimensions.
8181

8282
```javascript
8383
var flatten = flattenArray.factory( [ 3, 3 ] );
@@ -103,9 +103,9 @@ out = flatten( arr );
103103

104104
The function accepts the following `options`:
105105

106-
- **copy**: `boolean` indicating whether to deep [copy][@stdlib/utils/copy] `array` elements. Default: `false`.
106+
- **copy**: `boolean` indicating whether to deep [copy][@stdlib/utils/copy] array elements. Default: `false`.
107107

108-
To deep [copy][@stdlib/utils/copy] `array` elements, set the `copy` option to `true`.
108+
To deep [copy][@stdlib/utils/copy] array elements, set the `copy` option to `true`.
109109

110110
<!-- eslint-disable object-curly-newline -->
111111

@@ -136,6 +136,7 @@ var bool = ( arr[1][1] === out[4] );
136136
## Notes
137137

138138
- A flatten `function` returned by the factory method does **not** validate that input `arrays` actually have the specified dimensions.
139+
- The `factory` method uses code evaluation, which may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP).
139140

140141
</section>
141142

@@ -152,50 +153,48 @@ var bool = ( arr[1][1] === out[4] );
152153
```javascript
153154
var flattenArray = require( '@stdlib/utils/flatten-array' );
154155

155-
var xStride;
156-
var yStride;
157-
var zStride;
158-
var bool;
159-
var tmp1;
160-
var tmp2;
161-
var arr;
162-
var val;
163-
var out;
164-
var N;
165-
var M;
166-
var L;
167-
var i;
168-
var j;
169-
var k;
170-
171-
N = 1000;
172-
M = 100;
173-
L = 10;
174-
175-
// Create an NxMxL (3D) array...
176-
arr = new Array( N );
177-
for ( i = 0; i < N; i++ ) {
178-
tmp1 = new Array( M );
179-
for ( j = 0; j < M; j++ ) {
180-
tmp2 = new Array( L );
181-
for ( k = 0; k < L; k++ ) {
182-
tmp2[ k ] = (M*L*i) + (j*L) + k + 1;
156+
function tensor( N, M, L ) {
157+
var tmp1;
158+
var tmp2;
159+
var out;
160+
var i;
161+
var j;
162+
var k;
163+
164+
out = [];
165+
for ( i = 0; i < N; i++ ) {
166+
tmp1 = [];
167+
for ( j = 0; j < M; j++ ) {
168+
tmp2 = [];
169+
for ( k = 0; k < L; k++ ) {
170+
tmp2.push( (M*L*i) + (j*L) + k + 1 );
171+
}
172+
tmp1.push( tmp2 );
183173
}
184-
tmp1[ j ] = tmp2;
174+
out.push( tmp1 );
185175
}
186-
arr[ i ] = tmp1;
176+
return out;
187177
}
188-
// Create a flattened (strided) array:
189-
out = flattenArray( arr );
190-
191-
// To access the arr[4][20][2] element...
192-
xStride = M * L;
193-
yStride = L;
194-
zStride = 1;
195-
val = out[ (4*xStride) + (20*yStride) + (2*zStride) ];
178+
179+
// Define array dimensions:
180+
var N = 1000;
181+
var M = 100;
182+
var L = 10;
183+
184+
// Create a 3-dimensional nested array:
185+
var data = tensor( N, M, L );
186+
187+
// Create a flattened (strided) array from a 3-dimensional nested array:
188+
var arr = flattenArray( data );
189+
190+
// To access the data[4][20][2] element...
191+
var xStride = M * L;
192+
var yStride = L;
193+
var zStride = 1;
194+
var v = arr[ (4*xStride) + (20*yStride) + (2*zStride) ];
196195
// returns 4203
197196

198-
bool = ( arr[4][20][2] === val );
197+
var bool = ( data[4][20][2] === v );
199198
// returns true
200199
```
201200

@@ -223,6 +222,8 @@ bool = ( arr[4][20][2] === val );
223222

224223
[@stdlib/utils/copy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/copy
225224

225+
[mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
226+
226227
<!-- <related-links> -->
227228

228229
[@stdlib/utils/flatten-object]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/flatten-object

lib/node_modules/@stdlib/utils/flatten-array/benchmark/benchmark.large_arrays.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ bench( pkg+'::large_arrays', function benchmark( b ) {
4545
L = 10;
4646

4747
// Create an NxMxL (3D) array...
48-
data = new Array( N );
48+
data = [];
4949
for ( i = 0; i < N; i++ ) {
50-
tmp1 = new Array( M );
50+
tmp1 = [];
5151
for ( j = 0; j < M; j++ ) {
52-
tmp2 = new Array( L );
52+
tmp2 = [];
5353
for ( k = 0; k < L; k++ ) {
54-
tmp2[ k ] = (M*L*i) + (j*L) + k + 1;
54+
tmp2.push( (M*L*i) + (j*L) + k + 1 );
5555
}
56-
tmp1[ j ] = tmp2;
56+
tmp1.push( tmp2 );
5757
}
58-
data[ i ] = tmp1;
58+
data.push( tmp1 );
5959
}
6060
b.tic();
6161
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/utils/flatten-array/docs/repl.txt

Lines changed: 2 additions & 2 deletions
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
options: Object (optional)
@@ -50,7 +50,7 @@
5050

5151
Parameters
5252
----------
53-
dims: Array<integer>
53+
dims: ArrayLike<integer>
5454
Dimensions.
5555

5656
options: Object (optional)

lib/node_modules/@stdlib/utils/flatten-array/docs/types/index.d.ts

Lines changed: 7 additions & 3 deletions
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
interface Options {
2226
/**
2327
* Boolean indicating whether to deep copy array elements.
@@ -43,7 +47,7 @@ interface FactoryOptions {
4347
* @param arr - array to flatten
4448
* @returns flattened array
4549
*/
46-
type Unary = ( arr: Array<any> ) => Array<any>;
50+
type FlattenFunction = ( arr: ArrayLike<any> ) => Array<any>;
4751

4852
/**
4953
* Interface for the flattenArray function.
@@ -64,7 +68,7 @@ interface FlattenArray {
6468
* var out = flattenArray( arr );
6569
* // returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
6670
*/
67-
( arr: Array<any>, options?: Options ): Array<any>;
71+
( arr: ArrayLike<any>, options?: Options ): Array<any>;
6872

6973
/**
7074
* Returns a function for flattening arrays having specified dimensions.
@@ -89,7 +93,7 @@ interface FlattenArray {
8993
* out = flatten( [[5,6],[7,8]] );
9094
* // returns [ 5, 6, 7, 8 ]
9195
*/
92-
factory( dims: Array<number>, options?: FactoryOptions ): Unary;
96+
factory( dims: ArrayLike<number>, options?: FactoryOptions ): FlattenFunction;
9397
}
9498

9599
/**

lib/node_modules/@stdlib/utils/flatten-array/docs/types/test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ import flattenArray = require( './index' );
2424
// The function returns an array...
2525
{
2626
const arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
27+
2728
flattenArray( arr ); // $ExpectType any[]
2829
}
2930

30-
// The compiler throws an error if the function is provided a first argument which is not an array...
31+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
3132
{
32-
flattenArray( 'abc' ); // $ExpectError
3333
flattenArray( 123 ); // $ExpectError
3434
flattenArray( null ); // $ExpectError
3535
flattenArray( {} ); // $ExpectError
36-
flattenArray( ( x: number ): number => x ); // $ExpectError
3736
}
3837

3938
// The compiler throws an error if the function is provided a second argument which is not an object...
4039
{
4140
const arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
41+
4242
flattenArray( arr, null ); // $ExpectError
4343
}
4444

4545
// The compiler throws an error if the function is provided a `copy` option which is not a boolean...
4646
{
4747
const arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
48+
4849
flattenArray( arr, { 'copy': '5' } ); // $ExpectError
4950
flattenArray( arr, { 'copy': 123 } ); // $ExpectError
5051
flattenArray( arr, { 'copy': null } ); // $ExpectError
@@ -56,6 +57,7 @@ import flattenArray = require( './index' );
5657
// The compiler throws an error if the function is provided a `depth` option which is not a number...
5758
{
5859
const arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
60+
5961
flattenArray( arr, { 'depth': true } ); // $ExpectError
6062
flattenArray( arr, { 'depth': false } ); // $ExpectError
6163
flattenArray( arr, { 'depth': 'abc' } ); // $ExpectError
@@ -72,12 +74,13 @@ import flattenArray = require( './index' );
7274

7375
// Attached to main export is a `factory` method which returns a function...
7476
{
75-
flattenArray.factory( [ 2, 2 ] ); // $ExpectType Unary
77+
flattenArray.factory( [ 2, 2 ] ); // $ExpectType FlattenFunction
7678
}
7779

7880
// The `factory` method returns a function which returns an array...
7981
{
8082
const fcn = flattenArray.factory( [ 2, 2 ] );
83+
8184
fcn( [ [ 1, 2 ], [ 3, 4 ] ] ); // $ExpectType any[]
8285
}
8386

@@ -98,6 +101,7 @@ import flattenArray = require( './index' );
98101
// The compiler throws an error if the `factory` method is provided a `copy` option which is not a boolean...
99102
{
100103
const dims = [ 2, 2 ];
104+
101105
flattenArray.factory( dims, { 'copy': '5' } ); // $ExpectError
102106
flattenArray.factory( dims, { 'copy': 123 } ); // $ExpectError
103107
flattenArray.factory( dims, { 'copy': null } ); // $ExpectError
@@ -109,18 +113,18 @@ import flattenArray = require( './index' );
109113
// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments...
110114
{
111115
const fcn = flattenArray.factory( [ 2, 2 ] );
116+
112117
fcn( true ); // $ExpectError
113118
fcn( false ); // $ExpectError
114-
fcn( '5' ); // $ExpectError
115119
fcn( 123 ); // $ExpectError
116120
fcn( {} ); // $ExpectError
117-
fcn( ( x: number ): number => x ); // $ExpectError
118121
}
119122

120123
// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments...
121124
{
122125
const arr = [ [ 1, 2 ], [ 3, 4 ] ];
123126
const fcn = flattenArray.factory( [ 2, 2 ] );
127+
124128
fcn(); // $ExpectError
125129
fcn( arr, 0 ); // $ExpectError
126130
}

lib/node_modules/@stdlib/utils/flatten-array/examples/custom.js

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

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

23-
var flatten;
24-
var arr;
25-
var out;
26-
2723
// Create a flatten function customized for flattening 3x3 arrays:
28-
flatten = flattenArray.factory( [3, 3] );
24+
var flatten = flattenArray.factory( [3, 3] );
2925

30-
arr = [
26+
var arr = [
3127
[ 1, 2, 3 ],
3228
[ 4, 5, 6 ],
3329
[ 7, 8, 9 ]
3430
];
3531

36-
out = flatten( arr );
32+
var out = flatten( arr );
3733
console.log( out );
3834
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

lib/node_modules/@stdlib/utils/flatten-array/examples/custom_copy.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,19 @@
2222

2323
var flattenArray = require( './../lib' );
2424

25-
var flatten;
26-
var opts;
27-
var arr;
28-
var out;
29-
30-
opts = {
25+
var opts = {
3126
'copy': true
3227
};
3328

34-
flatten = flattenArray.factory( [3, 3], opts );
29+
var flatten = flattenArray.factory( [3, 3], opts );
3530

36-
arr = [
31+
var arr = [
3732
[ 1, 2, 3 ],
3833
[ 4, {'x': 5}, 6 ],
3934
[ 7, 8, 9 ]
4035
];
4136

42-
out = flatten( arr );
37+
var out = flatten( arr );
4338
console.log( out );
4439
// => [ 1, 2, 3, 4, {'x':5}, 6, 7, 8, 9 ]
4540

lib/node_modules/@stdlib/utils/flatten-array/examples/deep_copy.js

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

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

23-
var opts;
24-
var arr;
25-
var out;
23+
var arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
2624

27-
arr = [ 1, [2, [3, [4, [ 5 ], 6], 7], 8], 9 ];
28-
29-
opts = {
25+
var opts = {
3026
'depth': 2,
3127
'copy': true
3228
};
3329

34-
out = flattenArray( arr, opts );
30+
var out = flattenArray( arr, opts );
3531
console.log( out );
3632
// => [ 1, 2, 3, [4, [ 5 ], 6], 7, 8, 9 ]
3733

0 commit comments

Comments
 (0)