Skip to content

Commit 0c087b7

Browse files
committed
feat: add array/base/nested2objects
--- 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 5741019 commit 0c087b7

File tree

10 files changed

+664
-0
lines changed

10 files changed

+664
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
# nested2objects
22+
23+
> Convert nested arrays to objects.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var nested2objects = require( '@stdlib/array/base/nested2objects' );
31+
```
32+
33+
#### nested2objects( arr, fields )
34+
35+
Converts each nested array to an object.
36+
37+
```javascript
38+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
39+
40+
var fields = [ 'x', 'y' ];
41+
42+
var out = nested2objects( x, fields );
43+
// returns [ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ]
44+
```
45+
46+
The function supports the following parameters:
47+
48+
- **arr**: input array containing nested arrays.
49+
- **fields**: list of field names.
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
- The function assumes that all nested arrays have the same length.
58+
- The number of provided array labels should equal the length of each nested array.
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<section class="examples">
65+
66+
## Examples
67+
68+
<!-- eslint no-undef: "error" -->
69+
70+
```javascript
71+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
72+
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
73+
var nested2objects = require( '@stdlib/array/base/nested2objects' );
74+
75+
var x = filled2dBy( [ 10, 2 ], discreteUniform( -100, 100 ) );
76+
var fields = [ 'x', 'y' ];
77+
78+
var out = nested2objects( x, fields );
79+
// returns [...]
80+
```
81+
82+
</section>
83+
84+
<!-- /.examples -->
85+
86+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
87+
88+
<section class="related">
89+
90+
</section>
91+
92+
<!-- /.related -->
93+
94+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
95+
96+
<section class="links">
97+
98+
</section>
99+
100+
<!-- /.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 filled2d = require( '@stdlib/array/base/filled2d' );
26+
var pkg = require( './../package.json' ).name;
27+
var nested2objects = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':size=100', function benchmark( b ) {
33+
var fields;
34+
var x;
35+
var i;
36+
var v;
37+
38+
fields = [ 'x', 'y' ];
39+
x = filled2d( 10, [ 10, 10 ] );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = nested2objects( x, fields );
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arr, fields )
3+
Converts each nested array to an object.
4+
5+
The function assumes that all nested arrays have the same length.
6+
7+
The number of provided array labels should equal the length of each nested
8+
array.
9+
10+
Parameters
11+
----------
12+
arr: ArrayLikeObject<ArrayLikeObject>
13+
Input array containing nested arrays.
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 ], [ 3, 4 ] ];
26+
> var f = [ 'x', 'y' ];
27+
> var out = {{alias}}( x, f )
28+
[ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ]
29+
30+
See Also
31+
--------
32+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
* Converts each nested array to an object.
32+
*
33+
*
34+
* ## Notes
35+
*
36+
* - The function assumes that all nested arrays have the same length.
37+
* - The number of provided array labels should equal the length of each nested array.
38+
*
39+
* @param arr - input array
40+
* @param fields - list of field names
41+
* @returns output array
42+
*
43+
* @example
44+
* var x = [ [ 1, 2 ], [ 3, 4 ] ];
45+
* var fields = [ 'x', 'y' ];
46+
*
47+
* var out = nested2objects( x, fields );
48+
* // returns [ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ]
49+
*/
50+
declare function nested2objects<T = unknown, U extends PropertyKey = PropertyKey>( arr: ArrayLike<Collection<T> | AccessorArrayLike<T>>, fields: Collection<U> | AccessorArrayLike<U> ): Array<Record<U, T>>;
51+
52+
53+
// EXPORTS //
54+
55+
export = nested2objects;
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 nested2objects = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of objects...
25+
{
26+
nested2objects( [ [ 1, 2 ] ], [ '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+
nested2objects( 1, [ 'x', 'y' ] ); // $ExpectError
32+
nested2objects( true, [ 'x', 'y' ] ); // $ExpectError
33+
nested2objects( false, [ 'x', 'y' ] ); // $ExpectError
34+
nested2objects( null, [ 'x', 'y' ] ); // $ExpectError
35+
nested2objects( void 0, [ 'x', 'y' ] ); // $ExpectError
36+
nested2objects( {}, [ '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+
nested2objects( [], 1 ); // $ExpectError
42+
nested2objects( [], true ); // $ExpectError
43+
nested2objects( [], false ); // $ExpectError
44+
nested2objects( [], null ); // $ExpectError
45+
nested2objects( [], void 0 ); // $ExpectError
46+
nested2objects( [], {} ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
nested2objects(); // $ExpectError
52+
nested2objects( [] ); // $ExpectError
53+
nested2objects( [], [], [] ); // $ExpectError
54+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22+
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
23+
var nested2objects = require( './../lib' );
24+
25+
var x = filled2dBy( [ 10, 2 ], discreteUniform( -100, 100 ) );
26+
var fields = [ 'x', 'y' ];
27+
28+
var out = nested2objects( x, fields );
29+
console.log( out );
30+
// => [...]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
22+
* Convert nested arrays to objects.
23+
*
24+
* @module @stdlib/array/base/nested2objects
25+
*
26+
* @example
27+
* var nested2objects = require( '@stdlib/array/base/nested2objects' );
28+
*
29+
* var x = [ [ 1, 2 ], [ 3, 4 ] ];
30+
* var fields = [ 'x', 'y' ];
31+
*
32+
* var out = nested2objects( x, fields );
33+
* // returns [ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ]
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)