Avoid code duplicatiom.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
1 import { WebSocketCloseEventStatusString } from '../types/WebSocket';
2 import { v4 as uuid } from 'uuid';
3
4 export default class Utils {
5 static generateUUID(): string {
6 return uuid();
7 }
8
9 static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
10 return new Promise((resolve) => setTimeout(resolve, milliSeconds));
11 }
12
13 static secondsToHHMMSS(seconds: number): string {
14 return Utils.milliSecondsToHHMMSS(seconds * 1000);
15 }
16
17 static milliSecondsToHHMMSS(milliSeconds: number): string {
18 return new Date(milliSeconds).toISOString().substr(11, 8);
19 }
20
21 static removeExtraEmptyLines(tab: string[]): void {
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
37 static convertToDate(value): Date {
38 // Check
39 if (!value) {
40 return value;
41 }
42 // Check Type
43 if (!(value instanceof Date)) {
44 return new Date(value);
45 }
46 return value;
47 }
48
49 static convertToInt(value): number {
50 let changedValue = value;
51 if (!value) {
52 return 0;
53 }
54 if (Number.isSafeInteger(value)) {
55 return value;
56 }
57 // Check
58 if (typeof value === 'string') {
59 // Create Object
60 changedValue = parseInt(value);
61 }
62 return changedValue;
63 }
64
65 static convertToFloat(value): number {
66 let changedValue = value;
67 if (!value) {
68 return 0;
69 }
70 // Check
71 if (typeof value === 'string') {
72 // Create Object
73 changedValue = parseFloat(value);
74 }
75 return changedValue;
76 }
77
78 static convertToBoolean(value): boolean {
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
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;
96 }
97
98 static getRandomInt(max: number, min = 0): number {
99 if (min) {
100 return Math.floor(Math.random() * (max - min + 1) + min);
101 }
102 return Math.floor(Math.random() * max + 1);
103 }
104
105 static roundTo(number: number, scale: number): number {
106 const roundPower = Math.pow(10, scale);
107 return Math.round(number * roundPower) / roundPower;
108 }
109
110 static truncTo(number: number, scale: number): number {
111 const truncPower = Math.pow(10, scale);
112 return Math.trunc(number * truncPower) / truncPower;
113 }
114
115 static getRandomFloatRounded(max: number, min = 0, scale = 2): number {
116 if (min) {
117 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
118 }
119 return Utils.roundTo(Utils.getRandomFloat(max), scale);
120 }
121
122 static logPrefix(prefixString = ''): string {
123 const date = new Date();
124 return date.toLocaleString() + prefixString;
125 }
126
127 static cloneObject<T>(object: T): T {
128 return JSON.parse(JSON.stringify(object)) as T;
129 }
130
131 static isIterable(obj): boolean {
132 if (obj) {
133 return typeof obj[Symbol.iterator] === 'function';
134 }
135 return false;
136 }
137
138 static isEmptyJSon(document): boolean {
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
151 static isString(value): boolean {
152 return typeof value === 'string';
153 }
154
155 static isUndefined(value): boolean {
156 return typeof value === 'undefined';
157 }
158
159 static isNullOrUndefined(value): boolean {
160 // eslint-disable-next-line no-eq-null, eqeqeq
161 if (value == null) {
162 return true;
163 }
164 return false;
165 }
166
167 static isEmptyArray(object): boolean {
168 if (!object) {
169 return true;
170 }
171 if (Array.isArray(object) && object.length > 0) {
172 return false;
173 }
174 return true;
175 }
176
177 static isEmptyObject(obj): boolean {
178 return !Object.keys(obj).length;
179 }
180
181 static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
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 }
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])) {
208 return WebSocketCloseEventStatusString[code];
209 }
210 return '(Unknown)';
211 }
212 }