How to convert camelCase to MACRO_CASE

100 Views Asked by At

How does one convert camelCase to MACRO_CASE in JavaScript?

1

There are 1 best solutions below

0
On

Converting an Object:

const test = {
  testId: '123',
  testString: 'whoa'
};

const testConverted = Object.assign({}, ...Object.keys(test).map((key, index) => ({[key.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase()]: Object.values(test)[index]})));

console.log(testConverted);

Converting a String:

const testString = 'convertThisToMacroCase';
const convertedString = testString.replace(/[A-Z]/g, letter => `_${letter}`).toUpperCase();
console.log(convertedString);