Passed
Push — master ( 74c350...233e8c )
by Kevin Van
03:58 queued 12s
created

helper.ts ➔ groupByMonth   A

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 21
rs 9.5
c 0
b 0
f 0
cc 2
1
export function mapMatchStatus(statusCode) {
2
  const statusCodes = new Map([
3
    [`PP`, `Uitgesteld`],
4
    [`ST`, `Stopgezet`],
5
    [`F1`, `Forfait`],
6
    [`FI`, `Forfait`],
7
    [`F2`, `Forfait`],
8
    [`FF`, `Forfait`],
9
    [`AMC`, `Algemeen forfait`],
10
  ])
11
12
  return statusCodes.get(statusCode) || null
13
}
14
15
/**
16
 * Strip first character if the division matches a youth division structure AND starts with a number.
17
 *
18
 * General form:
19
 *  - G9E
20
 *  - 2G10G
21
 *  - P7L
22
 *  - 2P12M
23
 * The function should return true if it matches the form starting with a "2".
24
 *
25
 * @param {string} division
26
 */
27
export function isYouthDivisionWithNumericFirst(division) {
28
  return /^(\d+)([a-zA-Z]+)(\d*)([a-zA-Z])/.test(division)
29
}
30
31
/**
32
 * Remove the first character of a division string if it's a number.
33
 *
34
 * Numbers are sometimes added for youth divisions to indicate a second period within a same season.
35
 * For example, G9K is the regional U9 division K before new year's day. From January 1st, the teams
36
 * can be re-arranged in a new (more balanced) division, which will be named something like 2G2K,
37
 * with the "2" in front indicating this difference.
38
 *
39
 * @param {string} division
40
 */
41
export function replaceFirstCharIfNumber(division) {
42
  if (isYouthDivisionWithNumericFirst(division)) {
43
    // Remove first character.
44
    division = division.substr(1)
45
  }
46
47
  return division
48
}
49
50
/**
51
 * Convert a region+division into an output label.
52
 *
53
 * @param {array} divisionArray
54
 * @param {string} level
55
 */
56
export function outputDivision(divisionArray, level = ``) {
57
  if (divisionArray[0] === `BCA`) {
58
    return `Beker van Brabant`
59
  } else if (divisionArray[0] === `ESCA`) {
60
    return `Beker voor B-ploegen`
61
  } else if (divisionArray[0] === `FR`) {
62
    return `Vriendschappelijk`
63
  } else if (divisionArray[0] === `BVZ`) {
64
    return `Beker van Zemst`
65
  } else if (divisionArray[2] <= 4) {
66
    return `${divisionArray[2]}e ${level !== `nat` ? `Prov.` : `Nationale`} ${divisionArray[3]}`
67
  } else {
68
    return `U${divisionArray[2]} / ${divisionArray[3]}${divisionArray[4] ? ` / ${divisionArray[4]}` : ``}`
69
  }
70
}
71
72
/**
73
 * Replace a divisionCode with its descriptive label.
74
 *
75
 * @param {string} division
76
 */
77
export function mapDivision(division) {
78
  return /^([A-Z]+)?(\d+)?([a-zA-Z]+)(\d*)$/.exec(replaceFirstCharIfNumber(division))
79
}
80
81
/**
82
 * Retrieve mapping and the formatted descriptive label of a division.
83
 *
84
 * @param {string} division
85
 * @param {string} region
86
 */
87
export function formatDivision(division, region) {
88
  const divisionArr = mapDivision(division)
89
  return outputDivision(divisionArr, region)
90
}
91
92
/**
93
 * Truncate to <n> letters and optionally stop at the last word instead of letter.
94
 *
95
 * @param {int} size
96
 * @param {boolean} useWordBoundary
97
 */
98
export function truncate(size, useWordBoundary = true) {
99
  if (this.length <= size) {
100
    return this
101
  }
102
  const subString = this.substr(0, size - 1)
103
  return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(` `)) : subString) + `…`
104
}
105
106
/**
107
 * Map a positionCode to a descriptive label.
108
 *
109
 * @param {string} positionCode
110
 */
111
export function mapPositionCode(positionCode) {
112
  return getPositions().get(positionCode) || null
113
}
114
115
/**
116
 * List of all positions, in order of position on the fields.
117
 *
118
 * @param {string} positionCode
119
 */
120
export function getPositions() {
121
  const positions = new Map([
122
    [`k`, `Doelman`],
123
    [`d`, `Verdediger`],
124
    [`m`, `Middenvelder`],
125
    [`a`, `Aanvaller`],
126
  ])
127
  return positions
128
}
129
130
export function mapPsdStatus(statusCode) {
131
  const statusCodes = new Map([
132
    [0, `Gepland`],
133
    [1, `Forfait`],
134
    [2, `Afgelast`],
135
    [3, `Onderbroken`],
136
  ])
137
138
  return statusCodes.get(statusCode) || null
139
}
140
141
export function mapPsdStatusShort(statusCode) {
142
  const statusCodes = new Map([
143
    [0, ``],
144
    [1, `FF`],
145
    [2, `AFG`],
146
    [3, `STOP`],
147
  ])
148
149
  return statusCodes.get(statusCode) || null
150
}
151
152
export function mapPsdStatusIcon(statusCode) {
153
  const statusCodes = new Map([
154
    [0, ``],
155
    [1, `fa-times`],
156
    [2, `fa-times`],
157
    [3, `fa-ban`],
158
  ])
159
160
  return statusCodes.get(statusCode) || null
161
}
162
163
export function translateGameResult(result) {
164
  const statusCodes = new Map([
165
    [`WON`, `Gewonnen`],
166
    [`EQUAL`, `Gelijkgespeeld`],
167
    [`LOST`, `Verloren`],
168
  ])
169
  return statusCodes.get(result) || null
170
}
171
172
export function capitalizeFirstLetter(string) {
173
  return string.charAt(0).toUpperCase() + string.slice(1)
174
}
175
176
export function groupByDate(data) {
177
  const groups = data.reduce((groups, object) => {
178
    const date = object.start.split(` `)[0]
179
    if (!groups[date]) {
180
      groups[date] = []
181
    }
182
    groups[date].push(object)
183
    return groups
184
  }, {})
185
186
  // Edit: to add it in the array format instead
187
  const groupArrays = Object.keys(groups).map((date) => {
188
    return {
189
      date,
190
      objects: groups[date],
191
    }
192
  })
193
194
  return groupArrays
195
}
196
197
export function groupByMonth(data) {
198
  const groups = data.reduce((groups, object) => {
199
    const date = new Date(object.timestamp * 1000)
200
    const month = date.toLocaleString(`nl-BE`, { month: `long` })
201
    console.log(date, month)
202
    if (!groups[month]) {
203
      groups[month] = []
204
    }
205
    groups[month].push(object)
206
    return groups
207
  }, {})
208
209
  const groupArrays = Object.keys(groups).map((date) => {
210
    return {
211
      date,
212
      objects: groups[date],
213
    }
214
  })
215
216
  return groupArrays
217
}
218
219
export function replaceAll(source: string, search: string, replacement: string) {
220
  return source.replace(new RegExp(search, `g`), replacement)
221
}
222
223
export default {
224
  mapMatchStatus,
225
  mapDivision,
226
  formatDivision,
227
  truncate,
228
  mapPositionCode,
229
  getPositions,
230
  mapPsdStatus,
231
  mapPsdStatusShort,
232
  mapPsdStatusIcon,
233
  translateGameResult,
234
  capitalizeFirstLetter,
235
  groupByDate,
236
  groupByMonth,
237
  replaceAll,
238
}
239