Skip to content

Commit f20ec72

Browse files
committed
refactor
removed for loops and replaced with regex expressions and created a common method countOcurrences which will be invoked by countObliqueIsTwo & countHypenIsTwo method
1 parent c9a2912 commit f20ec72

File tree

1 file changed

+18
-45
lines changed

1 file changed

+18
-45
lines changed

src/modules/tasks/providers/executor.ts

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,64 +126,37 @@ export class Executor implements OnModuleInit {
126126
}
127127

128128
checkEquality(lhsFinal: any, rhsFinal: any): boolean {
129-
if (lhsFinal == rhsFinal) {
130-
return true;
131-
}
132-
133-
return false;
129+
return (lhsFinal==rhsFinal)
134130
}
135131

136132

137133
countObliqueIsTwo(str: string): boolean {
138-
let oblique = '/';
139-
let count = 0;
140-
for (let i = 0; i < str.length; i++) {
141-
if (str[i] == oblique) {
142-
count++;
143-
}
144-
}
145-
if (count == 2) {
146-
return true;
147-
}
148-
149-
return false;
134+
const oblique = '/';
135+
const count = this.countOccurrences(str, oblique);
136+
return count === 2;
150137
}
151138

152139
countHyphenIsTwo(str: string): boolean {
153-
let hyphen = '-';
154-
let count = 0;
155-
for (let i = 0; i < str.length; i++) {
156-
if (str[i] == hyphen) {
157-
count++;
158-
}
159-
}
160-
if (count == 2) {
161-
return true;
162-
}
163-
164-
return false;
140+
const hyphen = '-';
141+
const count = this.countOccurrences(str, hyphen);
142+
return count === 2;
165143
}
166144

167-
convertDateObjectToString(dateObject: Date): string {
168-
let date: any = dateObject.getDate();
169-
let month: any = dateObject.getMonth() + 1;
170-
let year: any = dateObject.getFullYear();
171145

172-
date = date.toString();
173-
if (date.length == 1) {
174-
date = '0' + date;
175-
}
176-
month = month.toString();
177-
if (month.length == 1) {
178-
month = '0' + month;
179-
}
146+
countOccurrences(str: string, char: string): number {
147+
const regex = new RegExp(char, 'g');
148+
const count = (str.match(regex) || []).length;
149+
return count;
150+
}
180151

181-
year = year.toString();
152+
convertDateObjectToString(dateObject: Date): string {
153+
const date = String(dateObject.getDate()).padStart(2, '0');
154+
const month = String(dateObject.getMonth() + 1).padStart(2, '0');
155+
const year = String(dateObject.getFullYear());
182156

183-
let result = date + '/' + month + '/' + year;
157+
return `${date}/${month}/${year}`;
158+
}
184159

185-
return result;
186-
}
187160

188161

189162

0 commit comments

Comments
 (0)