Skip to content

Commit

Permalink
replace prefix number with underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
alehechka committed Jun 28, 2022
1 parent 503ecf5 commit d0187d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions jenshared/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jenshared

import (
"fmt"
"log"
"regexp"
"strings"
Expand All @@ -24,8 +25,14 @@ type TypeItem struct {

// Title converts the JSON name to TitleCase
func (t TypeItem) Title() string {
specialCharacters := regexp.MustCompile(`[^a-zA-Z0-9]`)
return strings.Title(specialCharacters.ReplaceAllString(t.Name, "_"))
str := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(t.Name, "_")

numbers := regexp.MustCompile(`\d`)
if len(str) > 0 && numbers.MatchString(str[0:1]) {
str = fmt.Sprintf("_%s", str[1:])
}

return strings.Title(str)
}

// TypeItems is an array of TypeItem objects
Expand Down
2 changes: 1 addition & 1 deletion testdata/object.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"deeplyNestedStringArray": [[["hello", "world"]]],
"timeString": "2006-01-02",
"uuidString": "5051ec14-ce89-4fcf-985e-99628a373497",
"%badName%": "this should break stuff"
"9%badName%": "this should break stuff"
}

0 comments on commit d0187d6

Please sign in to comment.