Skip to content

Commit 44a8a87

Browse files
committed
feat: add array/base/zip2objects
--- 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: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - 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 40d9627 commit 44a8a87

File tree

11 files changed

+881
-0
lines changed

11 files changed

+881
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# zip2objects
22+
23+
> Zip one or more arrays to an array of objects.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zip2objects = require( '@stdlib/array/base/zip2objects' );
31+
```
32+
33+
#### zip2objects( arrays, labels )
34+
35+
Zips one or more arrays to an array of objects.
36+
37+
```javascript
38+
var x = [ 1, 2 ];
39+
var y = [ 3, 4 ];
40+
41+
var labels = [ 'x', 'y' ];
42+
43+
var out = zip2objects( [ x, y ], labels );
44+
// returns [ { 'x': 1, 'y': 3 }, { 'x': 2, 'y': 4 } ]
45+
```
46+
47+
The function supports the following parameters:
48+
49+
- **arrays**: list of arrays to zip.
50+
- **labels**: list of array labels.
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<section class="notes">
57+
58+
- The function assumes that the list of arrays to be zipped all have the same length.
59+
- The list of provided array labels should equal the number of arrays to be zipped.
60+
61+
</section>
62+
63+
<!-- /.notes -->
64+
65+
<section class="examples">
66+
67+
## Examples
68+
69+
<!-- eslint no-undef: "error" -->
70+
71+
```javascript
72+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
73+
var zeroTo = require( '@stdlib/array/base/zero-to' );
74+
var zip2objects = require( '@stdlib/array/base/zip2objects' );
75+
76+
var x = zeroTo( 10 );
77+
var y = discreteUniform( x.length, -100, 100 );
78+
79+
var labels = [ 'x', 'y' ];
80+
81+
var out = zip2objects( [ x, y ], labels );
82+
// returns [...]
83+
```
84+
85+
</section>
86+
87+
<!-- /.examples -->
88+
89+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
90+
91+
<section class="related">
92+
93+
</section>
94+
95+
<!-- /.related -->
96+
97+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="links">
100+
101+
</section>
102+
103+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var zip2objects = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':len=100', function benchmark( b ) {
33+
var labels;
34+
var x;
35+
var i;
36+
var v;
37+
38+
labels = [ 'x', 'y' ];
39+
x = zeroTo( 10 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = zip2objects( [ x, x ], labels );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isPlainObject( v ) ) {
50+
b.fail( 'should return an object' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var zip2objects = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var labels = [ 'x', 'y' ];
42+
var x = zeroTo( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var v;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = zip2objects( [ x, x ], labels );
58+
if ( typeof v !== 'object' ) {
59+
b.fail( 'should return an array' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isArray( v ) ) {
64+
b.fail( 'should return an array' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( arrays, labels )
3+
Zips one or more arrays to an array of objects.
4+
5+
The function assumes that the list of arrays to be zipped all have the same
6+
length.
7+
8+
The list of provided array labels should equal the number of arrays to be
9+
zipped.
10+
11+
Parameters
12+
----------
13+
arrays: ArrayLikeObject<ArrayLikeObject>
14+
List of arrays to zip.
15+
16+
labels: ArrayLikeObject
17+
List of array labels.
18+
19+
Returns
20+
-------
21+
out: Array<Object>
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = [ 1, 2 ];
27+
> var y = [ 3, 4 ];
28+
> var labels = [ 'x', 'y' ];
29+
> var out = {{alias}}( [ x, y ], labels )
30+
[ { 'x': 1, 'y': 3 }, { 'x': 2, 'y': 4 } ]
31+
32+
See Also
33+
--------
34+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection, AccessorArrayLike, ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Property key.
27+
*/
28+
type PropertyKey = string | number | symbol;
29+
30+
/**
31+
* Zips one or more arrays to an array of objects.
32+
*
33+
* ## Notes
34+
*
35+
* - The function assumes that the list of arrays to be zipped all have the same length.
36+
* - The list of provided array labels should equal the number of arrays to be zipped.
37+
*
38+
* @param arrays - list of arrays to be zipped
39+
* @param labels - list of array labels
40+
* @returns output array
41+
*
42+
* @example
43+
* var x = [ 1, 2, 3 ];
44+
* var y = [ 'a', 'b', 'c' ];
45+
*
46+
* var labels = [ 'x', 'y' ];
47+
*
48+
* var z = zip2objects( [ x, y ], labels );
49+
* // returns [ { 'x': 1, 'y': 'a' }, { 'x': 2, 'y': 'b' }, { 'x': 3, 'y': 'c' } ]
50+
*/
51+
declare function zip2objects<T = unknown, U extends PropertyKey = PropertyKey>( arrays: ArrayLike<Collection<T> | AccessorArrayLike<T>>, labels: Collection<U> | AccessorArrayLike<U> ): Array<Record<U, T>>;
52+
53+
54+
// EXPORTS //
55+
56+
export = zip2objects;

0 commit comments

Comments
 (0)