revert changes, according to PR
This commit is contained in:
59
snippets/deepMapKeys.md
Normal file
59
snippets/deepMapKeys.md
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
### deepMapKeys
|
||||||
|
|
||||||
|
Deep maps an object keys.
|
||||||
|
|
||||||
|
Creates an object with the same values as the provided object and keys generated by running the provided function for each key.
|
||||||
|
|
||||||
|
Use Object.keys(obj) to iterate over the object's keys. Use Array.prototype.reduce() to create a new object with the same values and mapped keys using fn.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const deepMapKeys = (obj, f) => (
|
||||||
|
Array.isArray(obj)
|
||||||
|
? obj.map(val => deepMapKeys(val, f))
|
||||||
|
: (typeof obj === 'object')
|
||||||
|
? Object.keys(obj).reduce((acc, current) => {
|
||||||
|
const val = obj[current];
|
||||||
|
acc[f(current)] = (val !== null && typeof val === 'object')
|
||||||
|
? deepMapKeys(val, f)
|
||||||
|
: acc[f(current)] = val;
|
||||||
|
return acc;
|
||||||
|
}, {})
|
||||||
|
: obj
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
'use strict';
|
||||||
|
const obj = {
|
||||||
|
foo:'1',
|
||||||
|
isnull:null,
|
||||||
|
nested:{
|
||||||
|
bar:'1',
|
||||||
|
child:{
|
||||||
|
withArray:[
|
||||||
|
{
|
||||||
|
grandChild:[ 'hello' ]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const upperKeysObj = deepMapKeys(obj, (key) => key.toUpperCase());
|
||||||
|
/*
|
||||||
|
Formatted JSON Data
|
||||||
|
{
|
||||||
|
"FOO":"1",
|
||||||
|
"ISNULL":null,
|
||||||
|
"NESTED":{
|
||||||
|
"BAR":"1",
|
||||||
|
"CHILD":{
|
||||||
|
"WITHARRAY":[
|
||||||
|
{
|
||||||
|
"GRANDCHILD":[ 'hello' ]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
```
|
||||||
@ -50,6 +50,7 @@ decapitalize:string,array,intermediate
|
|||||||
deepClone:object,recursion,intermediate
|
deepClone:object,recursion,intermediate
|
||||||
deepFlatten:array,recursion,intermediate
|
deepFlatten:array,recursion,intermediate
|
||||||
deepFreeze:object,recursion,intermediate
|
deepFreeze:object,recursion,intermediate
|
||||||
|
deepMapKeys:object,recursion
|
||||||
defaults:object,intermediate
|
defaults:object,intermediate
|
||||||
defer:function,intermediate
|
defer:function,intermediate
|
||||||
degreesToRads:math,beginner
|
degreesToRads:math,beginner
|
||||||
|
|||||||
32
test/deepMapKeys.test.js
Normal file
32
test/deepMapKeys.test.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
const expect = require('expect');
|
||||||
|
const { deepMapKeys, toCamelCase } = require('./_30s.js');
|
||||||
|
|
||||||
|
test('deepMapKeys is a Function', () => {
|
||||||
|
expect(deepMapKeys).toBeInstanceOf(Function);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Deep maps object keys', () => {
|
||||||
|
const obj = {
|
||||||
|
'foo_bar': 'hello',
|
||||||
|
'deep_child': {
|
||||||
|
'child_array': ['hello'],
|
||||||
|
'child_object_array': [
|
||||||
|
{ 'hola_hello': 'Gamarjoba' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
fooBar: 'hello',
|
||||||
|
deepChild: {
|
||||||
|
childArray: ['hello'],
|
||||||
|
childObjectArray: [
|
||||||
|
{
|
||||||
|
holaHello: 'Gamarjoba'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const camleCaseKeys = k => toCamelCase(k);
|
||||||
|
expect(deepMapKeys(obj, camleCaseKeys)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user