Skip to content

Commit

Permalink
Add AllLicenses function
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Sep 28, 2024
1 parent a96c7f0 commit 1557ca7
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions spdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// See https://spdx.org/licenses.
package spdx

import "strings"
import (
"sort"
"strings"
)

// License is a SPDX license.
type License struct {
Expand All @@ -43,8 +46,25 @@ type License struct {
// LicenseForID returns the License for the ID.
//
// The input ID is case-insensitive, that is any casing of the ID will
// result in the correct license.
// result in the correct License.
func LicenseForID(id string) (License, bool) {
license, ok := lowercaseIDToLicense[strings.ToLower(id)]
return license, ok
}

// AllLicenses returns a slice of all Licenses.
//
// This slice will be sorted by License ID.
func AllLicenses() []License {
licenses := make([]License, 0, len(lowercaseIDToLicense))
for _, license := range lowercaseIDToLicense {
licenses = append(licenses, license)
}
sort.Slice(
licenses,
func(i int, j int) bool {
return licenses[i].ID < licenses[j].ID
},
)
return licenses
}

0 comments on commit 1557ca7

Please sign in to comment.