Package Exports
- deep-map-keys
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (deep-map-keys) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
deep-map-keys
Recurses through a JSON-like object and transforms its keys, returning a new object.
Install
Install via npm.
npm install --save deep-map-keys
Usage
Suppose we want to change the keys of an object from snake case to camel case. We can do something like this:
const deepMapKeys = require('deep-map-keys');
let comment = {
comment_id: 42,
user_id: 1024,
user_name: 'Mufasa',
content: 'You deliberately disobeyed me.',
viewed_by: [
{ user_id: 3820, user_name: 'Zazu' },
{ user_id: 8391, user_name: 'Rafiki' }
]
};
let result = deepMapKeys(comment, key => {
return key.replace(/_(\w)/g, (match, char) => char.toUpperCase());
});
console.log(result); /*
{
commentId: 42,
userId: 1024,
userName: 'Mufasa',
content: 'You deliberately disobeyed me.',
viewedBy: [
{ userId: 3820, userName: 'Rafiki' },
{ userId: 8391, userName: 'Zazu' }
]
};
*/
API
deepMapKeys(object, transformFn, [options])
Applies transformFn
to each key in an object. Keys are visited recursively,
so nested keys will be transformed. A new object is always returned; the
original object is unmodified.
object
object
The object whose keys are to be transformed. This object may be an Array
.
transformFn
function
The function to call for each key. The return value of the function determines the transformed value. The function is called with a single argument:
- key: The key being transformed.
options
object
(optional)
An options object. The following options are accepted:
- thisArg: Sets the value of
this
withintransformFn
.
Returns
object
Returns a new object.
License
Copyright © 2016 Akim McMath. Licensed under the MIT License.