Skip to content

Commit e463221

Browse files
authored
feat(spaced): Avoid naming stutter (#12)
1 parent a4488b3 commit e463221

File tree

6 files changed

+95
-62
lines changed

6 files changed

+95
-62
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dist/
44
vendor/
55

66
.DS_Store
7+
debug.test
78
hyperspaced

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"github.com/72636c/hyperspaced"
2727
)
2828

29-
hyperspaced.Spaced("AESTHETIC")
29+
hyperspaced.One("AESTHETIC")
3030
// A E S T H E T I C
3131

32-
hyperspaced.SpacedN("AESTHETIC", 2)
32+
hyperspaced.N("AESTHETIC", 2)
3333
// A E S T H E T I C
3434
```
3535

hyperspaced.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ import (
66
"github.com/72636c/hyperspaced/internal/text/transform"
77
)
88

9-
// Spaced inserts a space between each character in a string.
10-
func Spaced(str string) string {
11-
return transform.Spaced(str)
12-
}
9+
var (
10+
// Spaced is an alias for One that is retained for backwards compatibility.
11+
Spaced = One
12+
13+
// SpacedN is an alias for N that is retained for backwards compatibility.
14+
SpacedN = N
15+
)
1316

14-
// SpacedN inserts n spaces between each character in a string.
15-
func SpacedN(str string, n int) string {
17+
// N inserts n spaces between each character in a string.
18+
func N(str string, n int) string {
1619
return transform.SpacedN(str, n)
1720
}
21+
22+
// One inserts a space between each character in a string.
23+
func One(str string) string {
24+
return transform.Spaced(str)
25+
}

hyperspaced_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
"github.com/72636c/hyperspaced"
77
)
88

9-
func ExampleSpaced() {
10-
output := hyperspaced.Spaced("AESTHETIC")
9+
func ExampleN() {
10+
output := hyperspaced.N("AESTHETIC", 2)
1111

1212
fmt.Println(output)
13-
// Output: A E S T H E T I C
13+
// Output: A E S T H E T I C
1414
}
1515

16-
func ExampleSpacedN() {
17-
output := hyperspaced.SpacedN("AESTHETIC", 2)
16+
func ExampleOne() {
17+
output := hyperspaced.One("AESTHETIC")
1818

1919
fmt.Println(output)
20-
// Output: A E S T H E T I C
20+
// Output: A E S T H E T I C
2121
}

internal/text/transform/spaced.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Spaced(str string) string {
1515
// SpacedN inserts n spaces between each character in a string.
1616
func SpacedN(str string, n int) string {
1717
if n < 0 {
18-
panic(errors.New("hyperspaced.SpacedN: really?"))
18+
panic(errors.New("hyperspaced/internal/text.SpacedN: really?"))
1919
}
2020

2121
chars := text.SplitString(str)

internal/text/transform/spaced_test.go

+71-47
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,7 @@ import (
66
"github.com/72636c/hyperspaced"
77
)
88

9-
func Test_Spaced(t *testing.T) {
10-
testCases := []struct {
11-
description string
12-
input string
13-
expected string
14-
}{
15-
{
16-
description: "Empty String",
17-
input: "",
18-
expected: "",
19-
},
20-
{
21-
description: "One Word",
22-
input: "AESTHETIC",
23-
expected: "A E S T H E T I C",
24-
},
25-
{
26-
description: "Two Words",
27-
input: "no u",
28-
expected: "n o u",
29-
},
30-
{
31-
description: "Emojified Buzzword",
32-
input: "\U0001f30f planet scale \u2696",
33-
expected: "🌏 p l a n e t s c a l e ⚖",
34-
},
35-
}
36-
37-
for _, testCase := range testCases {
38-
t.Run(testCase.description, func(t *testing.T) {
39-
actual := hyperspaced.Spaced(testCase.input)
40-
if actual != testCase.expected {
41-
t.Errorf(
42-
"expected %+[1]q (%[1]s), received %+[2]q (%[2]s)",
43-
testCase.expected,
44-
actual,
45-
)
46-
}
47-
})
48-
}
49-
}
50-
51-
func Test_SpacedN(t *testing.T) {
9+
func Suite_N(t *testing.T, f func(string, int) string) {
5210
testCases := []struct {
5311
description string
5412
inputString string
@@ -71,7 +29,7 @@ func Test_SpacedN(t *testing.T) {
7129

7230
for _, testCase := range testCases {
7331
t.Run(testCase.description, func(t *testing.T) {
74-
actual := hyperspaced.SpacedN(testCase.inputString, testCase.inputN)
32+
actual := f(testCase.inputString, testCase.inputN)
7533
if actual != testCase.expected {
7634
t.Errorf(
7735
"expected %+[1]q (%[1]s), received %+[2]q (%[2]s)",
@@ -83,8 +41,8 @@ func Test_SpacedN(t *testing.T) {
8341
}
8442
}
8543

86-
func Test_SpacedN_Panics_OnNegativeN(t *testing.T) {
87-
expectedMessage := "hyperspaced.SpacedN: really?"
44+
func Suite_N_Panics_OnNegativeN(t *testing.T, f func(string, int) string) {
45+
expectedMessage := "hyperspaced/internal/text.SpacedN: really?"
8846

8947
defer func() {
9048
r := recover()
@@ -105,7 +63,73 @@ func Test_SpacedN_Panics_OnNegativeN(t *testing.T) {
10563
}
10664
}()
10765

108-
hyperspaced.SpacedN("", -1)
66+
f("", -1)
10967

11068
t.Error("expected panic")
11169
}
70+
71+
func Suite_One(t *testing.T, f func(string) string) {
72+
testCases := []struct {
73+
description string
74+
input string
75+
expected string
76+
}{
77+
{
78+
description: "Empty String",
79+
input: "",
80+
expected: "",
81+
},
82+
{
83+
description: "One Word",
84+
input: "AESTHETIC",
85+
expected: "A E S T H E T I C",
86+
},
87+
{
88+
description: "Two Words",
89+
input: "no u",
90+
expected: "n o u",
91+
},
92+
{
93+
description: "Emojified Buzzword",
94+
input: "\U0001f30f planet scale \u2696",
95+
expected: "🌏 p l a n e t s c a l e ⚖",
96+
},
97+
}
98+
99+
for _, testCase := range testCases {
100+
t.Run(testCase.description, func(t *testing.T) {
101+
actual := f(testCase.input)
102+
if actual != testCase.expected {
103+
t.Errorf(
104+
"expected %+[1]q (%[1]s), received %+[2]q (%[2]s)",
105+
testCase.expected,
106+
actual,
107+
)
108+
}
109+
})
110+
}
111+
}
112+
113+
func Test_N(t *testing.T) {
114+
Suite_N(t, hyperspaced.N)
115+
}
116+
117+
func Test_N_Panics_OnNegativeN(t *testing.T) {
118+
Suite_N_Panics_OnNegativeN(t, hyperspaced.N)
119+
}
120+
121+
func Test_One(t *testing.T) {
122+
Suite_One(t, hyperspaced.One)
123+
}
124+
125+
func Test_Spaced(t *testing.T) {
126+
Suite_One(t, hyperspaced.Spaced)
127+
}
128+
129+
func Test_SpacedN(t *testing.T) {
130+
Suite_N(t, hyperspaced.SpacedN)
131+
}
132+
133+
func Test_SpacedN_Panics_OnNegativeN(t *testing.T) {
134+
Suite_N_Panics_OnNegativeN(t, hyperspaced.SpacedN)
135+
}

0 commit comments

Comments
 (0)