This commit is contained in:
2024-11-30 13:24:46 +07:00
commit 3e32e5869e
30 changed files with 1196 additions and 0 deletions

66
src/helpers/common.ts Normal file
View File

@ -0,0 +1,66 @@
import { Logger, ILogObj } from 'tslog';
const log: Logger<ILogObj> = new Logger({ name: '[CommonHelper]', type: 'pretty' });
export default class CommonHelper {
static randomInteger(min: number, max: number): number | null {
if (min > 0 && max > 0) {
return Math.floor(Math.random() * (max - min + 1)) + min;
} else {
log.error(new Error('Min & Max must be > 0'));
return null
}
}
static inArray(array: string[], keyword: string): boolean {
for (let val of array) {
if (val === keyword) {
return true;
}
}
return false;
}
static async sleep(seconds: number): Promise<void> {
if (seconds > 0) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
} else {
log.error(new Error('Second must be > 0'));
}
}
static convertArrayToObject(object: any, key: string, val = ''): any {
const ret: any = {};
for (let i in object) {
const d = object[i];
if (val) {
ret[d[key]] = d[val];
} else {
ret[d[key]] = d;
}
}
return ret;
}
static countObject(obj: any): number {
let c = 0;
for (let s in obj) {
c++;
}
return c;
}
static capitalizeFirstLetter(str: string): string {
if (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
return '';
}
}