initial
This commit is contained in:
66
src/helpers/common.ts
Normal file
66
src/helpers/common.ts
Normal 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 '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user