Passed
Push — feature/lighthouse ( 506e99...db2e00 )
by Kevin Van
11:37 queued 05:51
created

helper.js ➔ mapDivision   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
/**
2
 * Map a statusCode to a descriptive label.
3
 *
4
 * @param {string} statusCode
5
 */
6
export function mapMatchStatus(statusCode) {
7
  const statusCodes = new Map([
8
    ["PP", "Uitgesteld"],
9
    ["ST", "Stopgezet"],
10
    ["F1", "Forfait"],
11
    ["FI", "Forfait"],
12
    ["F2", "Forfait"],
13
    ["FF", "Forfait"],
14
    ["AMC", "Algemeen forfait"],
15
  ])
16
17
  return statusCodes.get(statusCode) || null
18
}
19
20
/**
21
 * Strip first character if the division matches a youth division structure AND starts with a number.
22
 *
23
 * General form:
24
 *  - G9E
25
 *  - 2G10G
26
 *  - P7L
27
 *  - 2P12M
28
 * The function should return true if it matches the form starting with a "2".
29
 *
30
 * @param {string} division
31
 */
32
export function isYouthDivisionWithNumericFirst(division) {
33
  return /^(\d+)([a-zA-Z]+)(\d*)([a-zA-Z])/.test(division)
34
}
35
36
/**
37
 * Remove the first character of a division string if it's a number.
38
 *
39
 * Numbers are sometimes added for youth divisions to indicate a second period within a same season.
40
 * For example, G9K is the regional U9 division K before new year's day. From January 1st, the teams
41
 * can be re-arranged in a new (more balanced) division, which will be named something like 2G2K,
42
 * with the "2" in front indicating this difference.
43
 *
44
 * @param {string} division
45
 */
46
export function replaceFirstCharIfNumber(division) {
47
  if (isYouthDivisionWithNumericFirst(division)) {
48
    // Remove first character.
49
    division = division.substr(1)
50
  }
51
52
  return division
53
}
54
55
/**
56
 * Convert a region+division into an output label.
57
 *
58
 * @param {array} divisionArray
59
 * @param {string} level
60
 */
61
export function outputDivision(divisionArray, level = "") {
62
  if (divisionArray[0] === "BCA") {
63
    return `Beker van Brabant`
64
  } else if (divisionArray[0] === "ESCA") {
65
    return `Beker voor B-ploegen`
66
  } else if (divisionArray[0] === "FR") {
67
    return `Vriendschappelijk`
68
  } else if (divisionArray[0] === "BVZ") {
69
    return `Beker van Zemst`
70
  } else if (divisionArray[2] <= 4) {
71
    return `${divisionArray[2]}e ${level !== "nat" ? "Prov." : "Nationale"} ${
72
      divisionArray[3]
73
    }`
74
  } else {
75
    return `U${divisionArray[2]} / ${divisionArray[3]}${
76
      divisionArray[4] ? ` / ${divisionArray[4]}` : ""
77
    }`
78
  }
79
}
80
81
/**
82
 * Replace a divisionCode with its descriptive label.
83
 *
84
 * @param {string} division
85
 */
86
export function mapDivision(division) {
87
  return /^([A-Z]+)?(\d+)?([a-zA-Z]+)(\d*)$/.exec(
88
    replaceFirstCharIfNumber(division)
89
  )
90
}
91
92
/**
93
 * Retrieve mapping and the formatted descriptive label of a division.
94
 *
95
 * @param {string} division
96
 * @param {string} region
97
 */
98
export function formatDivision(division, region) {
99
  const divisionArr = mapDivision(division)
100
  return outputDivision(divisionArr, region)
101
}
102
103
/**
104
 * Truncate to <n> letters and optionally stop at the last word instead of letter.
105
 *
106
 * @param {int} size
107
 * @param {boolean} useWordBoundary
108
 */
109
export function truncate(size, useWordBoundary = true) {
110
  if (this.length <= size) {
111
    return this
112
  }
113
  var subString = this.substr(0, size - 1)
114
  return (
115
    (useWordBoundary
116
      ? subString.substr(0, subString.lastIndexOf(" "))
117
      : subString) + "…"
118
  )
119
}
120
121
/**
122
 * Map a positionCode to a descriptive label.
123
 *
124
 * @param {string} positionCode
125
 */
126
export function mapPositionCode(positionCode) {
127
  return getPositions().get(positionCode) || null
128
}
129
130
/**
131
 * List of all positions, in order of position on the fields.
132
 *
133
 * @param {string} positionCode
134
 */
135
export function getPositions() {
136
  const positions = new Map([
137
    ["k", "Doelman"],
138
    ["d", "Verdediger"],
139
    ["m", "Middenvelder"],
140
    ["a", "Aanvaller"],
141
  ])
142
  return positions
143
}
144
145
export default {
146
  mapMatchStatus,
147
  mapDivision,
148
  formatDivision,
149
  truncate,
150
  mapPositionCode,
151
  getPositions,
152
}
153