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 return Utils
.convertToFloat(number.toFixed(scale
));
108 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
110 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
112 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
115 static logPrefix(prefixString
= ''): string {
116 const date
= new Date();
117 return date
.toLocaleString() + prefixString
;
120 static objectHasOwnProperty(object
, property
): boolean {
121 return Object.prototype
.hasOwnProperty
.call(object
, property
);
124 static cloneObject(object
) {
125 return JSON
.parse(JSON
.stringify(object
));
128 static isIterable(obj
): boolean {
130 return typeof obj
[Symbol
.iterator
] === 'function';
135 static isEmptyJSon(document
): boolean {
141 if (typeof document
!== 'object') {
145 return Object.keys(document
).length
=== 0;
148 static isString(value
): boolean {
149 return typeof value
=== 'string';
152 static isUndefined(value
): boolean {
153 return typeof value
=== 'undefined';
156 static isNullOrUndefined(value
): boolean {
157 // eslint-disable-next-line no-eq-null
164 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)}`;