to_camel_case_obj (to camel case obj) --> toCamelCaseObj

'to_camel_case_obj' ('to camel case obj') --> 'toCamelCaseObj'

by Yury

HTML

<p>
to_camel_case_obj (to camel case obj) --> toCamelCaseObj;
</p>

CSS

* {
  background: #1e1e1e;
  font-family: sans-serif;
  color: #c0c0c0;
  text-align: center;
}

JavaScript

// 'to_camel_case_obj' ('to camel case obj') --> 'toCamelCaseObj'
const toCamelCase = str => {
  return str.replace(/^([A-Z])|[\s-_](\w)/g, (match, p1, p2, offset) => {
    if (p2) return p2.toUpperCase();
    return p1.toLowerCase();
  });
};

const toCamelCaseObj = obj => {
  let newObj = {};
  for (const key in obj) {
    const camelKey = toCamelCase(key);
    newObj[camelKey] = obj[key];
  }
  return newObj;
};