forked from bcicen/go-units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.go
88 lines (75 loc) · 1.7 KB
/
units.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package units is a library for manipulating and converting between various units of measurement
package units
import (
"errors"
"sort"
"strings"
)
// Return all registered Units, sorted by name and quantity
func All() []Unit {
units := make(UnitList, 0, len(unitMap))
for _, u := range unitMap {
units = append(units, u)
}
sort.Sort(units)
return []Unit(units)
}
// MustConvertFloat converts a provided float from one Unit to another, panicking on error
func MustConvertFloat(x float64, from, to Unit) Value {
val, err := ConvertFloat(x, from, to)
if err != nil {
panic(err)
}
return val
}
// ConvertFloat converts a provided float from one Unit to another
func ConvertFloat(x float64, from, to Unit) (Value, error) {
path, err := ResolveConversion(from, to)
if err != nil {
return Value{}, err
}
for _, c := range path {
x = c.Fn(x)
}
return Value{x, to}, nil
}
// Find Unit matching name or symbol provided
func Find(s string) (Unit, error) {
allUnits := All()
// first try case-sensitive match
for _, u := range allUnits {
if matchUnit(s, u, true) {
return u, nil
}
}
// then case-insensitive
for _, u := range allUnits {
if matchUnit(s, u, false) {
return u, nil
}
}
// finally, try stripping plural suffix
if strings.HasSuffix(s, "s") || strings.HasSuffix(s, "S") {
s = s[:len(s)-1]
for _, u := range unitMap {
if matchUnit(s, u, false) {
return u, nil
}
}
}
return Unit{}, errors.New("unit \"" + s + "\" not found")
}
func matchUnit(s string, u Unit, matchCase bool) bool {
for _, name := range u.Names() {
if matchCase {
if name == s {
return true
}
} else {
if strings.EqualFold(s, name) {
return true
}
}
}
return false
}