Skip to content

Commit

Permalink
Add an option to asUpperCamelCase to not turn uppercased acronyms int…
Browse files Browse the repository at this point in the history
…o lowercased (#726)
  • Loading branch information
vivien-apple authored Sep 15, 2022
1 parent 2132026 commit 9ffb3b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,28 @@ function asLowerCamelCase(label) {
return str.replace(/[^A-Za-z0-9_]/g, '');
}

function asUpperCamelCase(label) {
let str = string.toCamelCase(label, false);
return str.replace(/[^A-Za-z0-9_]/g, '');
function asUpperCamelCase(label, options) {
const preserveAcronyms = options && options.hash.preserveAcronyms;

let tokens = label.replace(/[+()&]/g, '').split(/ |_|-|\//);

let str = tokens
.map((token) => {
let isAcronym = token == token.toUpperCase();
if (!isAcronym) {
return token[0].toUpperCase() + token.substring(1);
}

if (preserveAcronyms) {
return token;
}

// if preserveAcronyms is false, then anything beyond the first letter becomes lower-case.
return token[0] + token.substring(1).toLowerCase();
})
.join('');

return str.replace(/[^A-Za-z0-9_ ]/g, '');
}

function chip_friendly_endpoint_type_name(options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ async function asObjectiveCClass(type, cluster, options) {
}

if (isStruct) {
return `MTR${appHelper.asUpperCamelCase(
cluster
)}Cluster${appHelper.asUpperCamelCase(type)}`;
return `MTR${appHelper.asUpperCamelCase(cluster, {
hash: { preserveAcronyms: true },
})}Cluster${appHelper.asUpperCamelCase(type)}`;
}

return 'NSNumber';
Expand Down Expand Up @@ -211,7 +211,9 @@ function commandHasRequiredField(command) {
* "Enum" bits on the enum names while we're here.
*/
function objCEnumName(clusterName, enumLabel) {
clusterName = appHelper.asUpperCamelCase(clusterName);
clusterName = appHelper.asUpperCamelCase(clusterName, {
hash: { preserveAcronyms: true },
});
enumLabel = appHelper.asUpperCamelCase(enumLabel);
// Some enum names have one or more copies of the cluster name at the
// beginning.
Expand Down

0 comments on commit 9ffb3b7

Please sign in to comment.