Skip to content

Instantly share code, notes, and snippets.

@jtara1
Created September 9, 2019 22:03
Show Gist options
  • Save jtara1/34b81ff69948332cc5a441c4032d66d1 to your computer and use it in GitHub Desktop.
Save jtara1/34b81ff69948332cc5a441c4032d66d1 to your computer and use it in GitHub Desktop.
/**
* eg:
* { __createdAt: 'monday', order_id: 123, some_field_: 'abc' } =>
* { createdAt: 'monday', orderId: 123, someField: 'abc' }
*/
function convertPropertyNamesToCamelCase(object) {
const newObject = {};
_.mapKeys(object, (value, prop) => {
const newProp = prop
.replace(/^_*/, '')
.replace(/_*$/, '')
.replace(/(_[a-z])/g, (group0) => group0.toUpperCase().replace('_', ''));
newObject[newProp] = value;
});
return newObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment