Rename index.js to start.js
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
CommitLineData
2e3ac96d 1const {v4: uuid} = require('uuid');
7dde0b73
JB
2
3class Utils {
027b409a 4 static generateUUID() {
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
72766a82
JB
77 static convertToInt(value) {
78 let changedValue = value;
79 if (!value) {
7dde0b73
JB
80 return 0;
81 }
72766a82
JB
82 if (Number.isSafeInteger(value)) {
83 return value;
84 }
7dde0b73 85 // Check
72766a82 86 if (typeof value === 'string') {
7dde0b73 87 // Create Object
72766a82 88 changedValue = parseInt(value);
7dde0b73 89 }
72766a82 90 return changedValue;
7dde0b73
JB
91 }
92
72766a82
JB
93 static convertToFloat(value) {
94 let changedValue = value;
95 if (!value) {
7dde0b73
JB
96 return 0;
97 }
98 // Check
72766a82 99 if (typeof value === 'string') {
7dde0b73 100 // Create Object
72766a82 101 changedValue = parseFloat(value);
7dde0b73 102 }
72766a82 103 return changedValue;
7dde0b73
JB
104 }
105
a6e68f34
JB
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
7dde0b73
JB
122 static getRandomInt(max, min) {
123 if (min) {
61c2e33d 124 return Math.floor((Math.random() * (max - min + 1)) + min);
7dde0b73 125 }
61c2e33d 126 return Math.floor((Math.random() * max + 1));
7dde0b73
JB
127 }
128
ead548f2 129 static logPrefix(prefixString = '') {
7dde0b73 130 const date = new Date();
4455e614 131 return date.toLocaleString() + prefixString;
7dde0b73 132 }
1d7ca20c
JB
133
134 static objectHasOwnProperty(object, property) {
135 return Object.prototype.hasOwnProperty.call(object, property);
136 }
2e6f5966
JB
137
138 static cloneJSonDocument(jsonDocument) {
139 return JSON.parse(JSON.stringify(jsonDocument));
140 }
facd8ebd
JB
141
142 static isUndefined(value) {
ead548f2
JB
143 return typeof value === 'undefined';
144 }
145
146 static isNullOrUndefined(value) {
147 // eslint-disable-next-line eqeqeq
148 if (value == null) {
facd8ebd
JB
149 return true;
150 }
151 return false;
152 }
4a56deef
JB
153
154 static isEmptyArray(object) {
155 if (Array.isArray(object) && object.length > 0) {
156 return false;
157 }
158 return true;
159 }
7dde0b73
JB
160}
161
162module.exports = Utils;