-export class Utils {
- private constructor() {
- // This is intentional
- }
-
- public static logPrefix = (prefixString = ''): string => {
- return `${new Date().toLocaleString()}${prefixString}`;
- };
-
- public static generateUUID(): string {
- return crypto.randomUUID();
- }
-
- public static validateUUID(uuid: string): boolean {
- return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
- uuid
- );
- }
-
- public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
- return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
- }
-
- public static formatDurationMilliSeconds(duration: number): string {
- duration = Utils.convertToInt(duration);
- const hours = Math.floor(duration / (3600 * 1000));
- const minutes = Math.floor((duration / 1000 - hours * 3600) / 60);
- const seconds = duration / 1000 - hours * 3600 - minutes * 60;
- let hoursStr = hours.toString();
- let minutesStr = minutes.toString();
- let secondsStr = seconds.toString();
-
- if (hours < 10) {
- hoursStr = `0${hours.toString()}`;
- }
- if (minutes < 10) {
- minutesStr = `0${minutes.toString()}`;
- }
- if (seconds < 10) {
- secondsStr = `0${seconds.toString()}`;
- }
- return `${hoursStr}:${minutesStr}:${secondsStr.substring(0, 6)}`;
- }
-
- public static formatDurationSeconds(duration: number): string {
- return Utils.formatDurationMilliSeconds(duration * 1000);
- }
-
- public static convertToDate(
- value: Date | string | number | null | undefined
- ): Date | null | undefined {
- if (Utils.isNullOrUndefined(value)) {
- return value as null | undefined;
- }
- if (value instanceof Date) {
- return value;
- }
- if (Utils.isString(value) || typeof value === 'number') {
- return new Date(value);
- }
- return null;
- }
-
- public static convertToInt(value: unknown): number {
- if (!value) {
- return 0;
- }
- let changedValue: number = value as number;
- if (Number.isSafeInteger(value)) {
- return value as number;
- }
- if (typeof value === 'number') {
- return Math.trunc(value);
- }
- if (Utils.isString(value)) {
- changedValue = parseInt(value as string);
- }
- if (isNaN(changedValue)) {
- throw new Error(`Cannot convert to integer: ${value.toString()}`);
- }
- return changedValue;
- }
-
- public static convertToFloat(value: unknown): number {
- if (!value) {
- return 0;
- }
- let changedValue: number = value as number;
- if (Utils.isString(value)) {
- changedValue = parseFloat(value as string);
- }
- if (isNaN(changedValue)) {
- throw new Error(`Cannot convert to float: ${value.toString()}`);
- }
- return changedValue;
- }
-
- public static convertToBoolean(value: unknown): boolean {
- let result = false;
- if (value) {
- // Check the type
- if (typeof value === 'boolean') {
- return value;
- } else if (
- Utils.isString(value) &&
- ((value as string).toLowerCase() === 'true' || value === '1')
- ) {
- result = true;
- } else if (typeof value === 'number' && value === 1) {
- result = true;
- }
- }
- return result;
- }
-
- public static getRandomFloat(max = Number.MAX_VALUE, min = 0): number {
- if (max < min) {
- throw new RangeError('Invalid interval');
- }
- if (max - min === Infinity) {
- throw new RangeError('Invalid interval');
- }
- return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min;
- }
-
- public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number {
- max = Math.floor(max);
- if (!Utils.isNullOrUndefined(min) && min !== 0) {
- min = Math.ceil(min);
- return Math.floor(crypto.randomInt(min, max + 1));
- }
- return Math.floor(crypto.randomInt(max + 1));
- }
-
- public static roundTo(numberValue: number, scale: number): number {
- const roundPower = Math.pow(10, scale);
- return Math.round(numberValue * roundPower) / roundPower;
- }
-
- public static truncTo(numberValue: number, scale: number): number {
- const truncPower = Math.pow(10, scale);
- return Math.trunc(numberValue * truncPower) / truncPower;
- }