Skip to content

Commit ea2bd98

Browse files
committed
Automatically capitalize TS class names (Also remove spaces)
Fixes #8
1 parent fd3df31 commit ea2bd98

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

cmd/struct2ts/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func init() {
4444
KP.Flag("no-date", "Don't automatically handle time.Unix () <-> JS Date().").Short('D').BoolVar(&opts.NoDate)
4545
KP.Flag("no-helpers", "Don't output the helpers.").Short('H').BoolVar(&opts.NoHelpers)
4646
KP.Flag("no-default-values", "Don't assign default/zero values in the ctor.").Short('N').BoolVar(&opts.NoAssignDefaults)
47+
KP.Flag("no-capitalize", "Don't capitalize TS class names.").Short('C').BoolVar(&opts.NoCapitalize)
4748
KP.Flag("interface", "Only generate an interface (disables all the other options).").Short('i').BoolVar(&opts.InterfaceOnly)
4849

4950
KP.Flag("src-only", "Only output the Go code (helpful if you want to edit it yourself).").Short('s').BoolVar(&srcOnly)
@@ -206,13 +207,14 @@ func runStruct2TS(w io.Writer) error {
206207
InterfaceOnly: {{ .opts.InterfaceOnly }},
207208
208209
NoConstructor: {{ .opts.NoConstructor }},
210+
NoCapitalize: {{ .opts.NoCapitalize }},
209211
MarkOptional: {{ .opts.MarkOptional }},
210212
NoToObject: {{ .opts.NoToObject }},
211213
NoExports: {{ .opts.NoExports }},
212214
NoHelpers: {{ .opts.NoHelpers }},
213215
NoDate: {{ .opts.NoDate }},
214216
215-
ES6: {{ .opts.ES6 }},
217+
ES6: {{ .opts.ES6 }},
216218
})
217219
218220
{{ range $_, $t := .types }}

s2ts.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"reflect"
1111
"strings"
12+
"unicode"
1213
)
1314

1415
type Options struct {
@@ -18,6 +19,7 @@ type Options struct {
1819
InterfaceOnly bool
1920

2021
MarkOptional bool
22+
NoCapitalize bool
2123
NoConstructor bool
2224
NoToObject bool
2325
NoExports bool
@@ -78,6 +80,9 @@ func (s *StructToTS) addType(t reflect.Type, name, prefix string) (out *Struct)
7880

7981
if name == "" {
8082
name = t.Name()
83+
if !s.opts.NoCapitalize {
84+
name = capitalize(name)
85+
}
8186
}
8287

8388
out = &Struct{
@@ -247,3 +252,20 @@ func stripType(t reflect.Type) string {
247252
}
248253
return n
249254
}
255+
256+
func capitalize(s string) string {
257+
var last rune
258+
return strings.Map(func(r rune) rune {
259+
l := last
260+
last = r
261+
if unicode.IsSpace(r) {
262+
return 0
263+
}
264+
265+
if !unicode.IsLetter(l) && unicode.IsLetter(r) {
266+
r = unicode.ToUpper(r)
267+
}
268+
269+
return r
270+
}, s)
271+
}

0 commit comments

Comments
 (0)