1 const {v4
: uuid
} = require('uuid');
4 static generateUUID() {
9 return new Promise((resolve
) => setTimeout(resolve
, ms
));
12 static secondstoHHMMSS(seconds
) {
13 const date
= new Date(null);
14 date
.setSeconds(seconds
);
15 return date
.toISOString().substr(11, 8);
18 static removeExtraEmptyLines(tab
) {
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
27 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
28 // Remove the first one
34 static convertToDate(date
) {
40 if (!(date
instanceof Date
)) {
41 return new Date(date
);
46 static convertToObjectID(id
) {
49 if (typeof id
=== 'string') {
51 // eslint-disable-next-line no-undef
52 changedID
= new ObjectID(id
);
57 static convertToInt(value
) {
58 let changedValue
= value
;
62 if (Number
.isSafeInteger(value
)) {
66 if (typeof value
=== 'string') {
68 changedValue
= parseInt(value
);
73 static convertToFloat(value
) {
74 let changedValue
= value
;
79 if (typeof value
=== 'string') {
81 changedValue
= parseFloat(value
);
86 static convertToBoolean(value
) {
91 if (typeof value
=== 'boolean') {
96 result
= (value
=== 'true');
102 static getRandomFloat(max
, min
= 0) {
104 return Math
.random() * (max
- min
+ 1) + min
;
106 return Math
.random() * max
+ 1;
109 static getRandomInt(max
, min
= 0) {
111 return Math
.floor(Utils
.getRandomFloat(max
, min
));
113 return Math
.floor(Utils
.getRandomFloat(max
));
116 static roundTo(number
, scale
) {
117 return Utils
.convertToFloat(number
.toFixed(scale
));
120 static getRandomFloatRounded(max
, min
= 0, scale
= 2) {
122 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
124 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
127 static logPrefix(prefixString
= '') {
128 const date
= new Date();
129 return date
.toLocaleString() + prefixString
;
132 static objectHasOwnProperty(object
, property
) {
133 return Object
.prototype.hasOwnProperty
.call(object
, property
);
136 static cloneJSonDocument(jsonDocument
) {
137 return JSON
.parse(JSON
.stringify(jsonDocument
));
140 static isIterable(obj
) {
142 return typeof obj
[Symbol
.iterator
] === 'function';
147 static isEmptyJSon(document
) {
153 if (typeof document
!== 'object') {
157 return Object
.keys(document
).length
=== 0;
160 static isString(value
) {
161 return typeof value
=== 'string';
164 static isUndefined(value
) {
165 return typeof value
=== 'undefined';
168 static isNullOrUndefined(value
) {
169 // eslint-disable-next-line eqeqeq
176 static isEmptyArray(object
) {
177 if (Array
.isArray(object
) && object
.length
> 0) {
184 module
.exports
= Utils
;