Skip to content

Commit f4b7bf9

Browse files
committed
add property casing
1 parent 353d726 commit f4b7bf9

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

TypeScript.Interface.cst

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<%@ Import Namespace="System.Collections.Generic" %>
1111
<%@ Import Namespace="System.IO" %>
1212
<%@ Import Namespace="System.Linq" %>
13+
<%@ Import Namespace="System.Text" %>
1314
<%@ Import Namespace="Mono.Cecil" %>
1415

1516
<%@ Property Category="1.Type" Name="AssemblyFile"
@@ -22,7 +23,7 @@
2223
Description="The full type name to use." %>
2324

2425
<%@ Property Category="2.Interface" Name="ModuleName"
25-
Type="System.String" Default="" Optional="False"
26+
Type="System.String" Default="" Optional="True"
2627
Description="The TypeScript Module name."%>
2728
<%@ Property Category="2.Interface" Name="Extends"
2829
Type="System.String" Default="" Optional="True"
@@ -42,18 +43,26 @@
4243
<%@ Property Category="2.Interface" Name="UseOData"
4344
Type="System.Boolean" Default="False" Optional="False"
4445
Description="Use OData 4 types."%>
46+
<%@ Property Category="2.Interface" Name="PropertyNaming"
47+
Type="Naming" Default="PascalCase" Optional="False"
48+
Description="Controls property name syntax."%>
4549

4650
<% var scriptInterface = Generator.CreateInterface(AssemblyFile, ClassType, UseOData); %>
4751
<%= FileHeader ?? string.Empty %>
52+
53+
<% if (!string.IsNullOrEmpty(ModuleName)) { %>
4854
module <%= ModuleName %> {
4955
"use strict";
5056

57+
<% } %>
5158
export interface I<%= scriptInterface.Name %><% if (!string.IsNullOrEmpty(Extends)) { %> extends <%= Extends %><% } %> {
5259
<% foreach (var property in scriptInterface.Properties.Where(p => IsIgnored(p) == false)) { %>
53-
<%= property.Name %><%= OptionalFlag(property) %>: <%= property.Type %>;
60+
<%= PropertyName(property.Name) %><%= OptionalFlag(property) %>: <%= property.Type %>;
5461
<% } %>
5562
}
63+
<% if (!string.IsNullOrEmpty(ModuleName)) { %>
5664
}
65+
<% } %>
5766

5867
<script runat="template">
5968

@@ -64,6 +73,13 @@ public enum OptionalType
6473
None
6574
}
6675

76+
public enum Naming
77+
{
78+
PascalCase,
79+
CamalCase,
80+
LowerCaseUnderscore
81+
}
82+
6783
public string OptionalFlag(TypeScriptProperty property)
6884
{
6985
if (OptionalProperty == OptionalType.All)
@@ -89,4 +105,37 @@ public bool IsIgnored(string name)
89105

90106
return IgnoreProperties.Contains(name);
91107
}
108+
109+
public string PropertyName(string name)
110+
{
111+
if (string.IsNullOrEmpty(name))
112+
return string.Empty;
113+
114+
switch(PropertyNaming)
115+
{
116+
case Naming.CamalCase:
117+
return StringUtil.ToCamelCase(name);
118+
case Naming.LowerCaseUnderscore:
119+
return ToLowerUnderscoredWords(name);
120+
default:
121+
return StringUtil.ToPascalCase(name);
122+
}
123+
}
124+
125+
public string ToLowerUnderscoredWords(string value) {
126+
var builder = new StringBuilder(value.Length + 10);
127+
for (int index = 0; index < value.Length; index++) {
128+
char c = value[index];
129+
if (Char.IsUpper(c)) {
130+
if (index > 0 && value[index - 1] != '_')
131+
builder.Append('_');
132+
133+
builder.Append(Char.ToLower(c));
134+
} else {
135+
builder.Append(c);
136+
}
137+
}
138+
139+
return builder.ToString();
140+
}
92141
</script>

0 commit comments

Comments
 (0)