Passed
Push — task/relative-resources ( f05a29...9df06a )
by Tristan
03:42
created

resources/assets/js/helpers/routes.ts   B

Complexity

Total Complexity 48
Complexity/F 1.41

Size

Lines of Code 196
Function Count 34

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 48
eloc 142
mnd 14
bc 14
fnc 34
dl 0
loc 196
rs 8.5599
bpm 0.4117
cpm 1.4117
noi 0
c 0
b 0
f 0

32 Functions

Rating   Name   Duplication   Size   Complexity  
A routes.ts ➔ removeBaseUrl 0 15 3
A routes.ts ➔ jobBuilderTasks 0 3 1
A routes.ts ➔ managerApplicationShow 0 7 1
A routes.ts ➔ hrScreeningPlan 0 2 1
A routes.ts ➔ jobBuilderIntro 0 6 2
A routes.ts ➔ managerScreeningPlan 0 3 1
A routes.ts ➔ managerJobPreview 0 3 1
A routes.ts ➔ managerApplicantShow 0 7 1
A routes.ts ➔ jobBuilderImpact 0 3 1
A routes.ts ➔ hrJobReview 0 2 1
A routes.ts ➔ basePathname 0 4 2
A routes.ts ➔ baseApiUrl 0 3 1
A routes.ts ➔ isValidUrl 0 11 3
A routes.ts ➔ baseUrl 0 7 4
A routes.ts ➔ jobBuilderSkills 0 3 1
A routes.ts ➔ hrJobApplications 0 2 1
A routes.ts ➔ accountSettings 0 3 1
A routes.ts ➔ applicationReviewUpdate 0 6 1
A routes.ts ➔ hrJobIndex 0 3 1
A routes.ts ➔ managerJobSummary 0 3 1
A routes.ts ➔ applicationShow 0 8 1
A routes.ts ➔ managerJobIndex 0 3 1
A routes.ts ➔ managerFaq 0 7 2
A routes.ts ➔ applicantShow 0 8 1
A routes.ts ➔ imageUrl 0 7 1
A routes.ts ➔ jobBuilderDetails 0 3 1
A routes.ts ➔ jobBuilderReview 0 3 1
A routes.ts ➔ managerJobApplications 0 3 1
A routes.ts ➔ hrJobSummary 0 2 1
A routes.ts ➔ hrJobPreview 0 2 1
A routes.ts ➔ managerEditProfile 0 3 1
A routes.ts ➔ jobBuilderEnv 0 3 1

How to fix   Complexity   

Complexity

Complex classes like resources/assets/js/helpers/routes.ts 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
function stripTrailingSlash(str: string): string {
2
  return str.endsWith("/") ? str.slice(0, -1) : str;
3
}
4
5
function isValidUrl(str: string): boolean {
6
  if (str.startsWith("http://") || str.startsWith("https://")) {
7
    try {
8
      const url = new URL(str);
9
    } catch (_) {
10
      return false;
11
    }
12
    return true;
13
  }
14
  return false;
15
}
16
17
export function baseUrl(): string {
18
  const base = document.querySelector("meta[name='base-url']");
19
  if (base !== null) {
20
    return stripTrailingSlash(base.getAttribute("content") ?? "");
21
  }
22
  return "";
23
}
24
25
export function basePathname(): string {
26
  const base = baseUrl();
27
  return isValidUrl(base) ? new URL(baseUrl()).pathname : "";
28
}
29
30
export function baseApiUrl(version = 1): string {
31
  return `${baseUrl()}/api/v${version}`;
32
}
33
34
/**
35
 *
36
 * @param imgFile The name of the img file, not inluding the /images/ path.
37
 */
38
export function imageUrl(imgFile: string): string {
39
  return `${baseUrl()}/images/${imgFile}`;
40
}
41
42
/**
43
 * Removes the base url or base pathname if the given url starts with them.
44
 * @param url
45
 */
