3 Commits

Author SHA1 Message Date
4ded03bd72 chore(release): 1.0.2 2020-12-14 14:55:43 +00:00
68a97519d5 Merge pull request #4 from jimeh/fix-bytes-method
fix(bytes): change Bytes function to accept string input but return a byte slice
2020-12-14 14:55:04 +00:00
5dbdbbf341 fix(bytes): change Bytes function to accept string input but return a byte slice
The old method signature was just nonsensical, as you would always be
providing indented values via a string literal. So it makes much more
sense to have all methods accept a string argument, and then return
different types.

This also allows use of a `Bytesf` method.

This is technically a breaking change, but I'm classifying it as a
bugfix cause the old method signature was basically useless.
2020-12-14 14:52:32 +00:00
4 changed files with 50 additions and 36 deletions

View File

@@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
### [1.0.2](https://github.com/jimeh/undent/compare/v1.0.1...v1.0.2) (2020-12-14)
### Bug Fixes
* **bytes:** change Bytes function to accept string input but return a byte slice ([5dbdbbf](https://github.com/jimeh/undent/commit/5dbdbbf3416b024aac8fca4e218802d6ad49ea74))
### [1.0.1](https://github.com/jimeh/undent/compare/v1.0.0...v1.0.1) (2020-12-07) ### [1.0.1](https://github.com/jimeh/undent/compare/v1.0.0...v1.0.1) (2020-12-07)

View File

@@ -9,36 +9,17 @@ import (
var matcher = regexp.MustCompile(`(?m)^([ \t]*)(?:\S)`) var matcher = regexp.MustCompile(`(?m)^([ \t]*)(?:\S)`)
// Bytes removes leading indentation/white-space from given byte slice. // Bytes removes leading indentation/white-space from given string and returns
func Bytes(b []byte) []byte { // it as a byte slice.
if len(b) > 0 && b[0] == '\n' { func Bytes(s string) []byte {
b = b[1:] return []byte(String(s))
} }
matches := matcher.FindAll(b, -1) // Bytesf removes leading indentation/white-space from given format string
if len(matches) == 0 { // before passing format and all additional arguments to fmt.Sprintf, returning
return b // the result as a byte slice.
} func Bytesf(format string, a ...interface{}) []byte {
return []byte(Stringf(format, a...))
index := 0
length := len(matches[0])
for i, s := range matches[1:] {
l := len(s)
if l < length {
index = i + 1
length = l
}
}
if length <= 1 {
return b
}
indent := matches[index][0 : length-1]
return regexp.MustCompile(
`(?m)^`+regexp.QuoteMeta(string(indent)),
).ReplaceAllLiteral(b, []byte{})
} }
// String removes leading indentation/white-space from given string. // String removes leading indentation/white-space from given string.

View File

@@ -7,11 +7,11 @@ import (
) )
func ExampleBytes() { func ExampleBytes() {
b := undent.Bytes([]byte(` b := undent.Bytes(`
{ {
"hello": "world" "hello": "world"
}`, }`,
)) )
fmt.Println(string(b)) fmt.Println(string(b))
// Output: // Output:
@@ -20,6 +20,20 @@ func ExampleBytes() {
// } // }
} }
func ExampleBytesf() {
s := undent.Bytesf(`
{
"hello": "%s"
}`,
"world",
)
fmt.Println(string(s))
// Output:
// {
// "hello": "world"
// }
}
func ExampleString() { func ExampleString() {
s := undent.String(` s := undent.String(`
{ {

View File

@@ -402,9 +402,21 @@ world
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
for _, tt := range stringTestCases { for _, tt := range stringTestCases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got := Bytes([]byte(tt.s)) got := Bytes(tt.s)
assert.Equal(t, []byte(tt.want), got) assert.IsType(t, []byte{}, got)
assert.Equal(t, tt.want, string(got))
})
}
}
func TestBytesf(t *testing.T) {
for _, tt := range stringfTestCases {
t.Run(tt.name, func(t *testing.T) {
got := Bytesf(tt.s, tt.a...)
assert.IsType(t, []byte{}, got)
assert.Equal(t, tt.want, string(got))
}) })
} }
} }
@@ -414,6 +426,7 @@ func TestString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got := String(tt.s) got := String(tt.s)
assert.IsType(t, "", got)
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
}) })
} }
@@ -424,6 +437,7 @@ func TestStringf(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got := Stringf(tt.s, tt.a...) got := Stringf(tt.s, tt.a...)
assert.IsType(t, "", got)
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
}) })
} }
@@ -432,10 +446,8 @@ func TestStringf(t *testing.T) {
func BenchmarkBytes(b *testing.B) { func BenchmarkBytes(b *testing.B) {
for _, tt := range stringTestCases { for _, tt := range stringTestCases {
b.Run(tt.name, func(b *testing.B) { b.Run(tt.name, func(b *testing.B) {
input := []byte(tt.s)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Bytes(input) Bytes(tt.s)
} }
}) })
} }