Commit | Line | Data |
---|---|---|
2e3ac96d | 1 | const {v4: uuid} = require('uuid'); |
7dde0b73 JB |
2 | |
3 | class Utils { | |
027b409a | 4 | static generateUUID() { |
2e3ac96d | 5 | return uuid(); |
7dde0b73 JB |
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 | ||
7dde0b73 JB |
18 | static removeExtraEmptyLines(tab) { |
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 | ||
560bcf5b JB |
34 | static convertToDate(date) { |
35 | // Check | |
36 | if (!date) { | |
37 | return date; | |
38 | } | |
39 | // Check Type | |
40 | if (!(date instanceof Date)) { | |
41 | return new Date(date); | |
42 | } | |
43 | return date; | |
44 | } | |
45 | ||
7dde0b73 JB |
46 | static convertToObjectID(id) { |
47 | let changedID = id; | |
48 | // Check | |
49 | if (typeof id === 'string') { | |
50 | // Create Object | |
51 | // eslint-disable-next-line no-undef | |
52 | changedID = new ObjectID(id); | |
53 | } | |
54 | return changedID; | |
55 | } | |
56 | ||
72766a82 JB |
57 | static convertToInt(value) { |
58 | let changedValue = value; | |
59 | if (!value) { | |
7dde0b73 JB |
60 | return 0; |
61 | } | |
72766a82 JB |
62 | if (Number.isSafeInteger(value)) { |
63 | return value; | |
64 | } | |
7dde0b73 | 65 | // Check |
72766a82 | 66 | if (typeof value === 'string') { |
7dde0b73 | 67 | // Create Object |
72766a82 | 68 | changedValue = parseInt(value); |
7dde0b73 | 69 | } |
72766a82 | 70 | return changedValue; |
7dde0b73 JB |
71 | } |
72 | ||
72766a82 JB |
73 | static convertToFloat(value) { |
74 | let changedValue = value; | |
75 | if (!value) { | |
7dde0b73 JB |
76 | return 0; |
77 | } | |
78 | // Check | |
72766a82 | 79 | if (typeof value === 'string') { |
7dde0b73 | 80 | // Create Object |
72766a82 | 81 | changedValue = parseFloat(value); |
7dde0b73 | 82 | } |
72766a82 | 83 | return changedValue; |
7dde0b73 JB |
84 | } |
85 | ||
a6e68f34 JB |
86 | static convertToBoolean(value) { |
87 | let result = false; | |
88 | // Check boolean | |
89 | if (value) { | |
90 | // Check the type | |
91 | if (typeof value === 'boolean') { | |
92 | // Already a boolean | |
93 | result = value; | |
94 | } else { | |
95 | // Convert | |
96 | result = (value === 'true'); | |
97 | } | |
98 | } | |
99 | return result; | |
100 | } | |
101 | ||
560bcf5b JB |
102 | static getRandomFloat(max, min = 0) { |
103 | if (min) { | |
104 | return Math.random() * (max - min + 1) + min; | |
105 | } | |
106 | return Math.random() * max + 1; | |
107 | } | |
108 | ||
109 | static getRandomInt(max, min = 0) { | |
7dde0b73 | 110 | if (min) { |
560bcf5b | 111 | return Math.floor(Utils.getRandomFloat(max, min)); |
7dde0b73 | 112 | } |
560bcf5b JB |
113 | return Math.floor(Utils.getRandomFloat(max)); |
114 | } | |
115 | ||
116 | static roundTo(number, scale) { | |
117 | return Utils.convertToFloat(number.toFixed(scale)); | |
118 | } | |
119 | ||
120 | static getRandomFloatRounded(max, min = 0, scale = 2) { | |
121 | if (min) { | |
122 | return Utils.roundTo(Utils.getRandomFloat(max, min), scale); | |
123 | } | |
124 | return Utils.roundTo(Utils.getRandomFloat(max), scale); | |
7dde0b73 JB |
125 | } |
126 | ||
ead548f2 | 127 | static logPrefix(prefixString = '') { |
7dde0b73 | 128 | const date = new Date(); |
4455e614 | 129 | return date.toLocaleString() + prefixString; |
7dde0b73 | 130 | } |
1d7ca20c JB |
131 | |
132 | static objectHasOwnProperty(object, property) { | |
133 | return Object.prototype.hasOwnProperty.call(object, property); | |
134 | } | |
2e6f5966 JB |
135 | |
136 | static cloneJSonDocument(jsonDocument) { | |
137 | return JSON.parse(JSON.stringify(jsonDocument)); | |
138 | } | |
facd8ebd | 139 | |
67e9cccf JB |
140 | static isIterable(obj) { |
141 | if (obj) { | |
142 | return typeof obj[Symbol.iterator] === 'function'; | |
143 | } | |
144 | return false; | |
145 | } | |
146 | ||
147 | static isEmptyJSon(document) { | |
148 | // Empty? | |
149 | if (!document) { | |
150 | return true; | |
151 | } | |
152 | // Check type | |
153 | if (typeof document !== 'object') { | |
154 | return true; | |
155 | } | |
156 | // Check | |
157 | return Object.keys(document).length === 0; | |
158 | } | |
159 | ||
560bcf5b JB |
160 | static isString(value) { |
161 | return typeof value === 'string'; | |
162 | } | |
163 | ||
facd8ebd | 164 | static isUndefined(value) { |
ead548f2 JB |
165 | return typeof value === 'undefined'; |
166 | } | |
167 | ||
168 | static isNullOrUndefined(value) { | |
169 | // eslint-disable-next-line eqeqeq | |
170 | if (value == null) { | |
facd8ebd JB |
171 | return true; |
172 | } | |
173 | return false; | |
174 | } | |
4a56deef JB |
175 | |
176 | static isEmptyArray(object) { | |
177 | if (Array.isArray(object) && object.length > 0) { | |
178 | return false; | |
179 | } | |
180 | return true; | |
181 | } | |
7dde0b73 JB |
182 | } |
183 | ||
184 | module.exports = Utils; |