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