Passed
Push — feature/update-managers-applca... ( b38314...b92f9a )
by Yonathan
04:52
created

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

Complexity

Total Complexity 49
Complexity/F 1.4

Size

Lines of Code 200
Function Count 35

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 49
eloc 145
mnd 14
bc 14
fnc 35
dl 0
loc 200
rs 8.48
bpm 0.4
cpm 1.4
noi 0
c 0
b 0
f 0

33 Functions

Rating   Name   Duplication   Size   Complexity  
A routes.ts ➔ removeBaseUrl 0 15 3
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 ➔ imageUrl 0 7 1
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 ➔ 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 ➔ managerFaq 0 7 2
A routes.ts ➔ managerJobIndex 0 3 1
A routes.ts ➔ applicantShow 0 8 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 ➔ jobShow 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
export function jobShow(locale: string, jobId: number): string {
59
  return `${baseUrl()}/${locale}/jobs/${jobId}`;
60
}
61
62
function applicationShow(
63
  locale: string,
64
  prefix: string,
65
  applicationId: number,
66
  jobId: number,
67
): string {
68
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applications/${applicationId}`;
69
}
70
71
function applicantShow(
72
  locale: string,
73
  prefix: string,
74
  applicantId: number,
75
  jobId: number,
76
): string {
77
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applicants/${applicantId}`;
78
}
79
80
export function managerApplicationShow(
81
  locale: string,
82
  applicationId: number,
83
  jobId: number,
84
): string {
85
  return applicationShow(locale, "manager", applicationId, jobId);
86
}
87
88
export function managerApplicantShow(
89
  locale: string,
90
  applicantId: number,
91
  jobId: number,
92
): string {
93
  return applicantShow(locale, "manager", applicantId, jobId);
94
}
95
96
export function managerEditProfile(locale: string): string {
97
  return `${baseUrl()}/${locale}/manager/profile`;
98
}
99
100
export function managerJobIndex(locale: string): string {
101
  return `${baseUrl()}/${locale}/manager/jobs`;
102
}
103
104
export function managerJobSummary(locale: string, jobId: number): string {
105
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}`;
106
}
107
108
export function managerJobPreview(locale: string, jobId: number): string {
109
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/preview`;
110
}
111
112
export function managerJobApplications(locale: string, jobId: number): string {
113
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/applications`;
114
}
115
116
export function managerScreeningPlan(locale: string, jobId: number): string {
117
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/assessment-plan`;
118
}
119
120
export function applicationReviewUpdate(
121
  locale: string,
122
  applicationId: number,
123
): string {
124
  return `${baseApiUrl()}/applications/${applicationId}/review`;
125
}
126
127
export function jobBuilderIntro(locale: string, jobId?: number): string {
128
  if (jobId) {
129
    return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/intro`;
130
  }
131
  return `${baseUrl()}/${locale}/manager/job-builder/intro`;
132
}
133
134
export function jobBuilderDetails(locale: string, jobId: number): string {
135
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/details`;
136
}
137
138
export function jobBuilderEnv(locale: string, jobId: number): string {
139
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/environment`;
140
}
141
142
export function jobBuilderImpact(locale: string, jobId: number): string {
143
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/impact`;
144
}
145
146
export function jobBuilderTasks(locale: string, jobId: number): string {
147
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/tasks`;
148
}
149
150
export function jobBuilderSkills(locale: string, jobId: number): string {
151
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/skills`;
152
}
153
154
export function jobBuilderReview(locale: string, jobId: number): string {
155
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/review`;
156
}
157
158
type FaqSection = "manager-who";
159
160
export function managerFaq(locale: string, faqSection?: FaqSection): string {
161
  const base = `${baseUrl()}/${locale}/manager/faq`;
162
  if (faqSection) {
163
    return `${base}#${faqSection}`;
164
  }
165
  return base;
166
}
167
168
export function hrJobIndex(locale: string): string {
169
  return `${baseUrl()}/${locale}/hr/jobs`;
170
}
171
export function hrJobSummary(locale: string, jobId: number): string {
172
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}`;
173
}
174
export function hrJobReview(locale: string, jobId: number): string {
175
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/review`;
176
}
177
export function hrJobPreview(locale: string, jobId: number): string {
178
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/preview`;
179
}
180
export function hrScreeningPlan(locale: string, jobId: number): string {
181
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/assessment-plan`;
182
}
183
export function hrJobApplications(locale: string, jobId: number): string {
184
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/applications`;
185
}
186
export const hrApplicationShow = (
187
  locale: string,
188
  applicationId: number,
189
  jobId: number,
190
): string => applicationShow(locale, "hr", applicationId, jobId);
191
export const hrApplicantShow = (
192
  locale: string,
193
  applicantId: number,
194
  jobId: number,
195
): string => applicantShow(locale, "hr", applicantId, jobId);
196
197
export function accountSettings(locale: string): string {
198
  return `${baseUrl()}/${locale}/settings`;
199
}
200