Skip to content

Commit 027becd

Browse files
committed
feat: add support for accessor arrays
1 parent 60355b1 commit 027becd

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

lib/node_modules/@stdlib/array/base/cartesian-product/docs/types/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { Collection } from '@stdlib/types/array';
23+
import { Collection, AccessorArrayLike } from '@stdlib/types/array';
2424

2525
/**
2626
* Returns the Cartesian product.
@@ -40,7 +40,7 @@ import { Collection } from '@stdlib/types/array';
4040
* var out = cartesianProduct( x1, x2 );
4141
* // returns [ [ 1, 4 ], [ 1, 5 ], [ 2, 4 ], [ 2, 5 ], [ 3, 4 ], [ 3, 5 ] ]
4242
*/
43-
declare function cartesianProduct<T = unknown, U = unknown>( x1: Collection<T>, x2: Collection<U> ): Array<[T, U]>;
43+
declare function cartesianProduct<T = unknown, U = unknown>( x1: Collection<T> | AccessorArrayLike<T>, x2: Collection<U> | AccessorArrayLike<U> ): Array<[T, U]>;
4444

4545

4646
// EXPORTS //

lib/node_modules/@stdlib/array/base/cartesian-product/lib/main.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var resolveGetter = require( '@stdlib/array/base/resolve-getter' );
24+
25+
2126
// MAIN //
2227

2328
/**
@@ -35,20 +40,25 @@
3540
* // returns [ [ 1, 4 ], [ 1, 5 ], [ 2, 4 ], [ 2, 5 ], [ 3, 4 ], [ 3, 5 ] ]
3641
*/
3742
function cartesianProduct( x1, x2 ) {
43+
var get1;
44+
var get2;
3845
var out;
3946
var M;
4047
var N;
4148
var v;
4249
var i;
4350
var j;
4451

52+
get1 = resolveGetter( x1 );
53+
get2 = resolveGetter( x2 );
54+
4555
M = x1.length;
4656
N = x2.length;
4757
out = [];
4858
for ( i = 0; i < M; i++ ) {
49-
v = x1[ i ];
59+
v = get1( x1, i );
5060
for ( j = 0; j < N; j++ ) {
51-
out.push( [ v, x2[ j ] ] );
61+
out.push( [ v, get2( x2, j ) ] );
5262
}
5363
}
5464
return out;

lib/node_modules/@stdlib/array/base/cartesian-product/test/test.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable max-len */
20+
1921
'use strict';
2022

2123
// MODULES //
2224

2325
var tape = require( 'tape' );
26+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2427
var cartesianProduct = require( './../lib' );
2528

2629

@@ -32,7 +35,7 @@ tape( 'main export is a function', function test( t ) {
3235
t.end();
3336
});
3437

35-
tape( 'the function returns the Cartesian product', function test( t ) {
38+
tape( 'the function returns the Cartesian product (indexed)', function test( t ) {
3639
var expected;
3740
var actual;
3841

@@ -55,7 +58,30 @@ tape( 'the function returns the Cartesian product', function test( t ) {
5558
t.end();
5659
});
5760

58-
tape( 'the function returns an empty array if provided one or more empty arrays', function test( t ) {
61+
tape( 'the function returns the Cartesian product (accessors)', function test( t ) {
62+
var expected;
63+
var actual;
64+
65+
actual = cartesianProduct( toAccessorArray( [ 1, 2 ] ), toAccessorArray( [ 3, 4 ] ) );
66+
expected = [ [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ] ];
67+
t.deepEqual( actual, expected, 'returns expected value' );
68+
69+
actual = cartesianProduct( toAccessorArray( [ 1 ] ), toAccessorArray( [ 3, 4 ] ) );
70+
expected = [ [ 1, 3 ], [ 1, 4 ] ];
71+
t.deepEqual( actual, expected, 'returns expected value' );
72+
73+
actual = cartesianProduct( toAccessorArray( [ 1, 2 ] ), toAccessorArray( [ 3 ] ) );
74+
expected = [ [ 1, 3 ], [ 2, 3 ] ];
75+
t.deepEqual( actual, expected, 'returns expected value' );
76+
77+
actual = cartesianProduct( toAccessorArray( [ 1, 2 ] ), toAccessorArray( [ 3, 4, 5 ] ) );
78+
expected = [ [ 1, 3 ], [ 1, 4 ], [ 1, 5 ], [ 2, 3 ], [ 2, 4 ], [ 2, 5 ] ];
79+
t.deepEqual( actual, expected, 'returns expected value' );
80+
81+
t.end();
82+
});
83+
84+
tape( 'the function returns an empty array if provided one or more empty arrays (indexed)', function test( t ) {
5985
var actual;
6086

6187
actual = cartesianProduct( [], [] );
@@ -69,3 +95,18 @@ tape( 'the function returns an empty array if provided one or more empty arrays'
6995

7096
t.end();
7197
});
98+
99+
tape( 'the function returns an empty array if provided one or more empty arrays (accessors)', function test( t ) {
100+
var actual;
101+
102+
actual = cartesianProduct( toAccessorArray( [] ), toAccessorArray( [] ) );
103+
t.deepEqual( actual, [], 'returns expected value' );
104+
105+
actual = cartesianProduct( toAccessorArray( [ 1, 2 ] ), toAccessorArray( [] ) );
106+
t.deepEqual( actual, [], 'returns expected value' );
107+
108+
actual = cartesianProduct( toAccessorArray( [] ), toAccessorArray( [ 3, 4 ] ) );
109+
t.deepEqual( actual, [], 'returns expected value' );
110+
111+
t.end();
112+
});

0 commit comments

Comments
 (0)