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 isIterable(obj) { |
19 | if (obj) { | |
20 | return typeof obj[Symbol.iterator] === 'function'; | |
21 | } | |
22 | return false; | |
23 | } | |
24 | ||
25 | static isEmptyJSon(document) { | |
26 | // Empty? | |
27 | if (!document) { | |
28 | return true; | |
29 | } | |
30 | // Check type | |
31 | if (typeof document !== 'object') { | |
32 | return true; | |
33 | } | |
34 | // Check | |
35 | return Object.keys(document).length === 0; | |
36 | } | |
37 | ||
38 | static removeExtraEmptyLines(tab) { | |
39 | // Start from the end | |
40 | for (let i = tab.length - 1; i > 0; i--) { | |
41 | // Two consecutive empty lines? | |
42 | if (tab[i].length === 0 && tab[i - 1].length === 0) { | |
43 | // Remove the last one | |
44 | tab.splice(i, 1); | |
45 | } | |
46 | // Check last line | |
47 | if (i === 1 && tab[i - 1].length === 0) { | |
48 | // Remove the first one | |
49 | tab.splice(i - 1, 1); | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
560bcf5b JB |
54 | static convertToDate(date) { |
55 | // Check | |
56 | if (!date) { | |
57 | return date; | |
58 | } | |
59 | // Check Type | |
60 | if (!(date instanceof Date)) { | |
61 | return new Date(date); | |
62 | } | |
63 | return date; | |
64 | } | |
65 | ||
7dde0b73 JB |
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 | ||
72766a82 JB |
77 | static convertToInt(value) { |
78 | let changedValue = value; | |
79 | if (!value) { | |
7dde0b73 JB |
80 | return 0; |
81 | } | |
72766a82 JB |
82 | if (Number.isSafeInteger(value)) { |
83 | return value; | |
84 | } | |
7dde0b73 | 85 | // Check |
72766a82 | 86 | if (typeof value === 'string') { |
7dde0b73 | 87 | // Create Object |
72766a82 | 88 | changedValue = parseInt(value); |
7dde0b73 | 89 | } |
72766a82 | 90 | return changedValue; |
7dde0b73 JB |
91 | } |
92 | ||
72766a82 JB |
93 | static convertToFloat(value) { |
94 | let changedValue = value; | |
95 | if (!value) { | |
7dde0b73 JB |
96 | return 0; |
97 | } | |
98 | // Check | |
72766a82 | 99 | if (typeof value === 'string') { |
7dde0b73 | 100 | // Create Object |
72766a82 | 101 | changedValue = parseFloat(value); |
7dde0b73 | 102 | } |
72766a82 | 103 | return changedValue; |
7dde0b73 JB |
104 | } |
105 | ||
a6e68f34 JB |
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 | ||
560bcf5b JB |
122 | static getRandomFloat(max, min = 0) { |
123 | if (min) { | |
124 | return Math.random() * (max - min + 1) + min; | |
125 | } | |
126 | return Math.random() * max + 1; | |
127 | } | |
128 | ||
129 | static getRandomInt(max, min = 0) { | |
7dde0b73 | 130 | if (min) { |
560bcf5b | 131 | return Math.floor(Utils.getRandomFloat(max, min)); |
7dde0b73 | 132 | } |
560bcf5b JB |
133 | return Math.floor(Utils.getRandomFloat(max)); |
134 | } | |
135 | ||
136 | static roundTo(number, scale) { | |
137 | return Utils.convertToFloat(number.toFixed(scale)); | |
138 | } | |
139 | ||
140 | static getRandomFloatRounded(max, min = 0, scale = 2) { | |
141 | if (min) { | |
142 | return Utils.roundTo(Utils.getRandomFloat(max, min), scale); | |
143 | } | |
144 | return Utils.roundTo(Utils.getRandomFloat(max), scale); | |
7dde0b73 JB |
145 | } |
146 | ||
ead548f2 | 147 | static logPrefix(prefixString = '') { |
7dde0b73 | 148 | const date = new Date(); |
4455e614 | 149 | return date.toLocaleString() + prefixString; |
7dde0b73 | 150 | } |
1d7ca20c JB |
151 | |
152 | static objectHasOwnProperty(object, property) { | |
153 | return Object.prototype.hasOwnProperty.call(object, property); | |
154 | } | |
2e6f5966 JB |
155 | |
156 | static cloneJSonDocument(jsonDocument) { | |
157 | return JSON.parse(JSON.stringify(jsonDocument)); | |
158 | } | |
facd8ebd | 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; |