| 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(milliSeconds: number): Promise<NodeJS.Timeout> { |
| 9 | return new Promise((resolve) => setTimeout(resolve, milliSeconds)); |
| 10 | } |
| 11 | |
| 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); |
| 18 | } |
| 19 | |
| 20 | static removeExtraEmptyLines(tab: string[]): void { |
| 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 | |
| 36 | static convertToDate(value): Date { |
| 37 | // Check |
| 38 | if (!value) { |
| 39 | return value; |
| 40 | } |
| 41 | // Check Type |
| 42 | if (!(value instanceof Date)) { |
| 43 | return new Date(value); |
| 44 | } |
| 45 | return value; |
| 46 | } |
| 47 | |
| 48 | static convertToInt(value): number { |
| 49 | let changedValue = value; |
| 50 | if (!value) { |
| 51 | return 0; |
| 52 | } |
| 53 | if (Number.isSafeInteger(value)) { |
| 54 | return value; |
| 55 | } |
| 56 | // Check |
| 57 | if (typeof value === 'string') { |
| 58 | // Create Object |
| 59 | changedValue = parseInt(value); |
| 60 | } |
| 61 | return changedValue; |
| 62 | } |
| 63 | |
| 64 | static convertToFloat(value): number { |
| 65 | let changedValue = value; |
| 66 | if (!value) { |
| 67 | return 0; |
| 68 | } |
| 69 | // Check |
| 70 | if (typeof value === 'string') { |
| 71 | // Create Object |
| 72 | changedValue = parseFloat(value); |
| 73 | } |
| 74 | return changedValue; |
| 75 | } |
| 76 | |
| 77 | static convertToBoolean(value): boolean { |
| 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 | |
| 93 | static getRandomFloat(max: number, min = 0): number { |
| 94 | return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min; |
| 95 | } |
| 96 | |
| 97 | static getRandomInt(max: number, min = 0): number { |
| 98 | if (min) { |
| 99 | return Math.floor(Math.random() * (max - min + 1) + min); |
| 100 | } |
| 101 | return Math.floor(Math.random() * max + 1); |
| 102 | } |
| 103 | |
| 104 | static roundTo(number: number, scale: number): number { |
| 105 | const roundPower = Math.pow(10, scale); |
| 106 | return Math.round(number * roundPower) / roundPower; |
| 107 | } |
| 108 | |
| 109 | static truncTo(number: number, scale: number): number { |
| 110 | const truncPower = Math.pow(10, scale); |
| 111 | return Math.trunc(number * truncPower) / truncPower; |
| 112 | } |
| 113 | |
| 114 | static getRandomFloatRounded(max: number, min = 0, scale = 2): number { |
| 115 | if (min) { |
| 116 | return Utils.roundTo(Utils.getRandomFloat(max, min), scale); |
| 117 | } |
| 118 | return Utils.roundTo(Utils.getRandomFloat(max), scale); |
| 119 | } |
| 120 | |
| 121 | static logPrefix(prefixString = ''): string { |
| 122 | const date = new Date(); |
| 123 | return date.toLocaleString() + prefixString; |
| 124 | } |
| 125 | |
| 126 | static cloneObject<T>(object: T): T { |
| 127 | return JSON.parse(JSON.stringify(object)) as T; |
| 128 | } |
| 129 | |
| 130 | static isIterable(obj): boolean { |
| 131 | if (obj) { |
| 132 | return typeof obj[Symbol.iterator] === 'function'; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | static isEmptyJSon(document): boolean { |
| 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 | |
| 150 | static isString(value): boolean { |
| 151 | return typeof value === 'string'; |
| 152 | } |
| 153 | |
| 154 | static isUndefined(value): boolean { |
| 155 | return typeof value === 'undefined'; |
| 156 | } |
| 157 | |
| 158 | static isNullOrUndefined(value): boolean { |
| 159 | // eslint-disable-next-line no-eq-null |
| 160 | if (value == null) { |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | static isEmptyArray(object): boolean { |
| 167 | if (!object) { |
| 168 | return true; |
| 169 | } |
| 170 | if (Array.isArray(object) && object.length > 0) { |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | static isEmptyObject(obj): boolean { |
| 177 | return !Object.keys(obj).length; |
| 178 | } |
| 179 | |
| 180 | static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; |
| 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 | } |
| 191 | } |