Skip to content

Commit 64086a3

Browse files
Neerajpathak07kgryte
authored andcommitted
feat: add object/capitalize-keys
Ref: stdlib-js#8755
1 parent 3de4c3c commit 64086a3

File tree

10 files changed

+719
-0
lines changed

10 files changed

+719
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# capitalizeKeys
22+
23+
> Convert the first letter of each object key to uppercase.
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 capitalizeKeys = require( '@stdlib/object/capitalize-keys' );
41+
```
42+
43+
#### capitalizeKeys( obj )
44+
45+
Converts the first letter of each `object` key to uppercase, mapping the transformed keys to a new `object` having the same values.
46+
47+
```javascript
48+
var obj1 = {
49+
'beepBoop': 1,
50+
'fooBar': 2
51+
};
52+
53+
var obj2 = capitalizeKeys( obj1 );
54+
// returns { 'BeepBoop': 1, 'FooBar': 2 }
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
## Notes
66+
67+
- The function only transforms **own** properties. Hence, the function does **not** transform inherited properties.
68+
- The function **shallow** copies key values.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var capitalizeKeys = require( '@stdlib/object/capitalize-keys' );
84+
85+
var obj1 = {
86+
'aa': 'beep',
87+
'bb': 'boop',
88+
'cc': 'foo',
89+
'dd': 'bar'
90+
};
91+
92+
var obj2 = capitalizeKeys( obj1 );
93+
94+
console.dir( obj2 );
95+
// => { 'Aa': 'beep', 'Bb': 'boop', 'Cc': 'foo', 'Dd': 'bar' }
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- 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. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
* * *
115+
116+
## See Also
117+
118+
- <span class="package-name">[`@stdlib/utils/uncapitalize-keys`][@stdlib/utils/uncapitalize-keys]</span><span class="delimiter">: </span><span class="description">convert the first letter of each object key to lowercase.</span>
119+
- <span class="package-name">[`@stdlib/utils/uppercase-keys`][@stdlib/utils/uppercase-keys]</span><span class="delimiter">: </span><span class="description">convert each object key to uppercase.</span>
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
<!-- <related-links> -->
130+
131+
[@stdlib/utils/uncapitalize-keys]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/uncapitalize-keys
132+
133+
[@stdlib/utils/uppercase-keys]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/uppercase-keys
134+
135+
<!-- </related-links> -->
136+
137+
</section>
138+
139+
<!-- /.links -->
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) 2018 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 randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var capitalizeKeys = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var obj;
33+
var out;
34+
var i;
35+
36+
obj = {
37+
'aa': 'beep',
38+
'bb': 'boop',
39+
'cc': 'foo',
40+
'dd': 'bar',
41+
'ee': randu()
42+
};
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
obj.ee = randu();
46+
out = capitalizeKeys( obj );
47+
if ( typeof out !== 'object' ) {
48+
b.fail( 'should return an object' );
49+
}
50+
}
51+
b.toc();
52+
if ( typeof out !== 'object' ) {
53+
b.fail( 'should return an object' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( obj )
3+
Converts the first letter of each object key to uppercase.
4+
5+
The function only transforms own properties. Hence, the function does not
6+
transform inherited properties.
7+
8+
The function shallow copies key values.
9+
10+
Parameters
11+
----------
12+
obj: Object
13+
Source object.
14+
15+
Returns
16+
-------
17+
out: Object
18+
New object.
19+
20+
Examples
21+
--------
22+
> var obj = { 'aa': 1, 'bb': 2 };
23+
> var out = {{alias}}( obj )
24+
{ 'Aa': 1, 'Bb': 2 }
25+
26+
See Also
27+
--------
28+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
/**
22+
* Converts the first letter of each object key to uppercase.
23+
*
24+
* ## Notes
25+
*
26+
* - The function only transforms own properties. Hence, the function does not transform inherited properties.
27+
* - The function shallow copies key values.
28+
*
29+
* @param obj - source object
30+
* @returns new object
31+
*
32+
* @example
33+
* var obj1 = {
34+
* 'aa': 1,
35+
* 'bb': 2
36+
* };
37+
*
38+
* var obj2 = capitalizeKeys( obj1 );
39+
* // returns { 'Aa': 1, 'Bb': 2 }
40+
*/
41+
declare function capitalizeKeys( obj: Object ): Object;
42+
43+
44+
// EXPORTS //
45+
46+
export = capitalizeKeys;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 capitalizeKeys = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object...
25+
{
26+
capitalizeKeys( { 'a': 1, 'b': 2 } ); // $ExpectType Object
27+
}
28+
29+
// The compiler throws an error if the function is provided an incorrect number of arguments...
30+
{
31+
capitalizeKeys(); // $ExpectError
32+
capitalizeKeys( [], 2 ); // $ExpectError
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
var capitalizeKeys = require( './../lib' );
22+
23+
var obj1 = {
24+
'aa': 'beep',
25+
'bb': 'boop',
26+
'cc': 'foo',
27+
'dd': 'bar'
28+
};
29+
30+
var obj2 = capitalizeKeys( obj1 );
31+
32+
console.dir( obj2 );
33+
// => { 'Aa': 'beep', 'Bb': 'boop', 'Cc': 'foo', 'Dd': 'bar' }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
/**
22+
* Convert the first letter of each object key to uppercase.
23+
*
24+
* @module @stdlib/object/capitalize-keys
25+
*
26+
* @example
27+
* var capitalizeKeys = require( '@stdlib/object/capitalize-keys' );
28+
*
29+
* var obj1 = {
30+
* 'aa': 1,
31+
* 'bb': 2
32+
* };
33+
*
34+
* var obj2 = capitalizeKeys( obj1 );
35+
* // returns { 'Aa': 1, 'Bb': 2 }
36+
*/
37+
38+
// MODULES //
39+
40+
var main = require( './main.js' );
41+
42+
43+
// EXPORTS //
44+
45+
module.exports = main;

0 commit comments

Comments
 (0)