Simplify DC current output type handling.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
1 import {v4 as uuid} from 'uuid';
2
3 export default 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 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
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
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
57 static convertToInt(value) {
58 let changedValue = value;
59 if (!value) {
60 return 0;
61 }
62 if (Number.isSafeInteger(value)) {
63 return value;
64 }
65 // Check
66 if (typeof value === 'string') {
67 // Create Object
68 changedValue = parseInt(value);
69 }
70 return changedValue;
71 }
72
73 static convertToFloat(value) {
74 let changedValue = value;
75 if (!value) {
76 return 0;
77 }
78 // Check
79 if (typeof value === 'string') {
80 // Create Object
81 changedValue = parseFloat(value);
82 }
83 return changedValue;
84 }
85
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
102 static getRandomFloat(max, min = 0) {
103 return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min;
104 }
105
106 static getRandomInt(max, min = 0) {
107 if (min) {
108 return Math.floor(Math.random() * (max - min + 1) + min);
109 }
110 return Math.floor(Math.random() * max + 1);
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);
122 }
123
124 static logPrefix(prefixString = '') {
125 const date = new Date();
126 return date.toLocaleString() + prefixString;
127 }
128
129 static objectHasOwnProperty(object, property) {
130 return Object.prototype.hasOwnProperty.call(object, property);
131 }
132
133 static cloneObject(object) {
134 return JSON.parse(JSON.stringify(object));
135 }
136
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
157 static isString(value) {
158 return typeof value === 'string';
159 }
160
161 static isUndefined(value) {
162 return typeof value === 'undefined';
163 }
164
165 static isNullOrUndefined(value) {
166 // eslint-disable-next-line eqeqeq
167 if (value == null) {
168 return true;
169 }
170 return false;
171 }
172
173 static isEmptyArray(object) {
174 if (Array.isArray(object) && object.length > 0) {
175 return false;
176 }
177 return true;
178 }
179
180 static isEmptyObject(obj) {
181 return !Object.keys(obj).length;
182 }
183 }