Passed
Push — master ( 7aa1e4...a37cc0 )
by Kevin Van
05:30
created

src/scripts/helper.ts   A

Complexity

Total Complexity 26
Complexity/F 3.71

Size

Lines of Code 142
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 100
mnd 19
bc 19
fnc 7
dl 0
loc 142
rs 10
bpm 2.7142
cpm 3.7142
noi 0
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A helper.ts ➔ translateGameResult 0 8 1
A helper.ts ➔ replaceAll 0 3 1
A helper.ts ➔ getPositions 0 14 1
A helper.ts ➔ mapPositionCode 0 8 1
A helper.ts ➔ mapPsdStatusShort 0 10 1
F helper.ts ➔ sortRankings 0 54 19
A helper.ts ➔ mapPsdStatus 0 10 1
1
import { RankingDataTeamObject } from "../Types/Ranking"
2
import Axios from "axios"
3
import { setupCache, setup } from "axios-cache-adapter"
4
import localforage from "localforage"
5
6
export function mapPsdStatus(statusCode: number): string | null {
7
  const statusCodes = new Map([
8
    [0, `Gepland`],
9
    [1, `Forfait`],
10
    [2, `Afgelast`],
11
    [3, `Onderbroken`],
12
  ])
13
14
  return statusCodes.get(statusCode) || null
15
}
16
17
export function mapPsdStatusShort(statusCode: number): string | null {
18
  const statusCodes = new Map([
19
    [0, ``],
20
    [1, `FF`],
21
    [2, `AFG`],
22
    [3, `STOP`],
23
  ])
24
25
  return statusCodes.get(statusCode) || null
26
}
27
28
export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) {
29
  // Rank lager: A stijgt in sortering.
30
  if (a.rank < b.rank) {
31
    return -1
32
  }
33
  if (a.rank > b.rank) {
34
    return 1
35
  }
36
  // Aantal overwinningen hoger: A stijgt in sortering.
37
  if (a.wins > b.wins) {
38
    return -1
39
  }
40
  if (a.wins < b.wins) {
41
    return 1
42
  }
43
  // Doelpuntensaldo beter: A stijgt in sortering.
44
  if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) {
45
    return -1
46
  }
47
  if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) {
48
    return 1
49
  }
50
  // Aantal gemaakte doelpunten hoger: A stijgt in sortering.
51
  if (a.goalsScored > b.goalsScored) {
52
    return -1
53
  }
54
  if (a.goalsScored < b.goalsScored) {
55
    return 1
56
  }
57
  // Aantal uitoverwinningen hoger: A stijgt in sortering.
58
  if (a.winsAway > b.winsAway) {
59
    return -1
60
  }
61
  if (a.winsAway < b.winsAway) {
62
    return 1
63
  }
64
  // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering.
65
  if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) {
66
    return -1
67
  }
68
  if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) {
69
    return 1
70
  }
71
  // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering.
72
  if (a.goalsScoredAway > b.goalsScoredAway) {
73
    return -1
74
  }
75
  if (a.goalsScoredAway < b.goalsScoredAway) {
76
    return 1
77
  }
78
79
  return a.team?.club?.localName.localeCompare(b.team?.club?.localName)
80
}
81
82
export function replaceAll(source: string, search: string, replacement: string) {
83
  return source.replace(new RegExp(search, `g`), replacement)
84
}
85
86
export function translateGameResult(result: string) {
87
  const statusCodes = new Map([
88
    [`WON`, `Gewonnen`],
89
    [`EQUAL`, `Gelijkgespeeld`],
90
    [`LOST`, `Verloren`],
91
  ])
92
  return statusCodes.get(result) || null
93
}
94
95
/**
96
 * Map a positionCode to a descriptive label.
97
 *
98
 * @param {string} positionCode
99
 */
100
export function mapPositionCode(positionCode: string) {
101
  return getPositions().get(positionCode) || null
102
}
103
104
/**
105
 * List of all positions, in order of position on the fields.
106
 *
107
 * @param {string} positionCode
108
 */
109
export function getPositions() {
110
  const positions = new Map([
111
    [`k`, `Doelman`],
112
    [`d`, `Verdediger`],
113
    [`m`, `Middenvelder`],
114
    [`a`, `Aanvaller`],
115
  ])
116
  return positions
117
}
118
119
export const forageStore = localforage.createInstance({
120
  driver: [localforage.INDEXEDDB, localforage.LOCALSTORAGE],
121
  name: `kcvv-cache`,
122
})
123
124
export const request = setup({
125
  cache: {
126
    maxAge: 15 * 60 * 1000,
127
    exclude: { query: false, filter: (config: { url: string }) => config.url.startsWith(`/profiles`) },
128
    store: forageStore,
129
    invalidate: async (config, request) => {
130
      if (request.clearCacheEntry) {
131
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
132
        // @ts-ignore
133
        await config.store.removeItem(config.uuid)
134
      }
135
    },
136
    readOnError: (error: { response: { status: number } }, request: any) => {
137
      return !error.response || (error.response.status >= 500 && error.response.status < 600)
138
    },
139
    clearOnStale: false,
140
  },
141
})
142