diff options
Diffstat (limited to 'media')
-rw-r--r-- | media/mediaType.go | 10 | ||||
-rw-r--r-- | media/mediaType_test.go | 11 |
2 files changed, 21 insertions, 0 deletions
diff --git a/media/mediaType.go b/media/mediaType.go index a6ba873eb..b56904cd9 100644 --- a/media/mediaType.go +++ b/media/mediaType.go @@ -15,10 +15,20 @@ package media import ( "fmt" + "strings" ) type Types []Type +func (t Types) GetByType(tp string) (Type, bool) { + for _, tt := range t { + if strings.EqualFold(tt.Type(), tp) { + return tt, true + } + } + return Type{}, false +} + // A media type (also known as MIME type and content type) is a two-part identifier for // file formats and format contents transmitted on the Internet. // For Hugo's use case, we use the top-level type name / subtype name + suffix. diff --git a/media/mediaType_test.go b/media/mediaType_test.go index e918b9393..c97ac782a 100644 --- a/media/mediaType_test.go +++ b/media/mediaType_test.go @@ -47,3 +47,14 @@ func TestDefaultTypes(t *testing.T) { } } + +func TestGetByType(t *testing.T) { + types := Types{HTMLType, RSSType} + + mt, found := types.GetByType("text/HTML") + require.True(t, found) + require.Equal(t, mt, HTMLType) + + _, found = types.GetByType("text/nono") + require.False(t, found) +} |