Skip to content

Commit 74dc3c3

Browse files
committed
feat: add array/base/rekey
--- 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 25a75d7 commit 74dc3c3

File tree

10 files changed

+777
-0
lines changed

10 files changed

+777
-0
lines changed
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 -->
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( '@stdlib/array/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+
});
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+
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;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 rekey = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of objects...
25+
{
26+
const x = [
27+
{
28+
'x': 1,
29+
'y': 2
30+
}
31+
];
32+
const mapping = {
33+
'x': 'a',
34+
'y': 'b'
35+
};
36+
37+
rekey( x, mapping ); // $ExpectType { [x: string]: number; }[]
38+
}
39+
40+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
41+
{
42+
const mapping = {
43+
'x': 'a',
44+
'y': 'b'
45+
};
46+
47+
rekey( 1, mapping ); // $ExpectError
48+
rekey( true, mapping ); // $ExpectError
49+
rekey( false, mapping ); // $ExpectError
50+
rekey( null, mapping ); // $ExpectError
51+
rekey( void 0, mapping ); // $ExpectError
52+
rekey( {}, mapping ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a second argument which is not an array-like object...
56+
{
57+
rekey( [], 1 ); // $ExpectError
58+
rekey( [], true ); // $ExpectError
59+
rekey( [], false ); // $ExpectError
60+
rekey( [], null ); // $ExpectError
61+
rekey( [], void 0 ); // $ExpectError
62+
}
63+
64+
// The compiler throws an error if the function is provided an unsupported number of arguments...
65+
{
66+
rekey(); // $ExpectError
67+
rekey( [] ); // $ExpectError
68+
rekey( [], {}, [] ); // $ExpectError
69+
}

0 commit comments

Comments
 (0)