46
export function removeBaseUrl(url: string): string {
47
  const base = baseUrl();
48
  if (url.startsWith(base)) {
49
    return url.substr(base.length);
50
  }
51
  const basePath = basePathname();
52
  if (url.startsWith(basePath)) {
53
    return url.substr(basePath.length);
54
  }
55
  return url;
56
}
57
58
function applicationShow(
59
  locale: string,
60
  prefix: string,
61
  applicationId: number,
62
  jobId: number,
63
): string {
64
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applications/${applicationId}`;
65
}
66
67
function applicantShow(
68
  locale: string,
69
  prefix: string,
70
  applicantId: number,
71
  jobId: number,
72
): string {
73
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applicants/${applicantId}`;
74
}
75
76
export function managerApplicationShow(
77
  locale: string,
78
  applicationId: number,
79
  jobId: number,
80
): string {
81
  return applicationShow(locale, "manager", applicationId, jobId);
82
}
83
84
export function managerApplicantShow(
85
  locale: string,
86
  applicantId: number,
87
  jobId: number,
88
): string {
89
  return applicantShow(locale, "manager", applicantId, jobId);
90
}
91
92
export function managerEditProfile(locale: string): string {
93
  return `${baseUrl()}/${locale}/manager/profile`;
94
}
95
96
export function managerJobIndex(locale: string): string {
97
  return `${baseUrl()}/${locale}/manager/jobs`;
98
}
99
100
export function managerJobSummary(locale: string, jobId: number): string {
101
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}`;
102
}
103
104
export function managerJobPreview(locale: string, jobId: number): string {
105
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/preview`;
106
}
107
108
export function managerJobApplications(locale: string, jobId: number): string {
109
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/applications`;
110
}
111
112
export function managerScreeningPlan(locale: string, jobId: number): string {
113
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/assessment-plan`;
114
}
115
116
export function applicationReviewUpdate(
117
  locale: string,
118
  applicationId: number,
119
): string {
120
  return `${baseApiUrl()}/applications/${applicationId}/review`;
121
}
122
123
export function jobBuilderIntro(locale: string, jobId?: number): string {
124
  if (jobId) {
125
    return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/intro`;
126
  }
127
  return `${baseUrl()}/${locale}/manager/job-builder/intro`;
128
}
129
130
export function jobBuilderDetails(locale: string, jobId: number): string {
131
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/details`;
132
}
133
134
export function jobBuilderEnv(locale: string, jobId: number): string {
135
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/environment`;
136
}
137
138
export function jobBuilderImpact(locale: string, jobId: number): string {
139
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/impact`;
140
}
141
142
export function jobBuilderTasks(locale: string, jobId: number): string {
143
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/tasks`;
144
}
145
146
export function jobBuilderSkills(locale: string, jobId: number): string {
147
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/skills`;
148
}
149
150
export function jobBuilderReview(locale: string, jobId: number): string {
151
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/review`;
152
}
153
154
type FaqSection = "manager-who";
155
156
export function managerFaq(locale: string, faqSection?: FaqSection): string {
157
  const base = `${baseUrl()}/${locale}/manager/faq`;
158
  if (faqSection) {
159
    return `${base}#${faqSection}`;
160
  }
161
  return base;
162
}
163
164
export function hrJobIndex(locale: string): string {
165
  return `${baseUrl()}/${locale}/hr/jobs`;
166
}
167
export function hrJobSummary(locale: string, jobId: number): string {
168
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}`;
169
}
170
export function hrJobReview(locale: string, jobId: number): string {
171
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/review`;
172
}
173
export function hrJobPreview(locale: string, jobId: number): string {
174
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/preview`;
175
}
176
export function hrScreeningPlan(locale: string, jobId: number): string {
177
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/assessment-plan`;
178
}
179
export function hrJobApplications(locale: string, jobId: number): string {
180
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/applications`;
181
}
182
export const hrApplicationShow = (
183
  locale: string,
184
  applicationId: number,
185
  jobId: number,
186
): string => applicationShow(locale, "hr", applicationId, jobId);
187
export const hrApplicantShow = (
188
  locale: string,
189
  applicantId: number,
190
  jobId: number,
191
): string => applicantShow(locale, "hr", applicantId, jobId);
192
193
export function accountSettings(locale: string): string {
194
  return `${baseUrl()}/${locale}/settings`;
195
}
196