Skip to content

Commit c238662

Browse files
committed
Auto-generated commit
1 parent f0d6c2c commit c238662

File tree

12 files changed

+790
-0
lines changed

12 files changed

+790
-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+
- [`d5a7391`](https://github.com/stdlib-js/stdlib/commit/d5a73912f19ca4825ce2f361fa4a37d193030ee3) - add `rekey` to namespace
14+
- [`74dc3c3`](https://github.com/stdlib-js/stdlib/commit/74dc3c37f9b0c66051ba2ab143035cb3a4460970) - add `array/base/rekey`
1315
- [`25a75d7`](https://github.com/stdlib-js/stdlib/commit/25a75d7ca2026a01c949a10d04a3a668b40e2e13) - add `groupValuesOnKey` to namespace
1416
- [`30a49c3`](https://github.com/stdlib-js/stdlib/commit/30a49c3a5c27d0a5d4cf8b75a82802faba76a145) - add `array/base/group-values-on-key`
1517
- [`1535192`](https://github.com/stdlib-js/stdlib/commit/1535192162a5b372854dd62cb028c2c5e32d7c0a) - add `nested2views` to namespace
@@ -239,6 +241,8 @@ A total of 32 issues were closed in this release:
239241

240242
<details>
241243

244+
- [`d5a7391`](https://github.com/stdlib-js/stdlib/commit/d5a73912f19ca4825ce2f361fa4a37d193030ee3) - **feat:** add `rekey` to namespace _(by Athan Reines)_
245+
- [`74dc3c3`](https://github.com/stdlib-js/stdlib/commit/74dc3c37f9b0c66051ba2ab143035cb3a4460970) - **feat:** add `array/base/rekey` _(by Athan Reines)_
242246
- [`25a75d7`](https://github.com/stdlib-js/stdlib/commit/25a75d7ca2026a01c949a10d04a3a668b40e2e13) - **feat:** add `groupValuesOnKey` to namespace _(by Athan Reines)_
243247
- [`30a49c3`](https://github.com/stdlib-js/stdlib/commit/30a49c3a5c27d0a5d4cf8b75a82802faba76a145) - **feat:** add `array/base/group-values-on-key` _(by Athan Reines)_
244248
- [`d25ee66`](https://github.com/stdlib-js/stdlib/commit/d25ee665c0ae01b053bb4aeae117db1a27a282fd) - **bench:** fix sample array _(by Athan Reines)_

base/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,15 @@ setReadOnly( ns, 'quinary5d', require( './../../base/quinary5d' ) );
15751575
*/
15761576
setReadOnly( ns, 'reject', require( './../../base/reject' ) );
15771577

1578+
/**
1579+
* @name rekey
1580+
* @memberof ns
1581+
* @readonly
1582+
* @type {Function}
1583+
* @see {@link module:@stdlib/array/base/rekey}
1584+
*/
1585+
setReadOnly( ns, 'rekey', require( './../../base/rekey' ) );
1586+
15781587
/**
15791588
* @name removeAt
15801589
* @memberof ns

base/rekey/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# rekey
22+
23+
> Copy and rename specified keys for every element in a provided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var rekey = require( '@stdlib/array/base/rekey' );
31+
```
32+
33+
#### rekey( arr, mapping )
34+
35+
Copies and renames specified 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 = rekey( x, mapping );
54+
// returns [ { 'a': 1, 'b': 2 }, { 'a': 3, 'b': 4 } ]
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **arr**: input array.
60+
- **mapping**: object mapping existing keys to new key names.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<section class="notes">
67+
68+
- The function **only** copies and renames 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 object creation.
69+
- The function assumes that each object has the keys specified in a provided mapping object.
70+
- The function performs **shallow** copies of key values.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
84+
var filledBy = require( '@stdlib/array/base/filled-by' );
85+
var rekey = require( '@stdlib/array/base/rekey' );
86+
87+
function clbk( idx ) {
88+
return {
89+
'x': idx,
90+
'y': discreteUniform( 0, 10 )
91+
};
92+
}
93+
94+
var x = filledBy( 10, clbk );
95+
var mapping = {
96+
'x': 'a',
97+
'y': 'b'
98+
};
99+
100+
var out = rekey( x, mapping );
101+
// returns [...]
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
</section>
121+
122+
<!-- /.links -->

base/rekey/benchmark/benchmark.js

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 rekey = 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 = rekey( 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/docs/repl.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( arr, mapping )
3+
Copies and renames specified keys for every element in a provided array.
4+
5+
The function only copies and renames those keys which are present in a
6+
provided mapping object. Any keys which are not present in the provided
7+
mapping object, but are present in the original objects, are omitted during
8+
object creation.
9+
10+
The function assumes that each object has the keys specified in a provided
11+
mapping object.
12+
13+
The function performs shallow copies of key values.
14+
15+
Parameters
16+
----------
17+
arr: ArrayLikeObject<Object>
18+
Input array.
19+
20+
mapping: Object
21+
Object mapping existing keys to new key names.
22+
23+
Returns
24+
-------
25+
out: Array<Object>
26+
Output array.
27+
28+
Examples
29+
--------
30+
> var x = [ { 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 } ];
31+
> var m = { 'x': 'a', 'y': 'b' };
32+
> var out = {{alias}}( x, m )
33+
[ { 'a': 1, 'b': 2 }, { 'a': 3, 'b': 4 } ]
34+
35+
See Also
36+
--------
37+

base/rekey/docs/types/index.d.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
* Copies and renames specified keys for every element in a provided array.
32+
*
33+
* ## Notes
34+
*
35+
* - The function only copies and renames 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 object creation.
36+
* - The function assumes that each object has the keys specified in a provided mapping object.
37+
* - The function performs shallow copies of key values.
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 = rekey( x, mapping );
60+
* // returns [ { 'a': 1, 'b': 2 }, { 'a': 3, 'b': 4 } ]
61+
*/
62+
declare function rekey<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] }>;
63+
64+
65+
// EXPORTS //
66+
67+
export = rekey;

0 commit comments

Comments
 (0)