Initial portage to TypeScript.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
CommitLineData
6af9012e 1import { v4 as uuid } from 'uuid';
7dde0b73 2
3f40bc9c 3export default class Utils {
6af9012e 4 static generateUUID(): string {
2e3ac96d 5 return uuid();
7dde0b73
JB
6 }
7
6af9012e 8 static async sleep(ms: number): Promise<NodeJS.Timeout> {
7dde0b73
JB
9 return new Promise((resolve) => setTimeout(resolve, ms));
10 }
11
6af9012e
JB
12 static secondstoHHMMSS(seconds): string {
13 const date = new Date();
7dde0b73
JB
14 date.setSeconds(seconds);
15 return date.toISOString().substr(11, 8);
16 }
17
6af9012e 18 static removeExtraEmptyLines(tab): void {
7dde0b73
JB
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
6af9012e 34 static convertToDate(value): Date {
560bcf5b 35 // Check
6af9012e
JB
36 if (!value) {
37 return value;
560bcf5b
JB
38 }
39 // Check Type
6af9012e
JB
40 if (!(value instanceof Date)) {
41 return new Date(value);
7dde0b73 42 }
6af9012e 43 return value;
7dde0b73
JB
44 }
45
6af9012e 46 static convertToInt(value): number {
72766a82
JB
47 let changedValue = value;
48 if (!value) {
7dde0b73
JB
49 return 0;
50 }
72766a82
JB
51 if (Number.isSafeInteger(value)) {
52 return value;
53 }
7dde0b73 54 // Check
72766a82 55 if (typeof value === 'string') {
7dde0b73 56 // Create Object
72766a82 57 changedValue = parseInt(value);
7dde0b73 58 }
72766a82 59 return changedValue;
7dde0b73
JB
60 }
61
6af9012e 62 static convertToFloat(value): number {
72766a82
JB
63 let changedValue = value;
64 if (!value) {
7dde0b73
JB
65 return 0;
66 }
67 // Check
72766a82 68 if (typeof value === 'string') {
7dde0b73 69 // Create Object
72766a82 70 changedValue = parseFloat(value);
7dde0b73 71 }
72766a82 72 return changedValue;
7dde0b73
JB
73 }
74
6af9012e 75 static convertToBoolean(value): boolean {
a6e68f34
JB
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
6af9012e 91 static getRandomFloat(max: number, min = 0): number {
fee83021 92 return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min;
560bcf5b
JB
93 }
94
6af9012e 95 static getRandomInt(max: number, min = 0): number {
7dde0b73 96 if (min) {
fee83021 97 return Math.floor(Math.random() * (max - min + 1) + min);
7dde0b73 98 }
fee83021 99 return Math.floor(Math.random() * max + 1);
560bcf5b
JB
100 }
101
6af9012e 102 static roundTo(number: number, scale: number): number {
560bcf5b
JB
103 return Utils.convertToFloat(number.toFixed(scale));
104 }
105
6af9012e 106 static getRandomFloatRounded(max: number, min = 0, scale = 2): number {
560bcf5b
JB
107 if (min) {
108 return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
109 }
110 return Utils.roundTo(Utils.getRandomFloat(max), scale);
7dde0b73
JB
111 }
112
6af9012e 113 static logPrefix(prefixString = ''): string {
7dde0b73 114 const date = new Date();
4455e614 115 return date.toLocaleString() + prefixString;
7dde0b73 116 }
1d7ca20c
JB
117
118 static objectHasOwnProperty(object, property) {
119 return Object.prototype.hasOwnProperty.call(object, property);
120 }
2e6f5966 121
2328be1e
JB
122 static cloneObject(object) {
123 return JSON.parse(JSON.stringify(object));
2e6f5966 124 }
facd8ebd 125
6af9012e 126 static isIterable(obj): boolean {
67e9cccf
JB
127 if (obj) {
128 return typeof obj[Symbol.iterator] === 'function';
129 }
130 return false;
131 }
132
6af9012e 133 static isEmptyJSon(document): boolean {
67e9cccf
JB
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
6af9012e 146 static isString(value): boolean {
560bcf5b
JB
147 return typeof value === 'string';
148 }
149
facd8ebd 150 static isUndefined(value) {
ead548f2
JB
151 return typeof value === 'undefined';
152 }
153
6af9012e
JB
154 static isNullOrUndefined(value): boolean {
155 // eslint-disable-next-line no-eq-null
ead548f2 156 if (value == null) {
facd8ebd
JB
157 return true;
158 }
159 return false;
160 }
4a56deef 161
6af9012e 162 static isEmptyArray(object): boolean {
4a56deef
JB
163 if (Array.isArray(object) && object.length > 0) {
164 return false;
165 }
166 return true;
167 }
7abfea5f 168
6af9012e 169 static isEmptyObject(obj): boolean {
7abfea5f
JB
170 return !Object.keys(obj).length;
171 }
7dde0b73 172}