| 1 | const {v4: uuid} = require('uuid'); |
| 2 | |
| 3 | class Utils { |
| 4 | static generateUUID() { |
| 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(value) { |
| 78 | let changedValue = value; |
| 79 | if (!value) { |
| 80 | return 0; |
| 81 | } |
| 82 | if (Number.isSafeInteger(value)) { |
| 83 | return value; |
| 84 | } |
| 85 | // Check |
| 86 | if (typeof value === 'string') { |
| 87 | // Create Object |
| 88 | changedValue = parseInt(value); |
| 89 | } |
| 90 | return changedValue; |
| 91 | } |
| 92 | |
| 93 | static convertToFloat(value) { |
| 94 | let changedValue = value; |
| 95 | if (!value) { |
| 96 | return 0; |
| 97 | } |
| 98 | // Check |
| 99 | if (typeof value === 'string') { |
| 100 | // Create Object |
| 101 | changedValue = parseFloat(value); |
| 102 | } |
| 103 | return changedValue; |
| 104 | } |
| 105 | |
| 106 | static convertToBoolean(value) { |
| 107 | let result = false; |
| 108 | // Check boolean |
| 109 | if (value) { |
| 110 | // Check the type |
| 111 | if (typeof value === 'boolean') { |
| 112 | // Already a boolean |
| 113 | result = value; |
| 114 | } else { |
| 115 | // Convert |
| 116 | result = (value === 'true'); |
| 117 | } |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | static getRandomInt(max, min) { |
| 123 | if (min) { |
| 124 | return Math.floor((Math.random() * (max - min)) + min); |
| 125 | } |
| 126 | return Math.floor((Math.random() * max)); |
| 127 | } |
| 128 | |
| 129 | static basicFormatLog(prefixString = '') { |
| 130 | const date = new Date(); |
| 131 | return date.toISOString().substr(0, 19) + prefixString; |
| 132 | } |
| 133 | |
| 134 | static objectHasOwnProperty(object, property) { |
| 135 | return Object.prototype.hasOwnProperty.call(object, property); |
| 136 | } |
| 137 | |
| 138 | static cloneJSonDocument(jsonDocument) { |
| 139 | return JSON.parse(JSON.stringify(jsonDocument)); |
| 140 | } |
| 141 | |
| 142 | static isUndefined(value) { |
| 143 | if (typeof value === 'undefined') { |
| 144 | return true; |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | module.exports = Utils; |