Skip to content

Commit 2883dca

Browse files
committed
Merge pull request #5 from CAFxX/patch-3
Properly capitalize initialisms
2 parents 6b0191c + 5c196e2 commit 2883dca

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

json-to-go.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,21 @@ function jsonToGo(json, typename)
154154

155155
function toProperCase(str)
156156
{
157-
if (str.length == 0)
158-
return "";
159-
160-
str = str.charAt(0).toUpperCase() + str.substr(1);
161-
162-
return str.replace(/[\s_-][a-z]+/g, function(txt)
157+
// see github.com/golang/lint/lint.go
158+
var commonInitialisms = [
159+
"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP",
160+
"HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA",
161+
"SMTP", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "UID", "UUID", "URI",
162+
"URL", "UTF8", "VM", "XML", "XSRF", "XSS"
163+
];
164+
165+
return str.replace(/(^|[\s_-])([a-z]+)/g, function(unused, sep, frag)
163166
{
164-
return txt.charAt(0)
165-
+ txt.charAt(1).toUpperCase()
166-
+ txt.substr(2).toLowerCase();
167+
if (commonInitialisms.indexOf(frag.toUpperCase()) >= 0) {
168+
return sep + frag.toUpperCase();
169+
} else {
170+
return sep + frag[0].toUpperCase() + frag.substr(1).toLowerCase();
171+
}
167172
});
168173
}
169-
}
174+
}

0 commit comments

Comments
 (0)