86b0daa2d57a15a8f4f9893e28e49899b34b6eb1
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
1 import crypto from 'crypto';
2
3 import clone from 'just-clone';
4
5 import { WebSocketCloseEventStatusString } from '../types/WebSocket';
6
7 export default class Utils {
8 private constructor() {
9 // This is intentional
10 }
11
12 public static logPrefix(prefixString = ''): string {
13 return new Date().toLocaleString() + prefixString;
14 }
15
16 public static generateUUID(): string {
17 return crypto.randomUUID();
18 }
19
20 public static validateUUID(uuid: string): boolean {
21 return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
22 uuid
23 );
24 }
25
26 public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
27 return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
28 }
29
30 public static formatDurationMilliSeconds(duration: number): string {
31 duration = Utils.convertToInt(duration);
32 const hours = Math.floor(duration / (3600 * 1000));
33 const minutes = Math.floor((duration / 1000 - hours * 3600) / 60);
34 const seconds = duration / 1000 - hours * 3600 - minutes * 60;
35 let hoursStr = hours.toString();
36 let minutesStr = minutes.toString();
37 let secondsStr = seconds.toString();
38
39 if (hours < 10) {
40 hoursStr = '0' + hours.toString();
41 }
42 if (minutes < 10) {
43 minutesStr = '0' + minutes.toString();
44 }
45 if (seconds < 10) {
46 secondsStr = '0' + seconds.toString();
47 }
48 return hoursStr + ':' + minutesStr + ':' + secondsStr.substring(0, 6);
49 }
50
51 public static formatDurationSeconds(duration: number): string {
52 return Utils.formatDurationMilliSeconds(duration * 1000);
53 }
54
55 public static convertToDate(
56 value: Date | string | number | null | undefined
57 ): Date | null | undefined {
58 if (Utils.isNullOrUndefined(value)) {
59 return value as null | undefined;
60 }
61 if (value instanceof Date) {
62 return value;
63 }
64 if (Utils.isString(value) || typeof value === 'number') {
65 return new Date(value);
66 }
67 return null;
68 }
69
70 public static convertToInt(value: unknown): number {
71 if (!value) {
72 return 0;
73 }
74 let changedValue: number = value as number;
75 if (Number.isSafeInteger(value)) {
76 return value as number;
77 }
78 if (typeof value === 'number') {
79 return Math.trunc(value);
80 }
81 if (Utils.isString(value)) {
82 changedValue = parseInt(value as string);
83 }
84 if (isNaN(changedValue)) {
85 throw new Error(`Cannot convert to integer: ${value.toString()}`);
86 }
87 return changedValue;
88 }
89
90 public static convertToFloat(value: unknown): number {
91 if (!value) {
92 return 0;
93 }
94 let changedValue: number = value as number;
95 if (Utils.isString(value)) {
96 changedValue = parseFloat(value as string);
97 }
98 if (isNaN(changedValue)) {
99 throw new Error(`Cannot convert to float: ${value.toString()}`);
100 }
101 return changedValue;
102 }
103
104 public static convertToBoolean(value: unknown): boolean {
105 let result = false;
106 if (value) {
107 // Check the type
108 if (typeof value === 'boolean') {
109 return value;
110 } else if (
111 Utils.isString(value) &&
112 ((value as string).toLowerCase() === 'true' || value === '1')
113 ) {
114 result = true;
115 } else if (typeof value === 'number' && value === 1) {
116 result = true;
117 }
118 }
119 return result;
120 }
121
122 public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number {
123 if (max < min || max < 0 || min < 0) {
124 throw new RangeError('Invalid interval');
125 }
126 const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff;
127 const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1;
128 return sign * (randomPositiveFloat * (max - min) + min);
129 }
130
131 public static getRandomInteger(max = Number.MAX_SAFE_INTEGER, min = 0): number {
132 if (max < min || max < 0 || min < 0) {
133 throw new RangeError('Invalid interval');
134 }
135 max = Math.floor(max);
136 if (!Utils.isNullOrUndefined(min) && min !== 0) {
137 min = Math.ceil(min);
138 return Math.floor(Utils.secureRandom() * (max - min + 1)) + min;
139 }
140 return Math.floor(Utils.secureRandom() * (max + 1));
141 }
142
143 public static roundTo(numberValue: number, scale: number): number {
144 const roundPower = Math.pow(10, scale);
145 return Math.round(numberValue * roundPower) / roundPower;
146 }
147
148 public static truncTo(numberValue: number, scale: number): number {
149 const truncPower = Math.pow(10, scale);
150 return Math.trunc(numberValue * truncPower) / truncPower;
151 }
152
153 public static getRandomFloatRounded(max = Number.MAX_VALUE, min = 0, scale = 2): number {
154 if (min) {
155 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
156 }
157 return Utils.roundTo(Utils.getRandomFloat(max), scale);
158 }
159
160 public static getRandomFloatFluctuatedRounded(
161 staticValue: number,
162 fluctuationPercent: number,
163 scale = 2
164 ): number {
165 if (fluctuationPercent === 0) {
166 return Utils.roundTo(staticValue, scale);
167 }
168 const fluctuationRatio = fluctuationPercent / 100;
169 return Utils.getRandomFloatRounded(
170 staticValue * (1 + fluctuationRatio),
171 staticValue * (1 - fluctuationRatio),
172 scale
173 );
174 }
175
176 public static isObject(item: unknown): boolean {
177 return (
178 Utils.isNullOrUndefined(item) === false &&
179 typeof item === 'object' &&
180 Array.isArray(item) === false
181 );
182 }
183
184 public static cloneObject<T extends object>(object: T): T {
185 return clone<T>(object);
186 }
187
188 public static isIterable<T>(obj: T): boolean {
189 return obj ? typeof obj[Symbol.iterator] === 'function' : false;
190 }
191
192 public static isString(value: unknown): boolean {
193 return typeof value === 'string';
194 }
195
196 public static isEmptyString(value: unknown): boolean {
197 return Utils.isString(value) && (value as string).trim().length === 0;
198 }
199
200 public static isUndefined(value: unknown): boolean {
201 return typeof value === 'undefined';
202 }
203
204 public static isNullOrUndefined(value: unknown): boolean {
205 // eslint-disable-next-line eqeqeq, no-eq-null
206 return value == null ? true : false;
207 }
208
209 public static isEmptyArray(object: unknown | unknown[]): boolean {
210 if (!Array.isArray(object)) {
211 return true;
212 }
213 if (object.length > 0) {
214 return false;
215 }
216 return true;
217 }
218
219 public static isEmptyObject(obj: object): boolean {
220 if (obj?.constructor !== Object) {
221 return false;
222 }
223 // Iterates over the keys of an object, if
224 // any exist, return false.
225 for (const _ in obj) {
226 return false;
227 }
228 return true;
229 }
230
231 public static insertAt = (str: string, subStr: string, pos: number): string =>
232 `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
233
234 /**
235 * @param retryNumber - the number of retries that have already been attempted
236 * @returns delay in milliseconds
237 */
238 public static exponentialDelay(retryNumber = 0): number {
239 const delay = Math.pow(2, retryNumber) * 100;
240 const randomSum = delay * 0.2 * Utils.secureRandom(); // 0-20% of the delay
241 return delay + randomSum;
242 }
243
244 public static async promiseWithTimeout<T>(
245 promise: Promise<T>,
246 timeoutMs: number,
247 timeoutError: Error,
248 timeoutCallback: () => void = () => {
249 /* This is intentional */
250 }
251 ): Promise<T> {
252 // Create a timeout promise that rejects in timeout milliseconds
253 const timeoutPromise = new Promise<never>((_, reject) => {
254 setTimeout(() => {
255 timeoutCallback();
256 reject(timeoutError);
257 }, timeoutMs);
258 });
259
260 // Returns a race between timeout promise and the passed promise
261 return Promise.race<T>([promise, timeoutPromise]);
262 }
263
264 /**
265 * Generate a cryptographically secure random number in the [0,1[ range
266 *
267 * @returns
268 */
269 public static secureRandom(): number {
270 return crypto.randomBytes(4).readUInt32LE() / 0x100000000;
271 }
272
273 public static JSONStringifyWithMapSupport(
274 obj: Record<string, unknown> | Record<string, unknown>[] | Map<unknown, unknown>,
275 space?: number
276 ): string {
277 return JSON.stringify(
278 obj,
279 (key, value: Record<string, unknown>) => {
280 if (value instanceof Map) {
281 return {
282 dataType: 'Map',
283 value: [...value],
284 };
285 }
286 return value;
287 },
288 space
289 );
290 }
291
292 /**
293 * Convert websocket error code to human readable string message
294 *
295 * @param code - websocket error code
296 * @returns human readable string message
297 */
298 public static getWebSocketCloseEventStatusString(code: number): string {
299 if (code >= 0 && code <= 999) {
300 return '(Unused)';
301 } else if (code >= 1016) {
302 if (code <= 1999) {
303 return '(For WebSocket standard)';
304 } else if (code <= 2999) {
305 return '(For WebSocket extensions)';
306 } else if (code <= 3999) {
307 return '(For libraries and frameworks)';
308 } else if (code <= 4999) {
309 return '(For applications)';
310 }
311 }
312 if (!Utils.isUndefined(WebSocketCloseEventStatusString[code])) {
313 return WebSocketCloseEventStatusString[code] as string;
314 }
315 return '(Unknown)';
316 }
317 }