1

So I have a list of variables in the file which I want to convert into all capitals separating underscores in javascript.

variable pattern is like this:

AwsKey

AwsSecret

CognitoUserPool

which I want to convert like below:

AWS_KEY

AWS_SECRET

COGNITO_USER_POOL

how do I write a function which does this in javascript? Any help would be much appreciated. Thank you.

Auto geek
  • 464
  • 1
  • 6
  • 21

1 Answers1

1

Edit: Sorry I forgotten to make them upper case

function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').replace(/([a-z])/, '$1).toUpperCase().slice(1); }

const camels = [
  'AwsKey',
  'AwsSecret',
  'CognitoUserPool',
];

function camelToCaps(str) { return str.replaceAll(/([A-Z])/g, '_$1').toUpperCase().slice(1); }

const caps = camels.map(camelToCaps);

console.log(caps);
Hakier
  • 505
  • 2
  • 13