Passed
Push — feature/application-skill-info... ( 2a7f8b...1281e5 )
by Chris
06:47
created

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

Complexity

Total Complexity 49
Complexity/F 1.4

Size

Lines of Code 221
Function Count 35

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

33 Functions

Rating   Name   Duplication   Size   Complexity  
A routes.ts ➔ removeBaseUrl 0 15 3
A routes.ts ➔ slugify 0 23 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 ➔ 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
/* eslint-disable no-useless-escape */
2
function stripTrailingSlash(str: string): string {
3
  return str.endsWith("/") ? str.slice(0, -1) : str;
4
}
5
6
function isValidUrl(str: string): boolean {
7
  if (str.startsWith("http://") || str.startsWith("https://")) {
8
    try {
9
      const url = new URL(str);
10
    } catch (_) {
11
      return false;
12
    }
13
    return true;
14
  }
15
  return false;
16
}
17
18
export function baseUrl(): string {
19
  const base = document.querySelector("meta[name='base-url']");
20
  if (base !== null) {
21
    return stripTrailingSlash(base.getAttribute("content") ?? "");
22
  }
23
  return "";
24
}
25
26
export function basePathname(): string {
27
  const base = baseUrl();
28
  return isValidUrl(base) ? new URL(baseUrl()).pathname : "";
29
}
30
31
export function baseApiUrl(version = 1): string {
32
  return `${baseUrl()}/api/v${version}`;
33
}
34
35
/**
36
 *
37
 * @param imgFile The name of the img file, not inluding the /images/ path.
38
 */
39
export function imageUrl(imgFile: string): string {
40
  return `${baseUrl()}/images/${imgFile}`;
41
}
42
43
/**
44
 * Removes the base url or base pathname if the given url starts with them.
45
 * @param url
46
 */
47
export function removeBaseUrl(url: string): string {
48
  const base = baseUrl();
49
  if (url.startsWith(base)) {
50
    return url.substr(base.length);
51
  }
52
  const basePath = basePathname();
53
  if (url.startsWith(basePath)) {
54
    return url.substr(basePath.length);
55
  }
56
  return url;
57
}
58
59
function applicationShow(
60
  locale: string,
61
  prefix: string,
62
  applicationId: number,
63
  jobId: number,
64
): string {
65
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applications/${applicationId}`;
66
}
67
68
function applicantShow(
69
  locale: string,
70
  prefix: string,
71
  applicantId: number,
72
  jobId: number,
73
): string {
74
  return `${baseUrl()}/${locale}/${prefix}/jobs/${jobId}/applicants/${applicantId}`;
75
}
76
77
export function managerApplicationShow(
78
  locale: string,
79
  applicationId: number,
80
  jobId: number,
81
): string {
82
  return applicationShow(locale, "manager", applicationId, jobId);
83
}
84
85
export function managerApplicantShow(
86
  locale: string,
87
  applicantId: number,
88
  jobId: number,
89
): string {
90
  return applicantShow(locale, "manager", applicantId, jobId);
91
}
92
93
export function managerEditProfile(locale: string): string {
94
  return `${baseUrl()}/${locale}/manager/profile`;
95
}
96
97
export function managerJobIndex(locale: string): string {
98
  return `${baseUrl()}/${locale}/manager/jobs`;
99
}
100
101
export function managerJobSummary(locale: string, jobId: number): string {
102
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}`;
103
}
104
105
export function managerJobPreview(locale: string, jobId: number): string {
106
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/preview`;
107
}
108
109
export function managerJobApplications(locale: string, jobId: number): string {
110
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/applications`;
111
}
112
113
export function managerScreeningPlan(locale: string, jobId: number): string {
114
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/assessment-plan`;
115
}
116
117
export function applicationReviewUpdate(
118
  locale: string,
119
  applicationId: number,
120
): string {
121
  return `${baseApiUrl()}/applications/${applicationId}/review`;
122
}
123
124
export function jobBuilderIntro(locale: string, jobId?: number): string {
125
  if (jobId) {
126
    return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/intro`;
127
  }
128
  return `${baseUrl()}/${locale}/manager/job-builder/intro`;
129
}
130
131
export function jobBuilderDetails(locale: string, jobId: number): string {
132
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/details`;
133
}
134
135
export function jobBuilderEnv(locale: string, jobId: number): string {
136
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/environment`;
137
}
138
139
export function jobBuilderImpact(locale: string, jobId: number): string {
140
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/impact`;
141
}
142
143
export function jobBuilderTasks(locale: string, jobId: number): string {
144
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/tasks`;
145
}
146
147
export function jobBuilderSkills(locale: string, jobId: number): string {
148
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/skills`;
149
}
150
151
export function jobBuilderReview(locale: string, jobId: number): string {
152
  return `${baseUrl()}/${locale}/manager/jobs/${jobId}/builder/review`;
153
}
154
155
type FaqSection = "manager-who";
156
157
export function managerFaq(locale: string, faqSection?: FaqSection): string {
158
  const base = `${baseUrl()}/${locale}/manager/faq`;
159
  if (faqSection) {
160
    return `${base}#${faqSection}`;
161
  }
162
  return base;
163
}
164
165
export function hrJobIndex(locale: string): string {
166
  return `${baseUrl()}/${locale}/hr/jobs`;
167
}
168
export function hrJobSummary(locale: string, jobId: number): string {
169
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}`;
170
}
171
export function hrJobReview(locale: string, jobId: number): string {
172
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/review`;
173
}
174
export function hrJobPreview(locale: string, jobId: number): string {
175
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/preview`;
176
}
177
export function hrScreeningPlan(locale: string, jobId: number): string {
178
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/assessment-plan`;
179
}
180
export function hrJobApplications(locale: string, jobId: number): string {
181
  return `${baseUrl()}/${locale}/hr/jobs/${jobId}/applications`;
182
}
183
export const hrApplicationShow = (
184
  locale: string,
185
  applicationId: number,
186
  jobId: number,
187
): string => applicationShow(locale, "hr", applicationId, jobId);
188
export const hrApplicantShow = (
189
  locale: string,
190
  applicantId: number,
191
  jobId: number,
192
): string => applicantShow(locale, "hr", applicantId, jobId);
193
194
export function accountSettings(locale: string): string {
195
  return `${baseUrl()}/${locale}/settings`;
196
}
197
198
/**
199
 * Converts a string to a url safe equivalent.
200
 * @param string Any input text.
201
 * @returns url safe string.
202
 */
203
export function slugify(string: string): string {
204
  const a =
205
    "àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;";
206
  const b =
207
    "aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------";
208
  const p = new RegExp(a.split("").join("|"), "g");
209
210
  return string
211
    .toString()
212
    .toLowerCase()
213
    .replace(/\s+/g, "-") // Replace spaces with -
214
    .replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
215
    .replace(/&/g, "-and-") // Replace & with 'and'
216
    .replace(/[^\w\-]+/g, "") // Remove all non-word characters
217
    .replace(/\-\-+/g, "-") // Replace multiple - with single -
218
    .replace(/^-+/, "") // Trim - from start of text
219
    .replace(/-+$/, ""); // Trim - from end of text
220
}
221