From d0187d6f97f54e8bf919d9064c6a375b46278aca Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Mon, 27 Jun 2022 21:33:41 -0500 Subject: [PATCH] replace prefix number with underscore --- jenshared/types.go | 11 +++++++++-- testdata/object.json | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/jenshared/types.go b/jenshared/types.go index 052857c..6f61a5d 100644 --- a/jenshared/types.go +++ b/jenshared/types.go @@ -1,6 +1,7 @@ package jenshared import ( + "fmt" "log" "regexp" "strings" @@ -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 diff --git a/testdata/object.json b/testdata/object.json index f4440c6..3d46b6e 100644 --- a/testdata/object.json +++ b/testdata/object.json @@ -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" }