helper.ts ➔ sortRankings   F
last analyzed

Complexity

Conditions 19

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 54
rs 0.5999
c 0
b 0
f 0
cc 19

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like helper.ts ➔ sortRankings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { RankingDataTeamObject } from "../Types/Ranking"
2
import Axios from "axios"
3
import { setupCache } from "axios-cache-interceptor"
4
5
export function mapPsdStatus(statusCode: number): string | null {
6
  const statusCodes = new Map([
7
    [0, `Gepland`],
8
    [1, `Forfait`],
9
    [2, `Afgelast`],
10
    [3, `Onderbroken`],
11
  ])
12
13
  return statusCodes.get(statusCode) || null
14
}
15
16
export function mapPsdStatusShort(statusCode: number): string | null {
17
  const statusCodes = new Map([
18
    [0, ``],
19
    [1, `FF`],
20
    [2, `AFG`],
21
    [3, `STOP`],
22
  ])
23
24
  return statusCodes.get(statusCode) || null
25
}
26
27
export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) {
28
  // Rank lager: A stijgt in sortering.
29
  if (a.rank < b.rank) {
30
    return -1
31
  }
32
  if (a.rank > b.rank) {
33
    return 1
34
  }
35
  // Aantal overwinningen hoger: A stijgt in sortering.
36
  if (a.wins > b.wins) {
37
    return -1
38
  }
39
  if (a.wins < b.wins) {
40
    return 1
41
  }
42
  // Doelpuntensaldo beter: A stijgt in sortering.
43
  if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) {
44
    return -1
45
  }
46
  if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) {
47
    return 1
48
  }
49
  // Aantal gemaakte doelpunten hoger: A stijgt in sortering.
50
  if (a.goalsScored > b.goalsScored) {
51
    return -1
52
  }
53
  if (a.goalsScored < b.goalsScored) {
54
    return 1
55
  }
56
  // Aantal uitoverwinningen hoger: A stijgt in sortering.
57
  if (a.winsAway > b.winsAway) {
58
    return -1
59
  }
60
  if (a.winsAway < b.winsAway) {
61
    return 1
62
  }
63
  // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering.
64
  if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) {
65
    return -1
66
  }
67
  if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) {
68
    return 1
69
  }
70
  // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering.
71
  if (a.goalsScoredAway > b.goalsScoredAway) {
72
    return -1
73
  }
74
  if (a.goalsScoredAway < b.goalsScoredAway) {
75
    return 1
76
  }
77
78
  return a.team?.club?.localName.localeCompare(b.team?.club?.localName)
79
}
80
81
export function replaceAll(source: string, search: string, replacement: string) {
82
  return source.replace(new RegExp(search, `g`), replacement)
83
}
84
85
export function translateGameResult(result: string) {
86
  const statusCodes = new Map([
87
    [`WON`, `Gewonnen`],
88
    [`EQUAL`, `Gelijkgespeeld`],
89
    [`LOST`, `Verloren`],
90
  ])
91
  return statusCodes.get(result) || null
92
}
93
94
/**
95
 * Map a positionCode to a descriptive label.
96
 *
97
 * @param {string} positionCode
98
 */
99
export function mapPositionCode(positionCode: string) {
100
  return getPositions().get(positionCode) || null
101
}
102
103
/**
104
 * List of all positions, in order of position on the fields.
105
 *
106
 * @param {string} positionCode
107
 */
108
export function getPositions() {
109
  const positions = new Map([
110
    [`k`, `Doelman`],
111
    [`d`, `Verdediger`],
112
    [`m`, `Middenvelder`],
113
    [`a`, `Aanvaller`],
114
    [`j`, `Speler`],
115
  ])
116
  return positions
117
}
118
119
/**
120
 * Setup instance of axios with caching support.
121
 */
122
export const request = setupCache(Axios, { cacheTakeover: false })
123