2021-03-30 18:00:06 -04:00
|
|
|
// Helper functions
|
2021-03-30 16:22:35 -04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2021-03-30 18:00:06 -04:00
|
|
|
// Truncate a string to a length
|
2021-04-01 16:17:13 -04:00
|
|
|
func truncate(s string, length int) string {
|
2021-03-30 16:22:35 -04:00
|
|
|
if length < 1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:43:46 -04:00
|
|
|
if len(s) < length {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:22:35 -04:00
|
|
|
var buf strings.Builder
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
for _, char := range s {
|
|
|
|
if count == length {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteRune(char)
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|