Fix remote transaction start.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
CommitLineData
2e3ac96d 1const {v4: uuid} = require('uuid');
7dde0b73
JB
2
3class Utils {
4 static generateGUID() {
2e3ac96d 5 return uuid();
7dde0b73
JB
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 convertToDate(date) {
19 // Check
20 if (!date) {
21 return date;
22 }
23 // Check Type
24 if (!(date instanceof Date)) {
25 return new Date(date);
26 }
27 return date;
28 }
29
30 static isIterable(obj) {
31 if (obj) {
32 return typeof obj[Symbol.iterator] === 'function';
33 }
34 return false;
35 }
36
37 static isEmptyJSon(document) {
38 // Empty?
39 if (!document) {
40 return true;
41 }
42 // Check type
43 if (typeof document !== 'object') {
44 return true;
45 }
46 // Check
47 return Object.keys(document).length === 0;
48 }
49
50 static removeExtraEmptyLines(tab) {
51 // Start from the end
52 for (let i = tab.length - 1; i > 0; i--) {
53 // Two consecutive empty lines?
54 if (tab[i].length === 0 && tab[i - 1].length === 0) {
55 // Remove the last one
56 tab.splice(i, 1);
57 }
58 // Check last line
59 if (i === 1 && tab[i - 1].length === 0) {
60 // Remove the first one
61 tab.splice(i - 1, 1);
62 }
63 }
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(id) {
78 let changedID = id;
79 if (!id) {
80 return 0;
81 }
82 // Check
83 if (typeof id === 'string') {
84 // Create Object
85 changedID = parseInt(id);
86 }
87 return changedID;
88 }
89
90 static convertToFloat(id) {
91 let changedID = id;
92 if (!id) {
93 return 0;
94 }
95 // Check
96 if (typeof id === 'string') {
97 // Create Object
98 changedID = parseFloat(id);
99 }
100 return changedID;
101 }
102
103 static getRandomInt(max, min) {
104 if (min) {
105 return Math.floor((Math.random() * (max - min)) + min);
106 }
107 return Math.floor((Math.random() * max));
108 }
109
110 static basicFormatLog(prefixString = '') {
111 const date = new Date();
112 return date.toISOString().substr(0, 19) + prefixString;
113 }
114}
115
116module.exports = Utils;