Skip to content

Instantly share code, notes, and snippets.

@anurag-roy
Last active January 8, 2021 14:25
Show Gist options
  • Save anurag-roy/cb6b06f48b3c5d6649bffd63ae09ba6a to your computer and use it in GitHub Desktop.
Save anurag-roy/cb6b06f48b3c5d6649bffd63ae09ba6a to your computer and use it in GitHub Desktop.
Camel Case to Sentence Case Converter
const camelEdges: RegExp = /([A-Z](?=[A-Z][a-z])|[^A-Z](?=[A-Z])|[a-zA-Z](?=[^a-zA-Z]))/g;
// Input Array consisting of codes starting with "WS_Something_"
const inputArray: string[] = [
"WS_BLDNGMGMT_CertificationUpdateFailed",
"WS_USRMGMT_CompanyNotFound",
"WS_GEN_UnauthorizedOperation",
];
// Values of the resultObject will be the converted strings
let resultObject: {[key: string]: string} = {};
for (let code of inputArray) {
const lastIndex: number = code.lastIndexOf("_");
// Get the substring after the last underscore
const pascalValue: string = code.slice(lastIndex + 1);
const convertedValue: string = pascalValue.replace(camelEdges,'$1 ');
resultObject[code] = convertedValue;
}
console.log(resultObject);
/** Output
{
"WS_BLDNGMGMT_CertificationUpdateFailed": "Certification Update Failed",
"WS_USRMGMT_CompanyNotFound": "Company Not Found",
"WS_GEN_UnauthorizedOperation": "Unauthorized Operation"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment