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