83cb66ecfb2605c481695594a4cb89a99b8de9ea
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 truncTo(number: number, scale
: number): number {
110 const truncPower
= Math.pow(10, scale
);
111 return Math.trunc(number * truncPower
) / truncPower
;
114 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
116 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
118 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
121 static logPrefix(prefixString
= ''): string {
122 const date
= new Date();
123 return date
.toLocaleString() + prefixString
;
126 static cloneObject
<T
>(object
: T
): T
{
127 return JSON
.parse(JSON
.stringify(object
)) as T
;
130 static isIterable(obj
): boolean {
132 return typeof obj
[Symbol
.iterator
] === 'function';
137 static isEmptyJSon(document
): boolean {
143 if (typeof document
!== 'object') {
147 return Object.keys(document
).length
=== 0;
150 static isString(value
): boolean {
151 return typeof value
=== 'string';
154 static isUndefined(value
): boolean {
155 return typeof value
=== 'undefined';
158 static isNullOrUndefined(value
): boolean {
159 // eslint-disable-next-line no-eq-null
166 static isEmptyArray(object
): boolean {
170 if (Array.isArray(object
) && object
.length
> 0) {
176 static isEmptyObject(obj
): boolean {
177 return !Object.keys(obj
).length
;
180 static insertAt
= (str
: string, subStr
: string, pos
: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
183 * @param {number} [retryNumber=0]
184 * @return {number} - delay in milliseconds
186 static exponentialDelay(retryNumber
= 0): number {
187 const delay
= Math.pow(2, retryNumber
) * 100;
188 const randomSum
= delay
* 0.2 * Math.random(); // 0-20% of the delay
189 return delay
+ randomSum
;