Make CircularArray class self-contained and an independant module.
[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
2328be1e
JB
120 static cloneObject(object) {
121 return JSON.parse(JSON.stringify(object));
2e6f5966 122 }
facd8ebd 123
6af9012e 124 static isIterable(obj): boolean {
67e9cccf
JB
125 if (obj) {
126 return typeof obj[Symbol.iterator] === 'function';
127 }
128 return false;
129 }
130
6af9012e 131 static isEmptyJSon(document): boolean {
67e9cccf
JB
132 // Empty?
133 if (!document) {
134 return true;
135 }
136 // Check type
137 if (typeof document !== 'object') {
138 return true;
139 }
140 // Check
141 return Object.keys(document).length === 0;
142 }
143
6af9012e 144 static isString(value): boolean {
560bcf5b
JB
145 return typeof value === 'string';
146 }
147
7ec46a9a 148 static isUndefined(value): boolean {
ead548f2
JB
149 return typeof value === 'undefined';
150 }
151
6af9012e
JB
152 static isNullOrUndefined(value): boolean {
153 // eslint-disable-next-line no-eq-null
ead548f2 154 if (value == null) {
facd8ebd
JB
155 return true;
156 }
157 return false;
158 }
4a56deef 159
6af9012e 160 static isEmptyArray(object): boolean {
fd1ee77c
JB
161 if (!object) {
162 return true;
163 }
4a56deef
JB
164 if (Array.isArray(object) && object.length > 0) {
165 return false;
166 }
167 return true;
168 }
7abfea5f 169
6af9012e 170 static isEmptyObject(obj): boolean {
7abfea5f
JB
171 return !Object.keys(obj).length;
172 }
7ec46a9a
JB
173
174 static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
032d6efc
JB
175
176 /**
177 * @param {number} [retryNumber=0]
178 * @return {number} - delay in milliseconds
179 */
180 static exponentialDelay(retryNumber = 0): number {
181 const delay = Math.pow(2, retryNumber) * 100;
182 const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay
183 return delay + randomSum;
184 }
7dde0b73 185}