-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (23 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const _ = require('lodash');
// Using playful naming based on "Alice the Camel" song:
// "horse" represents the input object, and "Alice" is her name.
// Alice has no humps and she came to be transformed into a camel
module.exports = (horse, deep = false) => {
// Function to transform object keys using a provided function
const transformObjectKeys = (obj, transformFn) => _.mapKeys(obj, (value, key) => transformFn(key));
// Recursively applies transformation to object keys
const deeplyTransformObjectKeys = (obj, transformFn) => {
return _.mapValues(obj, value => {
return _.isPlainObject(value) ? deeplyTransformObjectKeys(value, transformFn) : value;
});
};
// Function to transform a key to camelCase
const transformKeyToCamelCase = key => _.camelCase(key);
// If deep transformation is needed, use the deeplyTransformObjectKeys function
if (deep) {
return deeplyTransformObjectKeys(horse, transformKeyToCamelCase);
} else {
// If not, just transform the first-level keys
return transformObjectKeys(horse, transformKeyToCamelCase);
}
};