1 import { WebSocketCloseEventStatusString
} from
'../types/WebSocket';
2 import { v4
as uuid
} from
'uuid';
4 export default class Utils
{
5 static generateUUID(): string {
9 static async sleep(milliSeconds
: number): Promise
<NodeJS
.Timeout
> {
10 return new Promise((resolve
) => setTimeout(resolve
, milliSeconds
));
13 static secondsToHHMMSS(seconds
: number): string {
14 return Utils
.milliSecondsToHHMMSS(seconds
* 1000);
17 static milliSecondsToHHMMSS(milliSeconds
: number): string {
18 return new Date(milliSeconds
).toISOString().substr(11, 8);
21 static removeExtraEmptyLines(tab
: string[]): void {
23 for (let i
= tab
.length
- 1; i
> 0; i
--) {
24 // Two consecutive empty lines?
25 if (tab
[i
].length
=== 0 && tab
[i
- 1].length
=== 0) {
26 // Remove the last one
30 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
31 // Remove the first one
37 static convertToDate(value
): Date {
43 if (!(value
instanceof Date)) {
44 return new Date(value
);
49 static convertToInt(value
): number {
50 let changedValue
= value
;
54 if (Number.isSafeInteger(value
)) {
58 if (typeof value
=== 'string') {
60 changedValue
= parseInt(value
);
65 static convertToFloat(value
): number {
66 let changedValue
= value
;
71 if (typeof value
=== 'string') {
73 changedValue
= parseFloat(value
);
78 static convertToBoolean(value
): boolean {
83 if (typeof value
=== 'boolean') {
88 result
= (value
=== 'true');
94 static getRandomFloat(max
: number, min
= 0): number {
95 return Math.random() < 0.5 ? (1 - Math.random()) * (max
- min
) + min
: Math.random() * (max
- min
) + min
;
98 static getRandomInt(max
: number, min
= 0): number {
100 return Math.floor(Math.random() * (max
- min
+ 1) + min
);
102 return Math.floor(Math.random() * max
+ 1);
105 static roundTo(number: number, scale
: number): number {
106 const roundPower
= Math.pow(10, scale
);
107 return Math.round(number * roundPower
) / roundPower
;
110 static truncTo(number: number, scale
: number): number {
111 const truncPower
= Math.pow(10, scale
);
112 return Math.trunc(number * truncPower
) / truncPower
;
115 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
117 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
119 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
122 static logPrefix(prefixString
= ''): string {
123 const date
= new Date();
124 return date
.toLocaleString() + prefixString
;
127 static cloneObject
<T
>(object
: T
): T
{
128 return JSON
.parse(JSON
.stringify(object
)) as T
;
131 static isIterable(obj
): boolean {
133 return typeof obj
[Symbol
.iterator
] === 'function';
138 static isEmptyJSon(document
): boolean {
144 if (typeof document
!== 'object') {
148 return Object.keys(document
).length
=== 0;
151 static isString(value
): boolean {
152 return typeof value
=== 'string';
155 static isUndefined(value
): boolean {
156 return typeof value
=== 'undefined';
159 static isNullOrUndefined(value
): boolean {
160 // eslint-disable-next-line no-eq-null, eqeqeq
167 static isEmptyArray(object
): boolean {
171 if (Array.isArray(object
) && object
.length
> 0) {
177 static isEmptyObject(obj
): boolean {
178 return !Object.keys(obj
).length
;
181 static insertAt
= (str
: string, subStr
: string, pos
: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
184 * @param {number} [retryNumber=0]
185 * @return {number} - delay in milliseconds
187 static exponentialDelay(retryNumber
= 0): number {
188 const delay
= Math.pow(2, retryNumber
) * 100;
189 const randomSum
= delay
* 0.2 * Math.random(); // 0-20% of the delay
190 return delay
+ randomSum
;
193 static getWebSocketCloseEventStatusString(code
: number): string {
194 if (code
>= 0 && code
<= 999) {
196 } else if (code
>= 1016) {
198 return '(For WebSocket standard)';
199 } else if (code
<= 2999) {
200 return '(For WebSocket extensions)';
201 } else if (code
<= 3999) {
202 return '(For libraries and frameworks)';
203 } else if (code
<= 4999) {
204 return '(For applications)';
207 if (!Utils
.isUndefined(WebSocketCloseEventStatusString
[code
])) {
208 return WebSocketCloseEventStatusString
[code
];