Skip to content

Commit f0d6c2c

Browse files
committed
Auto-generated commit
1 parent 15a96d4 commit f0d6c2c

File tree

12 files changed

+903
-0
lines changed

12 files changed

+903
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Features
1212

13+
- [`25a75d7`](https://github.com/stdlib-js/stdlib/commit/25a75d7ca2026a01c949a10d04a3a668b40e2e13) - add `groupValuesOnKey` to namespace
14+
- [`30a49c3`](https://github.com/stdlib-js/stdlib/commit/30a49c3a5c27d0a5d4cf8b75a82802faba76a145) - add `array/base/group-values-on-key`
1315
- [`1535192`](https://github.com/stdlib-js/stdlib/commit/1535192162a5b372854dd62cb028c2c5e32d7c0a) - add `nested2views` to namespace
1416
- [`e484f42`](https://github.com/stdlib-js/stdlib/commit/e484f42a6bd37fcafb4309bcecad5c4270c132cc) - add `array/base/nested2views`
1517
- [`e83fc9a`](https://github.com/stdlib-js/stdlib/commit/e83fc9abdd3c0bbd6d7ba91794ced8774757aeae) - add `nested2objects` to namespace
@@ -237,6 +239,8 @@ A total of 32 issues were closed in this release:
237239

238240
<details>
239241

242+
- [`25a75d7`](https://github.com/stdlib-js/stdlib/commit/25a75d7ca2026a01c949a10d04a3a668b40e2e13) - **feat:** add `groupValuesOnKey` to namespace _(by Athan Reines)_
243+
- [`30a49c3`](https://github.com/stdlib-js/stdlib/commit/30a49c3a5c27d0a5d4cf8b75a82802faba76a145) - **feat:** add `array/base/group-values-on-key` _(by Athan Reines)_
240244
- [`d25ee66`](https://github.com/stdlib-js/stdlib/commit/d25ee665c0ae01b053bb4aeae117db1a27a282fd) - **bench:** fix sample array _(by Athan Reines)_
241245
- [`4fd7c3c`](https://github.com/stdlib-js/stdlib/commit/4fd7c3c4193f809283db6672754b35bbe00fd79f) - **bench:** fix sample array _(by Athan Reines)_
242246
- [`1535192`](https://github.com/stdlib-js/stdlib/commit/1535192162a5b372854dd62cb028c2c5e32d7c0a) - **feat:** add `nested2views` to namespace _(by Athan Reines)_

base/group-values-on-key/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
# groupValuesOnKey
22+
23+
> Group the elements of an array according to a specified property name.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var groupValuesOnKey = require( '@stdlib/array/base/group-values-on-key' );
41+
```
42+
43+
#### groupValuesOnKey( x, key )
44+
45+
Groups the elements of an array according to a specified property name.
46+
47+
```javascript
48+
var x = [
49+
{
50+
'x': 1,
51+
'y': 2
52+
},
53+
{
54+
'x': 1,
55+
'y': 3
56+
}
57+
];
58+
59+
var out = groupValuesOnKey( x, 'y' );
60+
// returns { '2': [ { 'x': 1, 'y': 2 } ], '3': [ { 'x': 1, 'y': 3 } ] }
61+
```
62+
63+
The function supports the following parameters:
64+
65+
- **x**: input array.
66+
- **key**: property name whose values are used to determine groups.
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
73+
74+
<section class="notes">
75+
76+
## Notes
77+
78+
- Each value associated with a specified `key` should resolve to a value which can be serialized as an object key. As a counterexample,
79+
80+
```javascript
81+
var x = [
82+
{
83+
'x': 1,
84+
'y': {}
85+
},
86+
{
87+
'x': 1,
88+
'y': {}
89+
}
90+
];
91+
92+
var out = groupValuesOnKey( x, 'y' );
93+
// returns { '[object Object]': [ { 'x': 1, 'y': {} }, { 'x': 1, 'y': {} } ] }
94+
```
95+
96+
while each "group" is unique, all input array elements resolve to the same group because each group identifier serializes to the same string.
97+
98+
- If provided an empty array, the function returns an empty object.
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<!-- Package usage examples. -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
114+
var filledBy = require( '@stdlib/array/base/filled-by' );
115+
var groupValuesOnKey = require( '@stdlib/array/base/group-values-on-key' );
116+
117+
function clbk( idx ) {
118+
return {
119+
'x': idx,
120+
'y': discreteUniform( 0, 10 )
121+
};
122+
}
123+
124+
// Create an array of random objects:
125+
var values = filledBy( 100, clbk );
126+
127+
// Group elements according to the values of `y`:
128+
var out = groupValuesOnKey( values, 'y' );
129+
// returns {...}
130+
131+
console.log( out );
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="references">
141+
142+
</section>
143+
144+
<!-- /.references -->
145+
146+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147+
148+
<section class="related">
149+
150+
</section>
151+
152+
<!-- /.related -->
153+
154+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="links">
157+
158+
</section>
159+
160+
<!-- /.links -->
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
26+
var filledBy = require( './../../../base/filled-by' );
27+
var pkg = require( './../package.json' ).name;
28+
var groupValuesOnKey = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Returns an object.
35+
*
36+
* @private
37+
* @param {NonNegativeInteger} idx - element index
38+
* @returns {Object} object
39+
*/
40+
function clbk( idx ) {
41+
return {
42+
'x': idx,
43+
'y': idx
44+
};
45+
}
46+
47+
/**
48+
* Creates a benchmark function.
49+
*
50+
* @private
51+
* @param {PositiveInteger} len - array length
52+
* @returns {Function} benchmark function
53+
*/
54+
function createBenchmark( len ) {
55+
var x = filledBy( len, clbk );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var out;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
out = groupValuesOnKey( x, 'y' );
71+
if ( typeof out !== 'object' ) {
72+
b.fail( 'should return an object' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isPlainObject( out ) ) {
77+
b.fail( 'should return an object' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
105+
f = createBenchmark( len );
106+
bench( pkg+':len='+len+',num_groups='+len, f );
107+
}
108+
}
109+
110+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, key )
3+
Groups the elements of an array according to a specified property name.
4+
5+
If provided an empty array, the function returns an empty object.
6+
7+
Parameters
8+
----------
9+
x: ArrayLike
10+
Input array.
11+
12+
key: string|number|symbol
13+
Property name whose values are used to determine groups. Each value
14+
associated with a specified `key` should resolve to a value which can be
15+
serialized as an object key.
16+
17+
Returns
18+
-------
19+
out: Object
20+
Group results.
21+
22+
Examples
23+
--------
24+
> var x = [ { 'x': 1, 'y': 2 }, { 'x': 1, 'y': 3 } ];
25+
> var out = {{alias}}( x, 'y' )
26+
{ '2': [ { 'x': 1, 'y': 2 } ], '3': [ { 'x': 1, 'y': 3 } ] }
27+
28+
See Also
29+
--------
30+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
* Object key.
27+
*/
28+
type Key = string | symbol | number;
29+
30+
/**
31+
* Groups the elements of an array according to a specified property name.
32+
*
33+
* @param x - input array
34+
* @param key - property name whose values are used to determine groups
35+
* @returns group results
36+
*
37+
* @example
38+
* var x = [
39+
* {
40+
* 'x': 1,
41+
* 'y': 2
42+
* },
43+
* {
44+
* 'x': 1,
45+
* 'y': 3
46+
* }
47+
* ];
48+
*
49+
* var out = groupValuesOnKey( x, 'y' );
50+
* // returns { '2': [ { 'x': 1, 'y': 2 } ], '3': [ { 'x': 1, 'y': 3 } ] }
51+
*/
52+
declare function groupValuesOnKey<T extends Record<Key, any>, K extends keyof T>( x: Collection<T> | AccessorArrayLike<T>, key: K ): Record<string, Array<T>>;
53+
54+
55+
// EXPORTS //
56+
57+
export = groupValuesOnKey;

0 commit comments

Comments
 (0)