Skip to content

Commit 867fec5

Browse files
committed
Auto-generated commit
1 parent fe90a0e commit 867fec5

File tree

12 files changed

+1233
-1
lines changed

12 files changed

+1233
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2025-07-28)
7+
## Unreleased (2025-07-29)
88

99
<section class="features">
1010

1111
### Features
1212

13+
- [`c8567dc`](https://github.com/stdlib-js/stdlib/commit/c8567dc46194708845553af0631c54cc81b25170) - add `rekeyViews` to namespace
14+
- [`768090d`](https://github.com/stdlib-js/stdlib/commit/768090def5056f9bb863878fd253b35e23a1f786) - add `array/base/rekey-views`
1315
- [`d5a7391`](https://github.com/stdlib-js/stdlib/commit/d5a73912f19ca4825ce2f361fa4a37d193030ee3) - add `rekey` to namespace
1416
- [`74dc3c3`](https://github.com/stdlib-js/stdlib/commit/74dc3c37f9b0c66051ba2ab143035cb3a4460970) - add `array/base/rekey`
1517
- [`25a75d7`](https://github.com/stdlib-js/stdlib/commit/25a75d7ca2026a01c949a10d04a3a668b40e2e13) - add `groupValuesOnKey` to namespace
@@ -244,6 +246,8 @@ A total of 32 issues were closed in this release:
244246

245247
<details>
246248

249+
- [`c8567dc`](https://github.com/stdlib-js/stdlib/commit/c8567dc46194708845553af0631c54cc81b25170) - **feat:** add `rekeyViews` to namespace _(by Athan Reines)_
250+
- [`768090d`](https://github.com/stdlib-js/stdlib/commit/768090def5056f9bb863878fd253b35e23a1f786) - **feat:** add `array/base/rekey-views` _(by Athan Reines)_
247251
- [`d3b0f25`](https://github.com/stdlib-js/stdlib/commit/d3b0f251b37fdc27cd3a02ac794dddba0b2f7b36) - **fix:** correct loop bounds in toJSON method for `nested2views` _(by Philipp Burckhardt)_
248252
- [`fbc5910`](https://github.com/stdlib-js/stdlib/commit/fbc5910c2290928c2fdc730a6b166b39e637d304) - **fix:** remove unnecessary loop in toJSON method for entries2views _(by Philipp Burckhardt)_
249253
- [`8cdfd62`](https://github.com/stdlib-js/stdlib/commit/8cdfd621717a7a41428526107a42364a921b07ae) - **fix:** ensure proper cache invalidation _(by Athan Reines)_

base/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,15 @@ setReadOnly( ns, 'reject', require( './../../base/reject' ) );
15841584
*/
15851585
setReadOnly( ns, 'rekey', require( './../../base/rekey' ) );
15861586

1587+
/**
1588+
* @name rekeyViews
1589+
* @memberof ns
1590+
* @readonly
1591+
* @type {Function}
1592+
* @see {@link module:@stdlib/array/base/rekey-views}
1593+
*/
1594+
setReadOnly( ns, 'rekeyViews', require( './../../base/rekey-views' ) );
1595+
15871596
/**
15881597
* @name removeAt
15891598
* @memberof ns

base/rekey-views/README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
# rekeyViews
22+
23+
> Create an array containing views with renamed keys for every element in a provided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var rekeyViews = require( '@stdlib/array/base/rekey-views' );
31+
```
32+
33+
#### rekeyViews( arr, mapping )
34+
35+
Returns an array containing views with renamed keys for every element in a provided array.
36+
37+
```javascript
38+
var x = [
39+
{
40+
'x': 1,
41+
'y': 2
42+
},
43+
{
44+
'x': 3,
45+
'y': 4
46+
}
47+
];
48+
var mapping = {
49+
'x': 'a',
50+
'y': 'b'
51+
};
52+
53+
var out = rekeyViews( x, mapping );
54+
// returns [ <Object>, <Object> ]
55+
56+
var v0 = out[ 0 ].toJSON();
57+
// returns { 'a': 1, 'b': 2 }
58+
59+
var v1 = out[ 1 ].toJSON();
60+
// returns { 'a': 3, 'b': 4 }
61+
62+
// Mutate the first element in the input array:
63+
x[ 0 ].x = 5;
64+
65+
v0 = out[ 0 ].toJSON();
66+
// returns { 'a': 5, 'b': 2 }
67+
68+
// Set a view property:
69+
out[ 1 ].b = 'beep';
70+
71+
v1 = out[ 1 ].toJSON();
72+
// returns { 'a': 3, 'b': 'beep' }
73+
74+
var y = x.slice();
75+
// returns [ { 'x': 5, 'y': 2 }, { 'x': 3, 'y': 'beep' } ]
76+
```
77+
78+
The function has the following parameters:
79+
80+
- **arr**: input array.
81+
- **mapping**: object mapping existing keys to new key names.
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<section class="notes">
88+
89+
- The function returns views having **only** those keys which are present in a provided mapping object. Any keys which are **not** present in the provided mapping object, but are present in the original objects, are **omitted** during view creation.
90+
- The function assumes that each object has the keys specified in a provided mapping object.
91+
- Each view in the returned array shares the same memory as the corresponding elements in the input arrays. Accordingly, mutation of either an array element or a view will mutate the other.
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
105+
var filledBy = require( '@stdlib/array/base/filled-by' );
106+
var rekeyViews = require( '@stdlib/array/base/rekey-views' );
107+
108+
function clbk( idx ) {
109+
return {
110+
'x': idx,
111+
'y': discreteUniform( 0, 10 )
112+
};
113+
}
114+
115+
var x = filledBy( 10, clbk );
116+
var mapping = {
117+
'x': 'a',
118+
'y': 'b'
119+
};
120+
121+
var out = rekeyViews( x, mapping );
122+
// returns [...]
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
</section>
142+
143+
<!-- /.links -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 filledBy = require( './../../../base/filled-by' );
26+
var pkg = require( './../package.json' ).name;
27+
var rekeyViews = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Returns an object.
34+
*
35+
* @private
36+
* @param {NonNegativeInteger} idx - element index
37+
* @returns {Object} object
38+
*/
39+
function clbk( idx ) {
40+
return {
41+
'x': idx,
42+
'y': idx
43+
};
44+
}
45+
46+
47+
// MAIN //
48+
49+
bench( pkg+':size=100', function benchmark( b ) {
50+
var mapping;
51+
var x;
52+
var i;
53+
var v;
54+
55+
mapping = {
56+
'x': 'a',
57+
'y': 'b'
58+
};
59+
x = filledBy( 100, clbk );
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
v = rekeyViews( x, mapping );
64+
if ( typeof v !== 'object' ) {
65+
b.fail( 'should return an array' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isArray( v ) ) {
70+
b.fail( 'should return an array' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});

base/rekey-views/docs/repl.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( arr, mapping )
3+
Returns an array containing views with renamed keys for every element in a
4+
provided array.
5+
6+
The function returns views having only those keys which are present in a
7+
provided mapping object. Any keys which are not present in the provided
8+
mapping object, but are present in the original objects, are omitted during
9+
view creation.
10+
11+
The function assumes that each object has the keys specified in a provided
12+
mapping object.
13+
14+
Each view in the returned array shares the same memory as the corresponding
15+
elements in the input arrays. Accordingly, mutation of either an array
16+
element or a view will mutate the other.
17+
18+
Parameters
19+
----------
20+
arr: ArrayLikeObject<Object>
21+
Input array.
22+
23+
mapping: Object
24+
Object mapping existing keys to new key names.
25+
26+
Returns
27+
-------
28+
out: Array<Object>
29+
Output array.
30+
31+
Examples
32+
--------
33+
> var x = [ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ];
34+
> var m = { 'x': 'a', 'y': 'b' };
35+
> var out = {{alias}}( x, m );
36+
> var v = out[ 0 ].toJSON()
37+
{ 'a': 1, 'b': 2 }
38+
> v = out[ 1 ].toJSON()
39+
{ 'a': 3, 'b': 4 }
40+
41+
See Also
42+
--------
43+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
* Returns an array containing views with renamed keys for every element in a provided array.
32+
*
33+
* ## Notes
34+
*
35+
* - The function returns views having only those keys which are present in a provided mapping object. Any keys which are not present in the provided mapping object, but are present in the original objects, are omitted during view creation.
36+
* - The function assumes that each object has the keys specified in a provided mapping object.
37+
* - Each view in the returned array shares the same memory as the corresponding elements in the input arrays. Accordingly, mutation of either an array element or a view will mutate the other.
38+
*
39+
* @param arr - input array
40+
* @param mapping - object mapping existing keys to new key names
41+
* @returns output array
42+
*
43+
* @example
44+
* var x = [
45+
* {
46+
* 'x': 1,
47+
* 'y': 2
48+
* },
49+
* {
50+
* 'x': 3,
51+
* 'y': 4
52+
* }
53+
* ];
54+
* var mapping = {
55+
* 'x': 'a',
56+
* 'y': 'b'
57+
* };
58+
*
59+
* var out = rekeyViews( x, mapping );
60+
* // returns [ <Object>, <Object> ]
61+
*
62+
* var v0 = out[ 0 ].toJSON();
63+
* // returns { 'a': 1, 'b': 2 }
64+
*
65+
* var v1 = out[ 1 ].toJSON();
66+
* // returns { 'a': 3, 'b': 4 }
67+
*
68+
* // Mutate the first element in the input array:
69+
* x[ 0 ].x = 5;
70+
*
71+
* v0 = out[ 0 ].toJSON();
72+
* // returns { 'a': 5, 'b': 2 }
73+
*
74+
* // Set a view property:
75+
* out[ 1 ].b = 'beep';
76+
*
77+
* v1 = out[ 1 ].toJSON();
78+
* // returns { 'a': 3, 'b': 'beep' }
79+
*
80+
* var y = x.slice();
81+
* // returns [ { 'x': 5, 'y': 2 }, { 'x': 3, 'y': 'beep' } ]
82+
*/
83+
declare function rekeyViews<T extends Record<PropertyKey, any>, OldKey extends keyof T, Mapping extends Record<OldKey, PropertyKey>>( arr: Collection<T> | AccessorArrayLike<T>, mapping: Mapping ): Array<{ [ K in OldKey as Mapping[K] ]: T[K] }>;
84+
85+
86+
// EXPORTS //
87+
88+
export = rekeyViews;

0 commit comments

Comments
 (0)