Commit | Line | Data |
---|---|---|
6af9012e | 1 | import { v4 as uuid } from 'uuid'; |
7dde0b73 | 2 | |
3f40bc9c | 3 | export default class Utils { |
6af9012e | 4 | static generateUUID(): string { |
2e3ac96d | 5 | return uuid(); |
7dde0b73 JB |
6 | } |
7 | ||
9ac86a7e JB |
8 | static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> { |
9 | return new Promise((resolve) => setTimeout(resolve, milliSeconds)); | |
7dde0b73 JB |
10 | } |
11 | ||
9ac86a7e JB |
12 | static secondsToHHMMSS(seconds: number): string { |
13 | return new Date(seconds * 1000).toISOString().substr(11, 8); | |
14 | } | |
15 | ||
16 | static milliSecondsToHHMMSS(milliSeconds: number): string { | |
17 | return new Date(milliSeconds).toISOString().substr(11, 8); | |
7dde0b73 JB |
18 | } |
19 | ||
ca64a820 | 20 | static removeExtraEmptyLines(tab: string[]): void { |
7dde0b73 JB |
21 | // Start from the end |
22 | for (let i = tab.length - 1; i > 0; i--) { | |
23 | // Two consecutive empty lines? | |
24 | if (tab[i].length === 0 && tab[i - 1].length === 0) { | |
25 | // Remove the last one | |
26 | tab.splice(i, 1); | |
27 | } | |
28 | // Check last line | |
29 | if (i === 1 && tab[i - 1].length === 0) { | |
30 | // Remove the first one | |
31 | tab.splice(i - 1, 1); | |
32 | } | |
33 | } | |
34 | } | |
35 | ||
6af9012e | 36 | static convertToDate(value): Date { |
560bcf5b | 37 | // Check |
6af9012e JB |
38 | if (!value) { |
39 | return value; | |
560bcf5b JB |
40 | } |
41 | // Check Type | |
6af9012e JB |
42 | if (!(value instanceof Date)) { |
43 | return new Date(value); | |
7dde0b73 | 44 | } |
6af9012e | 45 | return value; |
7dde0b73 JB |
46 | } |
47 | ||
6af9012e | 48 | static convertToInt(value): number { |
72766a82 JB |
49 | let changedValue = value; |
50 | if (!value) { | |
7dde0b73 JB |
51 | return 0; |
52 | } | |
72766a82 JB |
53 | if (Number.isSafeInteger(value)) { |
54 | return value; | |
55 | } | |
7dde0b73 | 56 | // Check |
72766a82 | 57 | if (typeof value === 'string') { |
7dde0b73 | 58 | // Create Object |
72766a82 | 59 | changedValue = parseInt(value); |
7dde0b73 | 60 | } |
72766a82 | 61 | return changedValue; |
7dde0b73 JB |
62 | } |
63 | ||
6af9012e | 64 | static convertToFloat(value): number { |
72766a82 JB |
65 | let changedValue = value; |
66 | if (!value) { | |
7dde0b73 JB |
67 | return 0; |
68 | } | |
69 | // Check | |
72766a82 | 70 | if (typeof value === 'string') { |
7dde0b73 | 71 | // Create Object |
72766a82 | 72 | changedValue = parseFloat(value); |
7dde0b73 | 73 | } |
72766a82 | 74 | return changedValue; |
7dde0b73 JB |
75 | } |
76 | ||
6af9012e | 77 | static convertToBoolean(value): boolean { |
a6e68f34 JB |
78 | let result = false; |
79 | // Check boolean | |
80 | if (value) { | |
81 | // Check the type | |
82 | if (typeof value === 'boolean') { | |
83 | // Already a boolean | |
84 | result = value; | |
85 | } else { | |
86 | // Convert | |
87 | result = (value === 'true'); | |
88 | } | |
89 | } | |
90 | return result; | |
91 | } | |
92 | ||
6af9012e | 93 | static getRandomFloat(max: number, min = 0): number { |
fee83021 | 94 | return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min; |
560bcf5b JB |
95 | } |
96 | ||
6af9012e | 97 | static getRandomInt(max: number, min = 0): number { |
7dde0b73 | 98 | if (min) { |
fee83021 | 99 | return Math.floor(Math.random() * (max - min + 1) + min); |
7dde0b73 | 100 | } |
fee83021 | 101 | return Math.floor(Math.random() * max + 1); |
560bcf5b JB |
102 | } |
103 | ||
6af9012e | 104 | static roundTo(number: number, scale: number): number { |
ad3de6c4 JB |
105 | const roundPower = Math.pow(10, scale); |
106 | return Math.round(number * roundPower) / roundPower; | |
560bcf5b JB |
107 | } |
108 | ||
6d3a11a0 JB |
109 | static truncTo(number: number, scale: number): number { |
110 | const truncPower = Math.pow(10, scale); | |
111 | return Math.trunc(number * truncPower) / truncPower; | |
112 | } | |
113 | ||
6af9012e | 114 | static getRandomFloatRounded(max: number, min = 0, scale = 2): number { |
560bcf5b JB |
115 | if (min) { |
116 | return Utils.roundTo(Utils.getRandomFloat(max, min), scale); | |
117 | } | |
118 | return Utils.roundTo(Utils.getRandomFloat(max), scale); | |
7dde0b73 JB |
119 | } |
120 | ||
6af9012e | 121 | static logPrefix(prefixString = ''): string { |
7dde0b73 | 122 | const date = new Date(); |
4455e614 | 123 | return date.toLocaleString() + prefixString; |
7dde0b73 | 124 | } |
1d7ca20c | 125 | |
e56aa9a4 JB |
126 | static cloneObject<T>(object: T): T { |
127 | return JSON.parse(JSON.stringify(object)) as T; | |
2e6f5966 | 128 | } |
facd8ebd | 129 | |
6af9012e | 130 | static isIterable(obj): boolean { |
67e9cccf JB |
131 | if (obj) { |
132 | return typeof obj[Symbol.iterator] === 'function'; | |
133 | } | |
134 | return false; | |
135 | } | |
136 | ||
6af9012e | 137 | static isEmptyJSon(document): boolean { |
67e9cccf JB |
138 | // Empty? |
139 | if (!document) { | |
140 | return true; | |
141 | } | |
142 | // Check type | |
143 | if (typeof document !== 'object') { | |
144 | return true; | |
145 | } | |
146 | // Check | |
147 | return Object.keys(document).length === 0; | |
148 | } | |
149 | ||
6af9012e | 150 | static isString(value): boolean { |
560bcf5b JB |
151 | return typeof value === 'string'; |
152 | } | |
153 | ||
7ec46a9a | 154 | static isUndefined(value): boolean { |
ead548f2 JB |
155 | return typeof value === 'undefined'; |
156 | } | |
157 | ||
6af9012e JB |
158 | static isNullOrUndefined(value): boolean { |
159 | // eslint-disable-next-line no-eq-null | |
ead548f2 | 160 | if (value == null) { |
facd8ebd JB |
161 | return true; |
162 | } | |
163 | return false; | |
164 | } | |
4a56deef | 165 | |
6af9012e | 166 | static isEmptyArray(object): boolean { |
fd1ee77c JB |
167 | if (!object) { |
168 | return true; | |
169 | } | |
4a56deef JB |
170 | if (Array.isArray(object) && object.length > 0) { |
171 | return false; | |
172 | } | |
173 | return true; | |
174 | } | |
7abfea5f | 175 | |
6af9012e | 176 | static isEmptyObject(obj): boolean { |
7abfea5f JB |
177 | return !Object.keys(obj).length; |
178 | } | |
7ec46a9a JB |
179 | |
180 | static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; | |
032d6efc JB |
181 | |
182 | /** | |
183 | * @param {number} [retryNumber=0] | |
184 | * @return {number} - delay in milliseconds | |
185 | */ | |
186 | static exponentialDelay(retryNumber = 0): number { | |
187 | const delay = Math.pow(2, retryNumber) * 100; | |
188 | const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay | |
189 | return delay + randomSum; | |
190 | } | |
7dde0b73 | 191 | } |