1 import { v4
as uuid
} from
'uuid';
3 export default class Utils
{
4 static generateUUID(): string {
8 static async sleep(ms
: number): Promise
<NodeJS
.Timeout
> {
9 return new Promise((resolve
) => setTimeout(resolve
, ms
));
12 static secondstoHHMMSS(seconds
): string {
13 const date
= new Date();
14 date
.setSeconds(seconds
);
15 return date
.toISOString().substr(11, 8);
18 static removeExtraEmptyLines(tab
): void {
20 for (let i
= tab
.length
- 1; i
> 0; i
--) {
21 // Two consecutive empty lines?
22 if (tab
[i
].length
=== 0 && tab
[i
- 1].length
=== 0) {
23 // Remove the last one
27 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
28 // Remove the first one
34 static convertToDate(value
): Date {
40 if (!(value
instanceof Date)) {
41 return new Date(value
);
46 static convertToInt(value
): number {
47 let changedValue
= value
;
51 if (Number.isSafeInteger(value
)) {
55 if (typeof value
=== 'string') {
57 changedValue
= parseInt(value
);
62 static convertToFloat(value
): number {
63 let changedValue
= value
;
68 if (typeof value
=== 'string') {
70 changedValue
= parseFloat(value
);
75 static convertToBoolean(value
): boolean {
80 if (typeof value
=== 'boolean') {
85 result
= (value
=== 'true');
91 static getRandomFloat(max
: number, min
= 0): number {
92 return Math.random() < 0.5 ? (1 - Math.random()) * (max
- min
) + min
: Math.random() * (max
- min
) + min
;
95 static getRandomInt(max
: number, min
= 0): number {
97 return Math.floor(Math.random() * (max
- min
+ 1) + min
);
99 return Math.floor(Math.random() * max
+ 1);
102 static roundTo(number: number, scale
: number): number {
103 return Utils
.convertToFloat(number.toFixed(scale
));
106 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
108 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
110 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
113 static logPrefix(prefixString
= ''): string {
114 const date
= new Date();
115 return date
.toLocaleString() + prefixString
;
118 static objectHasOwnProperty(object
, property
) {
119 return Object.prototype
.hasOwnProperty
.call(object
, property
);
122 static cloneObject(object
) {
123 return JSON
.parse(JSON
.stringify(object
));
126 static isIterable(obj
): boolean {
128 return typeof obj
[Symbol
.iterator
] === 'function';
133 static isEmptyJSon(document
): boolean {
139 if (typeof document
!== 'object') {
143 return Object.keys(document
).length
=== 0;
146 static isString(value
): boolean {
147 return typeof value
=== 'string';
150 static isUndefined(value
) {
151 return typeof value
=== 'undefined';
154 static isNullOrUndefined(value
): boolean {
155 // eslint-disable-next-line no-eq-null
162 static isEmptyArray(object
): boolean {
163 if (Array.isArray(object
) && object
.length
> 0) {
169 static isEmptyObject(obj
): boolean {
170 return !Object.keys(obj
).length
;