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