Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic helper #35

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
79 changes: 79 additions & 0 deletions src/utils/basicHelper.ts
Original file line number Diff line number Diff line change
@@ -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<string[]}
*/
async sortArray(arrayToSort: string[]): Promise<string[]> {
return arrayToSort.sort((a: string, b: string): number => a.localeCompare(b));
},

/**
* Sort array of numbers
* @param arrayToSort {number[]} Array to sort
* @return {Promise<number[]}
*/
async sortArrayNumber(arrayToSort: number[]): Promise<number[]> {
return arrayToSort.sort((a: number, b: number): number => a - b);
},

/**
* Sort array of dates
* @param arrayToSort {string[]} Array to sort
* @return {Promise<string[]}
*/
async sortArrayDate(arrayToSort: string[]): Promise<string[]> {
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<number|float>}
*/
async percentage(num: number, percentage: number): Promise<number> {
return (num / 100) * percentage;
},

/**
* Calculate age
* @param birthdate {string} Date of birth
* @returns {Promise<number>}
*/
async age(birthdate: Date): Promise<number> {
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<number> {
return text.split(searchedValue).length - 1;
},
};
Loading