@@ -63,7 +63,7 @@ func (c *Client) GetItem(imdbId string) (*Item, error) {
|
|||||||
Metascore: util.Atoi(b.Metascore, 0),
|
Metascore: util.Atoi(b.Metascore, 0),
|
||||||
RottenTomatoes: rt,
|
RottenTomatoes: rt,
|
||||||
ImdbRating: util.Atof64(b.ImdbRating, 0.0),
|
ImdbRating: util.Atof64(b.ImdbRating, 0.0),
|
||||||
ImdbVotes: util.Atoi(b.ImdbVotes, 0),
|
ImdbVotes: util.Atoi(util.StripNonNumeric(b.ImdbVotes), 0),
|
||||||
Language: b.Language,
|
Language: b.Language,
|
||||||
Country: b.Country,
|
Country: b.Country,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -4,17 +4,28 @@ import "regexp"
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
reStripNonAlphaNumeric *regexp.Regexp
|
reStripNonAlphaNumeric *regexp.Regexp
|
||||||
|
reStripNonNumeric *regexp.Regexp
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
reg1, err := regexp.Compile("[^a-zA-Z0-9]+")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
reStripNonAlphaNumeric = reg
|
reStripNonAlphaNumeric = reg1
|
||||||
|
|
||||||
|
reg2, err := regexp.Compile("[^0-9]+")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
reStripNonNumeric = reg2
|
||||||
}
|
}
|
||||||
|
|
||||||
func StripNonAlphaNumeric(value string) string {
|
func StripNonAlphaNumeric(value string) string {
|
||||||
// credits: https://golangcode.com/how-to-remove-all-non-alphanumerical-characters-from-a-string/
|
// credits: https://golangcode.com/how-to-remove-all-non-alphanumerical-characters-from-a-string/
|
||||||
return reStripNonAlphaNumeric.ReplaceAllString(value, "")
|
return reStripNonAlphaNumeric.ReplaceAllString(value, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StripNonNumeric(value string) string {
|
||||||
|
return reStripNonNumeric.ReplaceAllString(value, "")
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,3 +27,36 @@ func TestStripNonAlphaNumeric(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStripNonNumeric(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
value string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "remove non numeric",
|
||||||
|
args: args{
|
||||||
|
value: "10, 560",
|
||||||
|
},
|
||||||
|
want: "10560",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "remove nothing",
|
||||||
|
args: args{
|
||||||
|
value: "100",
|
||||||
|
},
|
||||||
|
want: "100",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := StripNonNumeric(tt.args.value); got != tt.want {
|
||||||
|
t.Errorf("StripNonNumeric() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user