1 import { v4
as uuid
} from
'uuid';
3 export default class Utils
{
4 static generateUUID(): string {
8 static async sleep(milliSeconds
: number): Promise
<NodeJS
.Timeout
> {
9 return new Promise((resolve
) => setTimeout(resolve
, milliSeconds
));
12 static secondsToHHMMSS(seconds
: number): string {
13 return new Date(seconds
* 1000).toISOString().substr(11, 8);
16 static milliSecondsToHHMMSS(milliSeconds
: number): string {
17 return new Date(milliSeconds
).toISOString().substr(11, 8);
20 static removeExtraEmptyLines(tab
): void {
22 for (let i
= tab
.length
- 1; i
> 0; i
--) {
23 // Two consecutive empty lines?
24 if (tab
[i
].length
=== 0 && tab
[i
- 1].length
=== 0) {
25 // Remove the last one
29 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
30 // Remove the first one
36 static convertToDate(value
): Date {
42 if (!(value
instanceof Date)) {
43 return new Date(value
);
48 static convertToInt(value
): number {
49 let changedValue
= value
;
53 if (Number.isSafeInteger(value
)) {
57 if (typeof value
=== 'string') {
59 changedValue
= parseInt(value
);
64 static convertToFloat(value
): number {
65 let changedValue
= value
;
70 if (typeof value
=== 'string') {
72 changedValue
= parseFloat(value
);
77 static convertToBoolean(value
): boolean {
82 if (typeof value
=== 'boolean') {
87 result
= (value
=== 'true');
93 static getRandomFloat(max
: number, min
= 0): number {
94 return Math.random() < 0.5 ? (1 - Math.random()) * (max
- min
) + min
: Math.random() * (max
- min
) + min
;
97 static getRandomInt(max
: number, min
= 0): number {
99 return Math.floor(Math.random() * (max
- min
+ 1) + min
);
101 return Math.floor(Math.random() * max
+ 1);
104 static roundTo(number: number, scale
: number): number {
105 const roundPower
= Math.pow(10, scale
);
106 return Math.round(number * roundPower
) / roundPower
;
109 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
111 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
113 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
116 static logPrefix(prefixString
= ''): string {
117 const date
= new Date();
118 return date
.toLocaleString() + prefixString
;
121 static cloneObject(object
) {
122 return JSON
.parse(JSON
.stringify(object
));
125 static isIterable(obj
): boolean {
127 return typeof obj
[Symbol
.iterator
] === 'function';
132 static isEmptyJSon(document
): boolean {
138 if (typeof document
!== 'object') {
142 return Object.keys(document
).length
=== 0;
145 static isString(value
): boolean {
146 return typeof value
=== 'string';
149 static isUndefined(value
): boolean {
150 return typeof value
=== 'undefined';
153 static isNullOrUndefined(value
): boolean {
154 // eslint-disable-next-line no-eq-null
161 static isEmptyArray(object
): boolean {
165 if (Array.isArray(object
) && object
.length
> 0) {
171 static isEmptyObject(obj
): boolean {
172 return !Object.keys(obj
).length
;
175 static insertAt
= (str
: string, subStr
: string, pos
: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
178 * @param {number} [retryNumber=0]
179 * @return {number} - delay in milliseconds
181 static exponentialDelay(retryNumber
= 0): number {
182 const delay
= Math.pow(2, retryNumber
) * 100;
183 const randomSum
= delay
* 0.2 * Math.random(); // 0-20% of the delay
184 return delay
+ randomSum
;