From 8cdef9cdbee544f349faac8d96572da823944272 Mon Sep 17 00:00:00 2001 From: nesrineabdmouleh Date: Tue, 28 May 2024 15:30:04 +0200 Subject: [PATCH 1/2] Add basic helper --- src/index.ts | 1 + src/utils/basicHelper.ts | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/utils/basicHelper.ts diff --git a/src/index.ts b/src/index.ts index e2ddf882..8067dafd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -98,3 +98,4 @@ export {default as modBlockwishlistBoStatistics} from '@pages/BO/modules/blockwi // Export utils export {default as testContext} from '@utils/test'; +export {default as basicHelper} from '@utils/basicHelper'; diff --git a/src/utils/basicHelper.ts b/src/utils/basicHelper.ts new file mode 100644 index 00000000..e29f09f9 --- /dev/null +++ b/src/utils/basicHelper.ts @@ -0,0 +1,79 @@ +const today: Date = new Date(); + +/** + * @module basicHelper + * @description Basic helper used to wrap basic methods don't used on pages + */ +export default { + /** + * Sort array of strings + * @param arrayToSort {string[]} Array to sort + * @return {Promise { + return arrayToSort.sort((a: string, b: string): number => a.localeCompare(b)); + }, + + /** + * Sort array of numbers + * @param arrayToSort {number[]} Array to sort + * @return {Promise { + return arrayToSort.sort((a: number, b: number): number => a - b); + }, + + /** + * Sort array of dates + * @param arrayToSort {string[]} Array to sort + * @return {Promise { + return arrayToSort.sort((a: string, b: string): number => new Date(a).getTime() - new Date(b).getTime()); + }, + + /** + * Calculate percentage + * @param num {number|float} Number to do the percentage + * @param percentage {number} Percentage value + * @returns {Promise} + */ + async percentage(num: number, percentage: number): Promise { + return (num / 100) * percentage; + }, + + /** + * Calculate age + * @param birthdate {string} Date of birth + * @returns {Promise} + */ + async age(birthdate: Date): Promise { + const age = today.getFullYear() - birthdate.getFullYear(); + + if (today.getMonth() < birthdate.getMonth() + || (today.getMonth() === birthdate.getMonth() && today.getDate() < birthdate.getDate())) { + return age - 1; + } + + return age; + }, + + /** + * Make a string's first character uppercase + * @param value {string} + * @returns {string} + */ + capitalize(value: string): string { + return value.charAt(0).toUpperCase() + value.slice(1); + }, + + /** + * Search occurrence of a value in text + * @param text {string} + * @param searchedValue {string} + * @returns {string} + */ + async searchOccurrence(text: string, searchedValue: string): Promise { + return text.split(searchedValue).length - 1; + }, +}; \ No newline at end of file From 02ad77f1ad437144a78cbd00b82f6e6b1e18859c Mon Sep 17 00:00:00 2001 From: nesrineabdmouleh Date: Tue, 28 May 2024 15:36:03 +0200 Subject: [PATCH 2/2] Fix lint errors --- src/utils/basicHelper.ts | 66 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/utils/basicHelper.ts b/src/utils/basicHelper.ts index e29f09f9..6c5a6136 100644 --- a/src/utils/basicHelper.ts +++ b/src/utils/basicHelper.ts @@ -5,75 +5,75 @@ const today: Date = new Date(); * @description Basic helper used to wrap basic methods don't used on pages */ export default { - /** + /** * Sort array of strings * @param arrayToSort {string[]} Array to sort * @return {Promise { - return arrayToSort.sort((a: string, b: string): number => a.localeCompare(b)); - }, + async sortArray(arrayToSort: string[]): Promise { + return arrayToSort.sort((a: string, b: string): number => a.localeCompare(b)); + }, - /** + /** * Sort array of numbers * @param arrayToSort {number[]} Array to sort * @return {Promise { - return arrayToSort.sort((a: number, b: number): number => a - b); - }, + async sortArrayNumber(arrayToSort: number[]): Promise { + return arrayToSort.sort((a: number, b: number): number => a - b); + }, - /** + /** * Sort array of dates * @param arrayToSort {string[]} Array to sort * @return {Promise { - return arrayToSort.sort((a: string, b: string): number => new Date(a).getTime() - new Date(b).getTime()); - }, + async sortArrayDate(arrayToSort: string[]): Promise { + return arrayToSort.sort((a: string, b: string): number => new Date(a).getTime() - new Date(b).getTime()); + }, - /** + /** * Calculate percentage * @param num {number|float} Number to do the percentage * @param percentage {number} Percentage value * @returns {Promise} */ - async percentage(num: number, percentage: number): Promise { - return (num / 100) * percentage; - }, + async percentage(num: number, percentage: number): Promise { + return (num / 100) * percentage; + }, - /** + /** * Calculate age * @param birthdate {string} Date of birth * @returns {Promise} */ - async age(birthdate: Date): Promise { - const age = today.getFullYear() - birthdate.getFullYear(); + async age(birthdate: Date): Promise { + const age = today.getFullYear() - birthdate.getFullYear(); - if (today.getMonth() < birthdate.getMonth() + if (today.getMonth() < birthdate.getMonth() || (today.getMonth() === birthdate.getMonth() && today.getDate() < birthdate.getDate())) { - return age - 1; - } + return age - 1; + } - return age; - }, + return age; + }, - /** + /** * Make a string's first character uppercase * @param value {string} * @returns {string} */ - capitalize(value: string): string { - return value.charAt(0).toUpperCase() + value.slice(1); - }, + capitalize(value: string): string { + return value.charAt(0).toUpperCase() + value.slice(1); + }, - /** + /** * Search occurrence of a value in text * @param text {string} * @param searchedValue {string} * @returns {string} */ - async searchOccurrence(text: string, searchedValue: string): Promise { - return text.split(searchedValue).length - 1; - }, -}; \ No newline at end of file + async searchOccurrence(text: string, searchedValue: string): Promise { + return text.split(searchedValue).length - 1; + }, +};