| 1 | const {v4: uuid} = require('uuid'); |
| 2 | |
| 3 | class Utils { |
| 4 | static generateGUID() { |
| 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 convertToDate(date) { |
| 19 | // Check |
| 20 | if (!date) { |
| 21 | return date; |
| 22 | } |
| 23 | // Check Type |
| 24 | if (!(date instanceof Date)) { |
| 25 | return new Date(date); |
| 26 | } |
| 27 | return date; |
| 28 | } |
| 29 | |
| 30 | static isIterable(obj) { |
| 31 | if (obj) { |
| 32 | return typeof obj[Symbol.iterator] === 'function'; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | static isEmptyJSon(document) { |
| 38 | // Empty? |
| 39 | if (!document) { |
| 40 | return true; |
| 41 | } |
| 42 | // Check type |
| 43 | if (typeof document !== 'object') { |
| 44 | return true; |
| 45 | } |
| 46 | // Check |
| 47 | return Object.keys(document).length === 0; |
| 48 | } |
| 49 | |
| 50 | static removeExtraEmptyLines(tab) { |
| 51 | // Start from the end |
| 52 | for (let i = tab.length - 1; i > 0; i--) { |
| 53 | // Two consecutive empty lines? |
| 54 | if (tab[i].length === 0 && tab[i - 1].length === 0) { |
| 55 | // Remove the last one |
| 56 | tab.splice(i, 1); |
| 57 | } |
| 58 | // Check last line |
| 59 | if (i === 1 && tab[i - 1].length === 0) { |
| 60 | // Remove the first one |
| 61 | tab.splice(i - 1, 1); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static convertToObjectID(id) { |
| 67 | let changedID = id; |
| 68 | // Check |
| 69 | if (typeof id === 'string') { |
| 70 | // Create Object |
| 71 | // eslint-disable-next-line no-undef |
| 72 | changedID = new ObjectID(id); |
| 73 | } |
| 74 | return changedID; |
| 75 | } |
| 76 | |
| 77 | static convertToInt(id) { |
| 78 | let changedID = id; |
| 79 | if (!id) { |
| 80 | return 0; |
| 81 | } |
| 82 | // Check |
| 83 | if (typeof id === 'string') { |
| 84 | // Create Object |
| 85 | changedID = parseInt(id); |
| 86 | } |
| 87 | return changedID; |
| 88 | } |
| 89 | |
| 90 | static convertToFloat(id) { |
| 91 | let changedID = id; |
| 92 | if (!id) { |
| 93 | return 0; |
| 94 | } |
| 95 | // Check |
| 96 | if (typeof id === 'string') { |
| 97 | // Create Object |
| 98 | changedID = parseFloat(id); |
| 99 | } |
| 100 | return changedID; |
| 101 | } |
| 102 | |
| 103 | static convertToBoolean(value) { |
| 104 | let result = false; |
| 105 | // Check boolean |
| 106 | if (value) { |
| 107 | // Check the type |
| 108 | if (typeof value === 'boolean') { |
| 109 | // Already a boolean |
| 110 | result = value; |
| 111 | } else { |
| 112 | // Convert |
| 113 | result = (value === 'true'); |
| 114 | } |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | static getRandomInt(max, min) { |
| 120 | if (min) { |
| 121 | return Math.floor((Math.random() * (max - min)) + min); |
| 122 | } |
| 123 | return Math.floor((Math.random() * max)); |
| 124 | } |
| 125 | |
| 126 | static basicFormatLog(prefixString = '') { |
| 127 | const date = new Date(); |
| 128 | return date.toISOString().substr(0, 19) + prefixString; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | module.exports = Utils; |