1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-04 06:07:55 +01:00
gojekyll/helpers/strings.go
2017-06-21 09:43:26 -04:00

34 lines
756 B
Go

package helpers
import (
"regexp"
)
var nonAlphanumericSequenceMatcher = regexp.MustCompile(`[^[:alnum:]]+`)
// Slugify replaces each sequence of non-alphanumerics by a single hyphen
func Slugify(s string) string {
return nonAlphanumericSequenceMatcher.ReplaceAllString(s, "-")
}
// LeftPad left-pads s with spaces to n wide. It's an alternative to http://left-pad.io.
func LeftPad(s string, n int) string {
if n <= len(s) {
return s
}
ws := make([]byte, n-len(s))
for i := range ws {
ws[i] = ' '
}
return string(ws) + s
}
// StringArrayToMap creates a map for use as a set.
func StringArrayToMap(strings []string) map[string]bool {
stringMap := map[string]bool{}
for _, s := range strings {
stringMap[s] = true
}
return stringMap
}