Simplify DC current output type handling.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
1 import { v4 as uuid } from 'uuid';
2
3 export default class Utils {
4 static generateUUID(): string {
5 return uuid();
6 }
7
8 static async sleep(ms: number): Promise<NodeJS.Timeout> {
9 return new Promise((resolve) => setTimeout(resolve, ms));
10 }
11
12 static secondstoHHMMSS(seconds): string {
13 const date = new Date();
14 date.setSeconds(seconds);
15 return date.toISOString().substr(11, 8);
16 }
17
18 static removeExtraEmptyLines(tab): void {
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(value): Date {
35 // Check
36 if (!value) {
37 return value;
38 }
39 // Check Type
40 if (!(value instanceof Date)) {
41 return new Date(value);
42 }
43 return value;
44 }
45
46 static convertToInt(value): number {
47 let changedValue = value;
48 if (!value) {
49 return 0;
50 }
51 if (Number.isSafeInteger(value)) {
52 return value;
53 }
54 // Check
55 if (typeof value === 'string') {
56 // Create Object
57 changedValue = parseInt(value);
58 }
59 return changedValue;
60 }
61
62 static convertToFloat(value): number {
63 let changedValue = value;
64 if (!value) {
65 return 0;
66 }
67 // Check
68 if (typeof value === 'string') {
69 // Create Object
70 changedValue = parseFloat(value);
71 }
72 return changedValue;
73 }
74
75 static convertToBoolean(value): boolean {
76 let result = false;
77 // Check boolean
78 if (value) {
79 // Check the type
80 if (typeof value === 'boolean') {
81 // Already a boolean
82 result = value;
83 } else {
84 // Convert
85 result = (value === 'true');
86 }
87 }
88 return result;
89 }
90
91 static getRandomFloat(max: number, min = 0): number {
92 return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min;
93 }
94
95 static getRandomInt(max: number, min = 0): number {
96 if (min) {
97 return Math.floor(Math.random() * (max - min + 1) + min);
98 }
99 return Math.floor(Math.random() * max + 1);
100 }
101
102 static roundTo(number: number, scale: number): number {
103 return Utils.convertToFloat(number.toFixed(scale));
104 }
105
106 static getRandomFloatRounded(max: number, min = 0, scale = 2): number {
107 if (min) {
108 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
109 }
110 return Utils.roundTo(Utils.getRandomFloat(max), scale);
111 }
112
113 static logPrefix(prefixString = ''): string {
114 const date = new Date();
115 return date.toLocaleString() + prefixString;
116 }
117
118 static objectHasOwnProperty(object, property) {
119 return Object.prototype.hasOwnProperty.call(object, property);
120 }
121
122 static cloneObject(object) {
123 return JSON.parse(JSON.stringify(object));
124 }
125
126 static isIterable(obj): boolean {
127 if (obj) {
128 return typeof obj[Symbol.iterator] === 'function';
129 }
130 return false;
131 }
132
133 static isEmptyJSon(document): boolean {
134 // Empty?
135 if (!document) {
136 return true;
137 }
138 // Check type
139 if (typeof document !== 'object') {
140 return true;
141 }
142 // Check
143 return Object.keys(document).length === 0;
144 }
145
146 static isString(value): boolean {
147 return typeof value === 'string';
148 }
149
150 static isUndefined(value) {
151 return typeof value === 'undefined';
152 }
153
154 static isNullOrUndefined(value): boolean {
155 // eslint-disable-next-line no-eq-null
156 if (value == null) {
157 return true;
158 }
159 return false;
160 }
161
162 static isEmptyArray(object): boolean {
163 if (Array.isArray(object) && object.length > 0) {
164 return false;
165 }
166 return true;
167 }
168
169 static isEmptyObject(obj): boolean {
170 return !Object.keys(obj).length;
171 }
172 }