Skip to content

Commit 455c0d4

Browse files
committed
feat: add array/base/zip2object
--- 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 a2d3800 commit 455c0d4

File tree

11 files changed

+714
-0
lines changed

11 files changed

+714
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
# zip2object
22+
23+
> Create an object from a provided list of properties and a provided list of corresponding values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zip2object = require( '@stdlib/array/base/zip2object' );
31+
```
32+
33+
#### zip2object( properties, values )
34+
35+
Returns an object from a provided list of properties and a provided list of corresponding values.
36+
37+
```javascript
38+
var properties = [ 'a', 'b' ];
39+
var values = [ 1, 2 ];
40+
41+
var out = zip2object( properties, values );
42+
// returns { 'a': 1, 'b': 2 }
43+
```
44+
45+
The function supports the following parameters:
46+
47+
- **properties**: list of properties.
48+
- **values**: list of values.
49+
50+
</section>
51+
52+
<!-- /.usage -->
53+
54+
<section class="notes">
55+
56+
- The function assumes that both input arrays have the same length.
57+
58+
</section>
59+
60+
<!-- /.notes -->
61+
62+
<section class="examples">
63+
64+
## Examples
65+
66+
<!-- eslint no-undef: "error" -->
67+
68+
```javascript
69+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
70+
var zeroTo = require( '@stdlib/array/base/zero-to' );
71+
var zip2object = require( '@stdlib/array/base/zip2object' );
72+
73+
var x1 = zeroTo( 10 );
74+
var x2 = discreteUniform( x1.length, -100, 100 );
75+
76+
var out = zip2object( x1, x2 );
77+
// returns {...}
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
85+
86+
<section class="related">
87+
88+
</section>
89+
90+
<!-- /.related -->
91+
92+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
93+
94+
<section class="links">
95+
96+
</section>
97+
98+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 zip2object = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':len=100', function benchmark( b ) {
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = zeroTo( 10 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
v = zip2object( x, x );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an object' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isPlainObject( v ) ) {
48+
b.fail( 'should return an object' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
27+
var pkg = require( './../package.json' ).name;
28+
var zip2object = 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 x = zeroTo( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = zip2object( x, x );
57+
if ( typeof v !== 'object' ) {
58+
b.fail( 'should return an object' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isPlainObject( v ) ) {
63+
b.fail( 'should return an object' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+len, f );
92+
}
93+
}
94+
95+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( properties, values )
3+
Creates an object from a provided list of properties and a provided list of
4+
corresponding values.
5+
6+
The function assumes that both input arrays are the same length.
7+
8+
Parameters
9+
----------
10+
properties: ArrayLikeObject
11+
List of properties.
12+
13+
values: ArrayLikeObject
14+
List of values.
15+
16+
Returns
17+
-------
18+
out: Object
19+
Result.
20+
21+
Examples
22+
--------
23+
> var x1 = [ 1, 2 ];
24+
> var x2 = [ 3, 4 ];
25+
> var out = {{alias}}( x1, x2 )
26+
{ '1': 3, '2': 4 }
27+
28+
See Also
29+
--------
30+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 } from '@stdlib/types/array';
24+
25+
/**
26+
* Property key.
27+
*/
28+
type PropertyKey = string | number | symbol;
29+
30+
/**
31+
* Creates an object from a provided list of properties and a provided list of corresponding values.
32+
*
33+
* ## Notes
34+
*
35+
* - The function assumes that both input arrays are the same length.
36+
*
37+
* @param properties - list of properties
38+
* @param values - list of values
39+
* @returns result
40+
*
41+
* @example
42+
* var properties = [ 1, 2 ];
43+
* var values = [ 3, 4 ];
44+
*
45+
* var out = zip2object( properties, values );
46+
* // returns { '1': 3, '2': 4 }
47+
*/
48+
declare function zip2object<T extends PropertyKey = PropertyKey, U = unknown>( properties: Collection<T> | AccessorArrayLike<T>, values: Collection<U> | AccessorArrayLike<U> ): Record<T, U>;
49+
50+
51+
// EXPORTS //
52+
53+
export = zip2object;
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+
import zip2object = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object...
25+
{
26+
zip2object( [ 1, 2 ], [ 1, 3 ] ); // $ExpectType Record<1 | 2, number>
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
30+
{
31+
zip2object( 1, [ 1, 3 ] ); // $ExpectError
32+
zip2object( true, [ 1, 3 ] ); // $ExpectError
33+
zip2object( false, [ 1, 3 ] ); // $ExpectError
34+
zip2object( null, [ 1, 3 ] ); // $ExpectError
35+
zip2object( void 0, [ 1, 3 ] ); // $ExpectError
36+
zip2object( {}, [ 1, 3 ] ); // $ExpectError
37+
}
38+
39+
// The compiler throws an error if the function is provided a second argument which is not an array-like object...
40+
{
41+
zip2object( [], 1 ); // $ExpectError
42+
zip2object( [], true ); // $ExpectError
43+
zip2object( [], false ); // $ExpectError
44+
zip2object( [], null ); // $ExpectError
45+
zip2object( [], void 0 ); // $ExpectError
46+
zip2object( [], {} ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
zip2object(); // $ExpectError
52+
zip2object( [] ); // $ExpectError
53+
zip2object( [], [], [] ); // $ExpectError
54+
}

0 commit comments

Comments
 (0)