Skip to content

Commit fd105b3

Browse files
committed
fix: added catch excemption handlers for utils
1 parent beac036 commit fd105b3

File tree

8 files changed

+74
-44
lines changed

8 files changed

+74
-44
lines changed

src/__tests__/shuffleArray.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ describe("shuffleArray", () => {
77

88
expect(shuffledArray).toHaveLength(array.length);
99
expect(shuffledArray).toEqual(expect.arrayContaining(array));
10-
expect(array).toEqual(expect.arrayContaining(shuffledArray));
10+
expect(array).toEqual(expect.arrayContaining(shuffledArray!));
1111

1212
const isOrderDifferent = array.some(
13-
(value, index) => value !== shuffledArray[index]
13+
(value, index) => value !== shuffledArray?.[index]
1414
);
1515
expect(isOrderDifferent).toBe(true);
1616
});

src/camelToSentenceCase.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const camelToSentenceCase = (str: string): string => {
2-
const temp = str.replace(/([A-Z])(?=[A-Z][a-z])|([a-z])(?=[A-Z])/g, "$& ");
3-
return temp.charAt(0).toUpperCase() + temp.slice(1);
1+
export const camelToSentenceCase = (str: string): string | undefined => {
2+
try {
3+
const temp = str.replace(/([A-Z])(?=[A-Z][a-z])|([a-z])(?=[A-Z])/g, "$& ");
4+
return temp.charAt(0).toUpperCase() + temp.slice(1);
5+
} catch (error) {
6+
console.error("Error converting camel case to sentence case", error);
7+
}
48
};

src/coercedGet.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ export const coercedGet = <T>(
33
path: string,
44
defaultValue?: any
55
): any => {
6-
const travel = (regexp: RegExp) =>
7-
String.prototype.split
8-
.call(path, regexp)
9-
.filter(Boolean)
10-
.reduce((res, key) => {
11-
return res !== null && res !== undefined && typeof res === "object"
12-
? (res as Record<string, any>)[key]
13-
: res;
14-
}, obj);
6+
try {
7+
const travel = (regexp: RegExp) =>
8+
String.prototype.split
9+
.call(path, regexp)
10+
.filter(Boolean)
11+
.reduce((res, key) => {
12+
return res !== null && res !== undefined && typeof res === "object"
13+
? (res as Record<string, any>)[key]
14+
: res;
15+
}, obj);
1516

16-
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
17-
return result === undefined || result === obj ? defaultValue : result;
17+
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
18+
return result === undefined || result === obj ? defaultValue : result;
19+
} catch (ex) {
20+
console.error("Error coercedGet", ex);
21+
}
1822
};

src/generateSlug.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
export const generateSlug = (str: string): string => {
2-
return str
3-
.toLowerCase()
4-
.replace(/[^a-z0-9]+/g, "-")
5-
.replace(/(^-|-$)/g, "");
1+
export const generateSlug = (str: string): string | undefined => {
2+
try {
3+
return str
4+
.toLowerCase()
5+
.replace(/[^a-z0-9]+/g, "-")
6+
.replace(/(^-|-$)/g, "");
7+
} catch (error) {
8+
console.error("Error generating slug", error);
9+
}
610
};

src/isValidEmail.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const isValidEmail = (email: string): boolean => {
2-
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
3-
return emailRegex.test(email);
1+
export const isValidEmail = (email: string): boolean | undefined => {
2+
try {
3+
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
4+
return emailRegex.test(email);
5+
} catch (error) {
6+
console.error("Error isValidEmail", error);
7+
}
48
};

src/shuffleArray.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
export const shuffleArray = <T>(array: T[]): T[] => {
2-
for (let i = array.length - 1; i > 0; i--) {
3-
const j = Math.floor(Math.random() * (i + 1));
4-
[array[i], array[j]] = [array[j], array[i]];
1+
export const shuffleArray = <T>(array: T[]): T[] | undefined => {
2+
try {
3+
for (let i = array.length - 1; i > 0; i--) {
4+
const j = Math.floor(Math.random() * (i + 1));
5+
[array[i], array[j]] = [array[j], array[i]];
6+
}
7+
return array;
8+
} catch (error) {
9+
console.error("Error shuffling array", error);
510
}
6-
return array;
711
};

src/toKebabCase.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
export const toKebabCase = (str: string) =>
2-
str
3-
.trim()
4-
.toLowerCase()
5-
.replace(/[^a-z0-9\s]/g, "")
6-
.replace(/\s+/g, "-");
1+
export const toKebabCase = (str: string) => {
2+
try {
3+
return str
4+
.trim()
5+
.toLowerCase()
6+
.replace(/[^a-z0-9\s]/g, "")
7+
.replace(/\s+/g, "-");
8+
} catch (error) {
9+
console.error("Error toKebabCase", error);
10+
}
11+
};

src/unslugify.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
export const unslugify = (slug: string): string =>
2-
slug
3-
.replace(/[-_]/g, " ")
4-
.replace(/\s+/g, " ")
5-
.trim()
6-
.replace(
7-
/\w\S*/g,
8-
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
9-
);
1+
export const unslugify = (slug: string): string | undefined => {
2+
try {
3+
return slug
4+
.replace(/[-_]/g, " ")
5+
.replace(/\s+/g, " ")
6+
.trim()
7+
.replace(
8+
/\w\S*/g,
9+
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
10+
);
11+
} catch (error) {
12+
console.error("Error unslugify", error);
13+
}
14+
};

0 commit comments

Comments
 (0)