Skip to content

Commit 4fc43a2

Browse files
committed
feat(client): add a helper function to get paper ID from paper title
1 parent facb339 commit 4fc43a2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package paperswithcode_go
33
import (
44
"github.com/codingpot/paperswithcode-go/internal/transport"
55
"net/http"
6+
"regexp"
7+
"strings"
68
"time"
79
)
810

911
const (
1012
BaseURL = "https://paperswithcode.com/api/v1"
1113
)
1214

15+
var whiteSpaceRegexp = regexp.MustCompile("\\s+")
16+
1317
// ClientOption can be used to swap the default http client or swap the API key
1418
type ClientOption func(*Client)
1519

@@ -42,3 +46,7 @@ type Client struct {
4246
HTTPClient *http.Client
4347
apiToken string
4448
}
49+
50+
func GetPaperIDFromPaperTitle(paperTitle string) string {
51+
return strings.ToLower(whiteSpaceRegexp.ReplaceAllString(paperTitle, "-"))
52+
}

client_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package paperswithcode_go
22

33
import (
4+
"fmt"
45
"github.com/codingpot/paperswithcode-go/internal/testutils"
56
"net/http"
67
"net/http/httptest"
@@ -36,3 +37,35 @@ func TestTransportIsNotProvidedWhenNoAPIIsProvided(t *testing.T) {
3637
c := NewClient()
3738
assert.Nil(t, c.HTTPClient.Transport)
3839
}
40+
41+
func ExampleGetPaperIDFromPaperTitle() {
42+
paperTitle := "Generative Adversarial Networks"
43+
fmt.Println(GetPaperIDFromPaperTitle(paperTitle))
44+
// Output: generative-adversarial-networks
45+
}
46+
47+
func TestGetPaperIDFromPaperTitle(t *testing.T) {
48+
tests := []struct {
49+
paperTitle string
50+
wantPaperID string
51+
}{
52+
{
53+
paperTitle: "This Is Paper",
54+
wantPaperID: "this-is-paper",
55+
},
56+
{
57+
paperTitle: "This Is Paper",
58+
wantPaperID: "this-is-paper",
59+
},
60+
{
61+
paperTitle: "This-Is-Paper",
62+
wantPaperID: "this-is-paper",
63+
},
64+
}
65+
for _, tt := range tests {
66+
t.Run(fmt.Sprintf("The ID of %s should be %s", tt.paperTitle, tt.wantPaperID),
67+
func(t *testing.T) {
68+
assert.Equal(t, tt.wantPaperID, GetPaperIDFromPaperTitle(tt.paperTitle))
69+
})
70+
}
71+
}

paper_result_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ func TestClient_PaperResultList(t *testing.T) {
4949
}
5050

5151
assert.Equal(t, expected, got)
52-
}
52+
}

0 commit comments

Comments
 (0)