Make statistics class a singleton.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
1 const {v4: uuid} = require('uuid');
2
3 class Utils {
4 static generateUUID() {
5 return uuid();
6 }
7
8 static sleep(ms) {
9 return new Promise((resolve) => setTimeout(resolve, ms));
10 }
11
12 static secondstoHHMMSS(seconds) {
13 const date = new Date(null);
14 date.setSeconds(seconds);
15 return date.toISOString().substr(11, 8);
16 }
17
18 static isIterable(obj) {
19 if (obj) {
20 return typeof obj[Symbol.iterator] === 'function';
21 }
22 return false;
23 }
24
25 static isEmptyJSon(document) {
26 // Empty?
27 if (!document) {
28 return true;
29 }
30 // Check type
31 if (typeof document !== 'object') {
32 return true;
33 }
34 // Check
35 return Object.keys(document).length === 0;
36 }
37
38 static removeExtraEmptyLines(tab) {
39 // Start from the end
40 for (let i = tab.length - 1; i > 0; i--) {
41 // Two consecutive empty lines?
42 if (tab[i].length === 0 && tab[i - 1].length === 0) {
43 // Remove the last one
44 tab.splice(i, 1);
45 }
46 // Check last line
47 if (i === 1 && tab[i - 1].length === 0) {
48 // Remove the first one
49 tab.splice(i - 1, 1);
50 }
51 }
52 }
53
54 static convertToDate(date) {
55 // Check
56 if (!date) {
57 return date;
58 }
59 // Check Type
60 if (!(date instanceof Date)) {
61 return new Date(date);
62 }
63 return date;
64 }
65
66 static convertToObjectID(id) {
67 let changedID = id;
68 // Check
69 if (typeof id === 'string') {
70 // Create Object
71 // eslint-disable-next-line no-undef
72 changedID = new ObjectID(id);
73 }
74 return changedID;
75 }
76
77 static convertToInt(value) {
78 let changedValue = value;
79 if (!value) {
80 return 0;
81 }
82 if (Number.isSafeInteger(value)) {
83 return value;
84 }
85 // Check
86 if (typeof value === 'string') {
87 // Create Object
88 changedValue = parseInt(value);
89 }
90 return changedValue;
91 }
92
93 static convertToFloat(value) {
94 let changedValue = value;
95 if (!value) {
96 return 0;
97 }
98 // Check
99 if (typeof value === 'string') {
100 // Create Object
101 changedValue = parseFloat(value);
102 }
103 return changedValue;
104 }
105
106 static convertToBoolean(value) {
107 let result = false;
108 // Check boolean
109 if (value) {
110 // Check the type
111 if (typeof value === 'boolean') {
112 // Already a boolean
113 result = value;
114 } else {
115 // Convert
116 result = (value === 'true');
117 }
118 }
119 return result;
120 }
121
122 static getRandomFloat(max, min = 0) {
123 if (min) {
124 return Math.random() * (max - min + 1) + min;
125 }
126 return Math.random() * max + 1;
127 }
128
129 static getRandomInt(max, min = 0) {
130 if (min) {
131 return Math.floor(Utils.getRandomFloat(max, min));
132 }
133 return Math.floor(Utils.getRandomFloat(max));
134 }
135
136 static roundTo(number, scale) {
137 return Utils.convertToFloat(number.toFixed(scale));
138 }
139
140 static getRandomFloatRounded(max, min = 0, scale = 2) {
141 if (min) {
142 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
143 }
144 return Utils.roundTo(Utils.getRandomFloat(max), scale);
145 }
146
147 static logPrefix(prefixString = '') {
148 const date = new Date();
149 return date.toLocaleString() + prefixString;
150 }
151
152 static objectHasOwnProperty(object, property) {
153 return Object.prototype.hasOwnProperty.call(object, property);
154 }
155
156 static cloneJSonDocument(jsonDocument) {
157 return JSON.parse(JSON.stringify(jsonDocument));
158 }
159
160 static isString(value) {
161 return typeof value === 'string';
162 }
163
164 static isUndefined(value) {
165 return typeof value === 'undefined';
166 }
167
168 static isNullOrUndefined(value) {
169 // eslint-disable-next-line eqeqeq
170 if (value == null) {
171 return true;
172 }
173 return false;
174 }
175
176 static isEmptyArray(object) {
177 if (Array.isArray(object) && object.length > 0) {
178 return false;
179 }
180 return true;
181 }
182 }
183
184 module.exports = Utils;