/
interval.go
143 lines (113 loc) · 2.91 KB
/
interval.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package interval
import (
"errors"
"regexp"
"strconv"
)
// Repetition model
type Repetition struct {
Repeats int
Distance int
Pace string
Recovery string
}
// TotalDistance returns the total distance for a repetition
func (repetition Repetition) TotalDistance() int {
return repetition.Repeats * repetition.Distance
}
// Set model
type Set struct {
Repeats int
Repetitions []Repetition
Recovery string
}
// TotalDistance returns the total distance for a set
func (set Set) TotalDistance() int {
var result int
for i := 0; i < len(set.Repetitions); i++ {
result += set.Repetitions[i].TotalDistance()
}
return set.Repeats * result
}
// Parse the notation of an interval
func Parse(notation string) (Set, error) {
var res Set
var err error
res, err = loadGroupedSets(notation)
if err != nil {
res, err = loadSet(notation)
}
if err != nil {
res, err = loadRepetitionSet(notation)
}
if err != nil {
return res, err
}
return res, nil
}
func loadGroupedSets(notation string) (Set, error) {
var result Set
groupedSetsRegex := regexp.MustCompile(`(\d+)\s*x\s*\{(.*?)\}\s*\[(.*?)\]`)
match := groupedSetsRegex.FindAllStringSubmatch(notation, -1)
if len(match) < 1 {
return result, errors.New("not a grouped set")
}
repeats, _ := strconv.Atoi(match[0][1])
repetitions := match[0][2]
recovery := match[0][3]
result, _ = loadRepetitionSet(repetitions)
result.Repeats = repeats
result.Recovery = recovery
return result, nil
}
func loadSet(notation string) (Set, error) {
var result Set
setsRegex := regexp.MustCompile(`(?:(\d+)\s*x\s*)?(\d+\s*x\s*.*)`)
match := setsRegex.FindAllStringSubmatch(notation, -1)
if len(match) < 1 {
return result, errors.New("not a set")
}
var repeats int
if len(match[0][1]) > 0 {
repeats, _ = strconv.Atoi(match[0][1])
} else {
repeats = 1
}
repetitions := match[0][0]
set, _ := loadRepetitionSet(repetitions)
set.Repeats = repeats
return set, nil
}
func loadRepetitionSet(notation string) (Set, error) {
var result Set
repetitionRegex := regexp.MustCompile(`(\d+)\s*x\s*(\d+)\s*\((.*?)\)\s*(?:\[(.*?)(?:\s*&\s*(.*?))?\]\s*)?(?:\[(.*?)\])?`)
match := repetitionRegex.FindAllStringSubmatch(notation, -1)
if len(match) < 1 {
return result, errors.New("no set")
}
var recoveryBetweenSets string
if len(match[0][6]) > 0 {
recoveryBetweenSets = match[0][6]
} else if len(match[0][5]) > 0 {
recoveryBetweenSets = match[0][5]
} else {
recoveryBetweenSets = ""
}
return Set{1, loadRepetitionsFromMatch(match), recoveryBetweenSets}, nil
}
func loadRepetitionsFromMatch(match [][]string) []Repetition {
var result []Repetition
for i := 0; i < len(match); i++ {
repeats, _ := strconv.Atoi(match[i][1])
distance, _ := strconv.Atoi(match[i][2])
pace := match[i][3]
recovery := match[i][4]
result = append(result, Repetition{
Repeats: repeats,
Distance: distance,
Pace: pace,
Recovery: recovery,
})
}
return result
}