Skip to content

Commit

Permalink
Merge branch 'release/v1.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed Nov 13, 2024
2 parents a0a85aa + 74fe596 commit 6324de2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A set of a ***high performance string tools*** that helps to build strings from
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/wissance/stringFormatter?style=plastic)
![GitHub issues](https://img.shields.io/github/issues/wissance/stringFormatter?style=plastic)
![GitHub Release Date](https://img.shields.io/github/release-date/wissance/stringFormatter)
![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.1/total?style=plastic)
![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.2/total?style=plastic)

![String Formatter: a convenient string formatting tool](/img/sf_cover.png)

Expand Down
22 changes: 16 additions & 6 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ func Format(template string, args ...any) string {
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
formattedStr.WriteString(strVal)
} else {
formattedStr.WriteByte('{')
formattedStr.WriteString(argNumberStr)
formattedStr.WriteByte('}')
if argNumberStr != "" {
formattedStr.WriteByte('{')
formattedStr.WriteString(argNumberStr)
formattedStr.WriteByte('}')
} else {
// complicated case when we have brackets in line and open line at the end
formattedStr.WriteByte('{')
}
}
i = j
}
Expand Down Expand Up @@ -210,9 +215,14 @@ func FormatComplex(template string, args map[string]any) string {
strVal := getItemAsStr(&arg, &argFormatOptions)
formattedStr.WriteString(strVal)
} else {
formattedStr.WriteByte('{')
formattedStr.WriteString(argNumberStr)
formattedStr.WriteByte('}')
if argNumberStr != "" {
formattedStr.WriteByte('{')
formattedStr.WriteString(argNumberStr)
formattedStr.WriteByte('}')
} else {
// complicated case when we have brackets in line and open line at the end
formattedStr.WriteByte('{')
}
}
i = j
}
Expand Down
24 changes: 23 additions & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,22 @@ func TestFormat(t *testing.T) {
},
expected: "Example is: {123 This is a test str, nothing more special -1.098743 main question error, is 42}",
},
"open bracket at the end of line of go file": {
"open bracket at the end of line of go line": {
template: "type serviceHealth struct {",
args: []any{},
expected: "type serviceHealth struct {",
},
"open bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
},

"close bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
args: []any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
Expand Down Expand Up @@ -202,6 +213,17 @@ func TestFormatComplex(t *testing.T) {
args: map[string]any{},
expected: " \"server\": {",
},
"open bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
args: map[string]any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
},

"close bracket at the end of line of go line with {} inside": {
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
args: map[string]any{},
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))
Expand Down

0 comments on commit 6324de2

Please sign in to comment.