-
Notifications
You must be signed in to change notification settings - Fork 1
/
humanize.go
33 lines (30 loc) · 979 Bytes
/
humanize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Package humanize provides methods for formatting and parsing values in human readable form.
package humanize
import (
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
"regexp"
)
// Humanizer is the main struct that provides the public methods.
type Humanizer struct {
provider languageProvider
printer *message.Printer
timeInputRe *regexp.Regexp
prefixInputRe *regexp.Regexp
allPrefixes []prefixDef // Helper slice of all prefixes.
}
// New creates a new humanizer for a given language.
func New(langName string) (*Humanizer, error) {
if provider, exists := languages[langName]; exists {
humanizer := &Humanizer{
provider: provider,
printer: message.NewPrinter(language.MustParse(langName)),
allPrefixes: make([]prefixDef, len(siPrefixes)+len(bitPrefixes)),
}
humanizer.buildTimeInputRe()
humanizer.preparePrefixes()
return humanizer, nil
}
return nil, fmt.Errorf("Language not supported: %s", langName)
}