Skip to content

Commit 7e5f17b

Browse files
committed
feat: add array/base/entries2views
--- 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 62be202 commit 7e5f17b

File tree

11 files changed

+997
-0
lines changed

11 files changed

+997
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# entries2views
22+
23+
> Convert array entries to an array of composite views.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var entries2views = require( '@stdlib/array/base/entries2views' );
31+
```
32+
33+
#### entries2views( arr, fields )
34+
35+
Converts array entries to an array of composite views.
36+
37+
```javascript
38+
var x = [ 1, 2, 3 ];
39+
var fields = [ 'x', 'y' ];
40+
41+
var out = entries2views( x, fields );
42+
// returns [ <Object>, <Object>, <Object> ]
43+
44+
var v0 = out[ 0 ].toJSON();
45+
// returns { 'x': 0, 'y': 1 }
46+
47+
var v1 = out[ 1 ].toJSON();
48+
// returns { 'x': 1, 'y': 2 }
49+
50+
var v2 = out[ 2 ].toJSON();
51+
// returns { 'x': 2, 'y': 3 }
52+
53+
// Mutate the input array:
54+
x[ 0 ] = 5;
55+
56+
v0 = out[ 0 ].toJSON();
57+
// returns { 'x': 0, 'y': 5 }
58+
59+
// Set a view property:
60+
out[ 1 ].y = 'beep';
61+
62+
v1 = out[ 1 ].toJSON();
63+
// returns { 'x': 1, 'y': 'beep' }
64+
65+
var y = x.slice();
66+
// returns [ 5, 'beep', 3 ]
67+
```
68+
69+
The function supports the following parameters:
70+
71+
- **arr**: input array.
72+
- **fields**: list of field names.
73+
74+
Each element in the returned array is a class instance having prototype properties corresponding to the list of field names. As demonstrated in the example above, to convert an element to a regular object, invoke an element's `toJSON` method. Note, however, that the object returned by an element's `toJSON` method no longer shares the same memory as the provided input array.
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<section class="notes">
81+
82+
- 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.
83+
- For each element of the returned array, the index view field is read-only and cannot be mutated.
84+
- Each view in the returned array shares the same memory as the corresponding element in the input array. Accordingly, mutation of either the input array or a view will mutate the other.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
98+
var entries2views = require( '@stdlib/array/base/entries2views' );
99+
100+
var x = discreteUniform( 10, -100, 100 );
101+
var fields = [ 'x', 'y' ];
102+
103+
var out = entries2views( x, fields );
104+
// returns [...]
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.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 entries2views = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':len=100', function benchmark( b ) {
33+
var fields;
34+
var x;
35+
var i;
36+
var v;
37+
38+
fields = [ 'x', 'y' ];
39+
x = zeroTo( 10 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = entries2views( 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: 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 entries2views = 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 fields = [ '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 = entries2views( x, fields );
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( arr, fields )
3+
Converts array entries to an array of composite views.
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+
For each element of the returned array, the index view field is read-only
11+
and cannot be mutated.
12+
13+
Parameters
14+
----------
15+
arr: ArrayLikeObject
16+
Input array.
17+
18+
fields: ArrayLikeObject
19+
List of field names.
20+
21+
Returns
22+
-------
23+
out: Array<Object>
24+
Output array.
25+
26+
Examples
27+
--------
28+
> var x = [ 1, 2 ];
29+
> var f = [ 'x', 'y' ];
30+
> var out = {{alias}}( x, f );
31+
> var v = out[ 0 ].toJSON()
32+
{ 'x': 0, 'y': 1 }
33+
> v = out[ 1 ].toJSON()
34+
{ 'x': 1, 'y': 2 }
35+
36+
See Also
37+
--------
38+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 composite views.
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+
* - For each element of the returned array, the index view field is read-only and cannot be mutated.
37+
*
38+
* @param arr - input array
39+
* @param fields - list of field names
40+
* @returns output array
41+
*
42+
* @example
43+
* var x = [ 1, 2, 3 ];
44+
* var fields = [ 'x', 'y' ];
45+
*
46+
* var out = entries2views( x, fields );
47+
* // returns [ <Object>, <Object>, <Object> ]
48+
*
49+
* var v0 = out[ 0 ].toJSON();
50+
* // returns { 'x': 0, 'y': 1 }
51+
*
52+
* var v1 = out[ 1 ].toJSON();
53+
* // returns { 'x': 1, 'y': 2 }
54+
*
55+
* var v2 = out[ 2 ].toJSON();
56+
* // returns { 'x': 2, 'y': 3 }
57+
*
58+
* // Mutate the input array:
59+
* x[ 0 ] = 5;
60+
*
61+
* v0 = out[ 0 ].toJSON();
62+
* // returns { 'x': 0, 'y': 5 }
63+
*
64+
* // Set a view property:
65+
* out[ 1 ].y = 'beep';
66+
*
67+
* v1 = out[ 1 ].toJSON();
68+
* // returns { 'x': 1, 'y': 'beep' }
69+
*
70+
* var y = x.slice();
71+
* // returns [ 5, 'beep', 3 ]
72+
*/
73+
declare function entries2views<T = unknown, U extends PropertyKey = PropertyKey>( arr: Collection<T> | AccessorArrayLike<T>, fields: Collection<U> | AccessorArrayLike<U> ): Array<Record<U, T>>;
74+
75+
76+
// EXPORTS //
77+
78+
export = entries2views;

0 commit comments

Comments
 (0)