1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-03 13:27:50 +01:00
gojekyll/helpers/strings.go

34 lines
756 B
Go
Raw Normal View History

2017-06-17 01:17:22 +02:00
package helpers
import (
2017-06-15 16:17:21 +02:00
"regexp"
)
2017-06-15 16:17:21 +02:00
var nonAlphanumericSequenceMatcher = regexp.MustCompile(`[^[:alnum:]]+`)
2017-06-16 01:49:04 +02:00
// Slugify replaces each sequence of non-alphanumerics by a single hyphen
func Slugify(s string) string {
2017-06-15 15:07:06 +02:00
return nonAlphanumericSequenceMatcher.ReplaceAllString(s, "-")
}
2017-06-15 13:19:49 +02:00
// LeftPad left-pads s with spaces to n wide. It's an alternative to http://left-pad.io.
2017-06-13 23:19:05 +02:00
func LeftPad(s string, n int) string {
2017-06-11 22:00:03 +02:00
if n <= len(s) {
return s
}
ws := make([]byte, n-len(s))
for i := range ws {
ws[i] = ' '
}
return string(ws) + s
}
2017-06-17 01:17:22 +02:00
// 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
}