diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-12 18:06:35 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-01-12 18:06:35 +0100 |
commit | 46db900dab9c0e6fcd9d227f10a32fb24f5c8bd9 (patch) | |
tree | d3fd208f1fe36d791d92963ec653bc663012be5f /resource/resource.go | |
parent | 60c9f3b1c34b69771e25a66906f150f460d73223 (diff) |
resource: Implement Resources.ByPrefix
Fixes #4266
Diffstat (limited to 'resource/resource.go')
-rw-r--r-- | resource/resource.go | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/resource/resource.go b/resource/resource.go index 4a535889d..bb84aed79 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -72,22 +72,39 @@ func (r Resources) ByType(tp string) Resources { func (r Resources) GetByPrefix(prefix string) Resource { prefix = strings.ToLower(prefix) for _, resource := range r { - var name string - f, ok := resource.(source.File) - if ok { - name = f.BaseFileName() - } else { - _, name = filepath.Split(resource.RelPermalink()) - } - name = strings.ToLower(name) - - if strings.HasPrefix(name, prefix) { + if matchesPrefix(resource, prefix) { return resource } } return nil } +// ByPrefix gets all resources matching the given base filename prefix, e.g +// "logo" will match logo.png. +func (r Resources) ByPrefix(prefix string) Resources { + var matches Resources + prefix = strings.ToLower(prefix) + for _, resource := range r { + if matchesPrefix(resource, prefix) { + matches = append(matches, resource) + } + } + return matches +} + +func matchesPrefix(r Resource, prefix string) bool { + var name string + f, ok := r.(source.File) + if ok { + name = f.BaseFileName() + } else { + _, name = filepath.Split(r.RelPermalink()) + } + name = strings.ToLower(name) + + return strings.HasPrefix(name, prefix) +} + type Spec struct { *helpers.PathSpec mimeTypes media.Types |