-
Notifications
You must be signed in to change notification settings - Fork 1
/
part.go
205 lines (193 loc) · 5.86 KB
/
part.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package labo
import (
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Part is a snapshot of a Nintendo Labo part that is used in the construction of a Nintendo Labo kit.
//
// Parts are built from reading Nintendo Labo product descriptions and contain varying levels
// of detail and verbosity. A part, depending on the content read, may contain mostly
// default part amounts, colors, genders, shapes and sizes.
type Part struct {
Amount int `json:"amount"`
Color string `json:"color"`
Gender string `json:"gender"`
Href *Href `json:"href"`
Name string `json:"name"`
Shape string `json:"shape"`
Size string `json:"size"`
Spares bool `json:"spares"`
}
// getPartAmount searches the argument string for substrings that describe the quantity
// of a Nintendo Labo kit part.
//
// getPartAmount relies on the argument string containing some
// form of numeric pattern or numeric namespace that can be used
// to determine the value of the parts provided. When the argument string contains
// no numeric pattern, the function performs a lookup against the current known
// max number of Nintendo Labo parts per kit. Should a value exceed the known range,
// the default value of one is assigned.
func getPartAmount(s string) int {
var (
amount = defaultPartAmount
ok bool
substring string
)
substring = regexpMatchNumbers.FindString(s)
ok = (len(substring) > 0)
if ok {
amount, _ = strconv.Atoi(substring)
return amount
}
substring = regexpMatchAmount.FindString(s)
substring = strings.ToLower(substring)
ok = (len(substring) > 0)
if ok {
amount = partAmountMap[substring]
}
return amount
}
// getPartColor searches the argument string for substrings that describe the color
// of a Nintendo Labo kit part.
//
// getPartColor relies on the argument string containing some
// form of color namespace that can be used to determine the color of the part.
// When the argument string does not contain a known Nintendo Labo part color,
// the default part color is assigned.
func getPartColor(s string) string {
var (
color = defaultPartColor
ok bool
substring string
)
substring = regexpMatchColor.FindString(s)
substring = strings.ToLower(substring)
ok = (len(substring) > 0)
if ok {
color = partColorMap[substring]
}
return color
}
// getPartGender searches the argument string for substrings that describe the gender
// of a Nintendo Labo kit part.
//
// getPartGender relies on the argument string containing some
// form of gender namespace that can be used to determine the gender of the part.
// When the argument string does not contain a known Nintendo Labo part gender,
// the default part gender is assigned.
func getPartGender(s string) string {
var (
gender = defaultPartGender
ok bool
substring string
)
substring = regexpMatchGender.FindString(s)
substring = strings.ToLower(substring)
ok = (len(substring) > 0)
if ok {
gender = partGenderMap[substring]
}
return gender
}
// getPartName returns the Nintendo Labo part name.
//
// getPartName works by substituting all potential part properties from
// within the Nintendo Labo kit name.
func getPartName(s string) string {
for _, r := range partRegexps {
s = r.ReplaceAllString(s, stringEmpty)
}
s = regexpMatchMultipleSpaces.ReplaceAllString(s, stringWhitespace)
s = regexp.MustCompile(`(?i)(\sx\s$)`).ReplaceAllString(s, stringEmpty)
s = strings.TrimSpace(s)
return s
}
// getPartShape searches the argument string for substrings that describe the shape
// of a Nintendo Labo kit part.
//
// getPartShape relies on the argument string containing some
// form of shape namespace that can be used to determine the shape of the part.
// When the argument string does not contain a known Nintendo Labo part shape,
// the default part shape is assigned.
func getPartShape(s string) string {
var (
ok bool
shape = defaultPartShape
substring string
)
substring = regexpMatchShape.FindString(s)
substring = strings.ToLower(substring)
ok = (len(substring) > 0)
if ok {
shape = partShapeMap[substring]
}
return shape
}
// getPartSize searches the argument string for substrings that describe the size
// of a Nintendo Labo kit part.
//
// getPartSize relies on the argument string containing some
// form of size namespace that can be used to determine the size of the part.
// When the argument string does not contain a known Nintendo Labo part size,
// the default part size is assigned.
func getPartSize(s string) string {
var (
ok bool
size = defaultPartSize
substring string
)
substring = regexpMatchSize.FindString(s)
substring = strings.ToLower(substring)
ok = (len(substring) > 0)
if ok {
size = partShapeMap[substring]
}
return size
}
// getPartSpares searches the argument string for substrings that indicates
// whether the Nintendo Labo part kit has spares.
func getPartSpares(s string) bool {
var (
ok bool
substring string
)
substring = regexpMatchSpares.FindString(s)
ok = (len(substring) > 0)
return ok
}
// newPart is a constructor function that instantiates and returns a new Part struct pointer.
//
// newPart requires a valid goquery.Selection pointer to instantiate a new Part.
func newPart(s *goquery.Selection) *Part {
var (
substring = strings.TrimSpace(s.Text())
)
return &Part{
Amount: getPartAmount(substring),
Color: getPartColor(substring),
Gender: getPartGender(substring),
Href: newHref(s),
Name: getPartName(substring),
Shape: getPartShape(substring),
Size: getPartSize(substring),
Spares: getPartSpares(substring)}
}
// newParts is a consturctor function that instantiates and returns a new Part slice.
func newParts(s *goquery.Selection) []*Part {
var (
part *Part
parts []*Part
ok bool
)
s.Each(func(i int, s *goquery.Selection) {
part = newPart(s)
ok = (part != nil)
if !ok {
return
}
parts = append(parts, part)
})
return parts
}