Skip to content

Commit e552134

Browse files
committed
Better proper-casing
1 parent ef36260 commit e552134

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

json-to-go.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function jsonToGo(json, typename)
9999

100100
function format(str)
101101
{
102-
return toProperCase(str.replace(/\s/g, "_")).replace(/_/g, "");
102+
return toProperCase(str).replace(/\s|_/g, "");
103103
}
104104

105105
function goType(val)
@@ -144,13 +144,18 @@ function jsonToGo(json, typename)
144144
return "interface{}";
145145
}
146146

147-
// Thanks to http://stackoverflow.com/a/5574446/1048862
148147
function toProperCase(str)
149148
{
150-
return str.replace(/[^_]*/gi, function(txt)
149+
if (str.length == 0)
150+
return "";
151+
152+
str = str.charAt(0).toUpperCase() + str.substr(1);
153+
154+
return str.replace(/[\s_][a-z]+/g, function(txt)
151155
{
152-
return txt.charAt(0).toUpperCase()
153-
+ txt.substr(1).toLowerCase();
156+
return txt.charAt(0)
157+
+ txt.charAt(1).toUpperCase()
158+
+ txt.substr(2).toLowerCase();
154159
});
155160
}
156161
}

0 commit comments

Comments
 (0)