Skip to content

Commit f3d7a2f

Browse files
committed
feat: add array/base/entries2objects
--- 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 0b79b81 commit f3d7a2f

File tree

11 files changed

+696
-0
lines changed

11 files changed

+696
-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+
# entries2objects
22+
23+
> Convert array entries to an array of objects.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var entries2objects = require( '@stdlib/array/base/entries2objects' );
31+
```
32+
33+
#### entries2objects( arr, fields )
34+
35+
Converts array entries to an array of objects.
36+
37+
```javascript
38+
var x = [ 1, 2 ];
39+
40+
var fields = [ 'x', 'y' ];
41+
42+
var out = entries2objects( x, fields );
43+
// returns [ { 'x': 0, 'y': 1 }, { 'x': 1, 'y': 2 } ]
44+
```
45+
46+
The function supports the following parameters:
47+
48+
- **arr**: input array.
49+
- **fields**: list of field names.
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
- The list of field names should be a two-element array where the first element corresponds to the field name of input array element index and the second element corresponds to the field name of the input array element value.
58+
59+
</section>
60+
61+
<!-- /.notes -->
62+
63+
<section class="examples">
64+
65+
## Examples
66+
67+
<!-- eslint no-undef: "error" -->
68+
69+
```javascript
70+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
71+
var entries2objects = require( '@stdlib/array/base/entries2objects' );
72+
73+
var x = discreteUniform( 10, -100, 100 );
74+
var fields = [ 'x', 'y' ];
75+
76+
var out = entries2objects( x, fields );
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: 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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var entries2objects = 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 = entries2objects( x, labels );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( v ) ) {
50+
b.fail( 'should return an array' );
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 entries2objects = 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 = entries2objects( 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arr, fields )
3+
Converts array entries to an array of objects.
4+
5+
The list of field names should be a two-element array where the first
6+
element corresponds to the field name of input array element index and the
7+
second element corresponds to the field name of the input array element
8+
value.
9+
10+
Parameters
11+
----------
12+
arr: ArrayLikeObject
13+
Input array.
14+
15+
fields: ArrayLikeObject
16+
List of field names.
17+
18+
Returns
19+
-------
20+
out: Array<Object>
21+
Output array.
22+
23+
Examples
24+
--------
25+
> var x = [ 1, 2 ];
26+
> var f = [ 'x', 'y' ];
27+
> var out = {{alias}}( x, f )
28+
[ { 'x': 0, 'y': 1 }, { 'x': 1, 'y': 2 } ]
29+
30+
See Also
31+
--------
32+
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+
* Converts array entries to an array of objects.
32+
*
33+
* ## Notes
34+
*
35+
* - The list of field names should be a two-element array where the first element corresponds to the field name of input array element index and the second element corresponds to the field name of the input array element value.
36+
*
37+
* @param arr - input array
38+
* @param fields - list of field names
39+
* @returns output array
40+
*
41+
* @example
42+
* var x = [ 1, 2, 3 ];
43+
* var fields = [ 'x', 'y' ];
44+
*
45+
* var out = entries2objects( x, fields );
46+
* // returns [ { 'x': 0, 'y': 1 }, { 'x': 1, 'y': 2 }, { 'x': 2, 'y': 3 } ]
47+
*/
48+
declare function entries2objects<T = unknown, U extends PropertyKey = PropertyKey>( arr: Collection<T> | AccessorArrayLike<T>, fields: Collection<U> | AccessorArrayLike<U> ): Array<Record<U, T>>;
49+
50+
51+
// EXPORTS //
52+
53+
export = entries2objects;
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 entries2objects = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of objects...
25+
{
26+
entries2objects( [ 1, 2, 3 ], [ 'x', 'y' ] ); // $ExpectType Record<"x" | "y", 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+
entries2objects( 1, [ 'x', 'y' ] ); // $ExpectError
32+
entries2objects( true, [ 'x', 'y' ] ); // $ExpectError
33+
entries2objects( false, [ 'x', 'y' ] ); // $ExpectError
34+
entries2objects( null, [ 'x', 'y' ] ); // $ExpectError
35+
entries2objects( void 0, [ 'x', 'y' ] ); // $ExpectError
36+
entries2objects( {}, [ 'x', 'y' ] ); // $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+
entries2objects( [], 1 ); // $ExpectError
42+
entries2objects( [], true ); // $ExpectError
43+
entries2objects( [], false ); // $ExpectError
44+
entries2objects( [], null ); // $ExpectError
45+
entries2objects( [], void 0 ); // $ExpectError
46+
entries2objects( [], {} ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
entries2objects(); // $ExpectError
52+
entries2objects( [] ); // $ExpectError
53+
entries2objects( [], [], [] ); // $ExpectError
54+
}

0 commit comments

Comments
 (0)