Skip to content

Commit c1e0ed8

Browse files
committed
fix issues/5
1 parent 2231ef1 commit c1e0ed8

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

internal/shield/shield.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package shield
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
76
"net/url"
7+
"strings"
88
"time"
99

1010
"github.com/pkg/errors"
@@ -19,8 +19,8 @@ func Badge(query url.Values, left, right, color string) ([]byte, error) {
1919
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2020
defer cancel()
2121

22-
uri := fmt.Sprintf("https://img.shields.io/badge/%s-%s-%s", left, right, color)
23-
22+
param := encoding(left) + "-" + encoding(right) + "-" + color
23+
uri := "https://img.shields.io/badge/" + param
2424
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
2525
if err != nil {
2626
return nil, errors.Wrap(err, "http request")
@@ -34,3 +34,12 @@ func Badge(query url.Values, left, right, color string) ([]byte, error) {
3434

3535
return body, nil
3636
}
37+
38+
// Dashes -- → - Dash
39+
// Underscores __ → _ Underscore
40+
// _ or Space → Space
41+
func encoding(s string) string {
42+
s = strings.ReplaceAll(s, "-", "--")
43+
s = strings.ReplaceAll(s, "_", "__")
44+
return s
45+
}

internal/shield/shield_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package shield
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"testing"
7+
)
8+
9+
func TestBadge(t *testing.T) {
10+
names := []string{
11+
"mr-j001",
12+
"ac_oier",
13+
}
14+
15+
for _, v := range names {
16+
b, err := Badge(url.Values{}, "LeetCode CN", v, "green")
17+
if err != nil {
18+
t.Error(err)
19+
}
20+
fmt.Println(string(b))
21+
}
22+
23+
}

0 commit comments

Comments
 (0)