-3

How does one convert camelCase to MACRO_CASE in JavaScript?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Noel Schenk
  • 724
  • 1
  • 8
  • 19

1 Answers1

0

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);
Noel Schenk
  • 724
  • 1
  • 8
  • 19