Cleanup workers handling classes.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
CommitLineData
32a1eb7a 1import { WebSocketCloseEventStatusString } from '../types/WebSocket';
6af9012e 2import { v4 as uuid } from 'uuid';
7dde0b73 3
3f40bc9c 4export default class Utils {
6af9012e 5 static generateUUID(): string {
2e3ac96d 6 return uuid();
7dde0b73
JB
7 }
8
9ac86a7e
JB
9 static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
10 return new Promise((resolve) => setTimeout(resolve, milliSeconds));
7dde0b73
JB
11 }
12
9ac86a7e 13 static secondsToHHMMSS(seconds: number): string {
a9dc13ad 14 return Utils.milliSecondsToHHMMSS(seconds * 1000);
9ac86a7e
JB
15 }
16
17 static milliSecondsToHHMMSS(milliSeconds: number): string {
18 return new Date(milliSeconds).toISOString().substr(11, 8);
7dde0b73
JB
19 }
20
ca64a820 21 static removeExtraEmptyLines(tab: string[]): void {
7dde0b73
JB
22 // Start from the end
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
27 tab.splice(i, 1);
28 }
29 // Check last line
30 if (i === 1 && tab[i - 1].length === 0) {
31 // Remove the first one
32 tab.splice(i - 1, 1);
33 }
34 }
35 }
36
6af9012e 37 static convertToDate(value): Date {
560bcf5b 38 // Check
6af9012e
JB
39 if (!value) {
40 return value;
560bcf5b
JB
41 }
42 // Check Type
6af9012e
JB
43 if (!(value instanceof Date)) {
44 return new Date(value);
7dde0b73 45 }
6af9012e 46 return value;
7dde0b73
JB
47 }
48
6af9012e 49 static convertToInt(value): number {
72766a82
JB
50 let changedValue = value;
51 if (!value) {
7dde0b73
JB
52 return 0;
53 }
72766a82
JB
54 if (Number.isSafeInteger(value)) {
55 return value;
56 }
7dde0b73 57 // Check
72766a82 58 if (typeof value === 'string') {
7dde0b73 59 // Create Object
72766a82 60 changedValue = parseInt(value);
7dde0b73 61 }
72766a82 62 return changedValue;
7dde0b73
JB
63 }
64
6af9012e 65 static convertToFloat(value): number {
72766a82
JB
66 let changedValue = value;
67 if (!value) {
7dde0b73
JB
68 return 0;
69 }
70 // Check
72766a82 71 if (typeof value === 'string') {
7dde0b73 72 // Create Object
72766a82 73 changedValue = parseFloat(value);
7dde0b73 74 }
72766a82 75 return changedValue;
7dde0b73
JB
76 }
77
6af9012e 78 static convertToBoolean(value): boolean {
a6e68f34
JB
79 let result = false;
80 // Check boolean
81 if (value) {
82 // Check the type
83 if (typeof value === 'boolean') {
84 // Already a boolean
85 result = value;
86 } else {
87 // Convert
88 result = (value === 'true');
89 }
90 }
91 return result;
92 }
93
6af9012e 94 static getRandomFloat(max: number, min = 0): number {
fee83021 95 return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min;
560bcf5b
JB
96 }
97
6af9012e 98 static getRandomInt(max: number, min = 0): number {
7dde0b73 99 if (min) {
fee83021 100 return Math.floor(Math.random() * (max - min + 1) + min);
7dde0b73 101 }
fee83021 102 return Math.floor(Math.random() * max + 1);
560bcf5b
JB
103 }
104
6af9012e 105 static roundTo(number: number, scale: number): number {
ad3de6c4
JB
106 const roundPower = Math.pow(10, scale);
107 return Math.round(number * roundPower) / roundPower;
560bcf5b
JB
108 }
109
6d3a11a0
JB
110 static truncTo(number: number, scale: number): number {
111 const truncPower = Math.pow(10, scale);
112 return Math.trunc(number * truncPower) / truncPower;
113 }
114
6af9012e 115 static getRandomFloatRounded(max: number, min = 0, scale = 2): number {
560bcf5b
JB
116 if (min) {
117 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
118 }
119 return Utils.roundTo(Utils.getRandomFloat(max), scale);
7dde0b73
JB
120 }
121
6af9012e 122 static logPrefix(prefixString = ''): string {
7dde0b73 123 const date = new Date();
4455e614 124 return date.toLocaleString() + prefixString;
7dde0b73 125 }
1d7ca20c 126
e56aa9a4
JB
127 static cloneObject<T>(object: T): T {
128 return JSON.parse(JSON.stringify(object)) as T;
2e6f5966 129 }
facd8ebd 130
6af9012e 131 static isIterable(obj): boolean {
67e9cccf
JB
132 if (obj) {
133 return typeof obj[Symbol.iterator] === 'function';
134 }
135 return false;
136 }
137
6af9012e 138 static isEmptyJSon(document): boolean {
67e9cccf
JB
139 // Empty?
140 if (!document) {
141 return true;
142 }
143 // Check type
144 if (typeof document !== 'object') {
145 return true;
146 }
147 // Check
148 return Object.keys(document).length === 0;
149 }
150
6af9012e 151 static isString(value): boolean {
560bcf5b
JB
152 return typeof value === 'string';
153 }
154
7ec46a9a 155 static isUndefined(value): boolean {
ead548f2
JB
156 return typeof value === 'undefined';
157 }
158
6af9012e 159 static isNullOrUndefined(value): boolean {
32a1eb7a 160 // eslint-disable-next-line no-eq-null, eqeqeq
ead548f2 161 if (value == null) {
facd8ebd
JB
162 return true;
163 }
164 return false;
165 }
4a56deef 166
6af9012e 167 static isEmptyArray(object): boolean {
fd1ee77c
JB
168 if (!object) {
169 return true;
170 }
4a56deef
JB
171 if (Array.isArray(object) && object.length > 0) {
172 return false;
173 }
174 return true;
175 }
7abfea5f 176
6af9012e 177 static isEmptyObject(obj): boolean {
7abfea5f
JB
178 return !Object.keys(obj).length;
179 }
7ec46a9a
JB
180
181 static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
032d6efc
JB
182
183 /**
184 * @param {number} [retryNumber=0]
185 * @return {number} - delay in milliseconds
186 */
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;
191 }
32a1eb7a
JB
192
193 static getWebSocketCloseEventStatusString(code: number): string {
194 if (code >= 0 && code <= 999) {
195 return '(Unused)';
196 } else if (code >= 1016) {
197 if (code <= 1999) {
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)';
205 }
206 }
207 if (!Utils.isUndefined(WebSocketCloseEventStatusString[code])) {
17991e8c 208 return WebSocketCloseEventStatusString[code] as string;
32a1eb7a
JB
209 }
210 return '(Unknown)';
211 }
7dde0b73 212}