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 isIterable(obj
) {
20 return typeof obj
[Symbol
.iterator
] === 'function';
25 static isEmptyJSon(document
) {
31 if (typeof document
!== 'object') {
35 return Object
.keys(document
).length
=== 0;
38 static removeExtraEmptyLines(tab
) {
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
47 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
48 // Remove the first one
54 static convertToDate(date
) {
60 if (!(date
instanceof Date
)) {
61 return new Date(date
);
66 static convertToObjectID(id
) {
69 if (typeof id
=== 'string') {
71 // eslint-disable-next-line no-undef
72 changedID
= new ObjectID(id
);
77 static convertToInt(value
) {
78 let changedValue
= value
;
82 if (Number
.isSafeInteger(value
)) {
86 if (typeof value
=== 'string') {
88 changedValue
= parseInt(value
);
93 static convertToFloat(value
) {
94 let changedValue
= value
;
99 if (typeof value
=== 'string') {
101 changedValue
= parseFloat(value
);
106 static convertToBoolean(value
) {
111 if (typeof value
=== 'boolean') {
116 result
= (value
=== 'true');
122 static getRandomFloat(max
, min
= 0) {
124 return Math
.random() * (max
- min
+ 1) + min
;
126 return Math
.random() * max
+ 1;
129 static getRandomInt(max
, min
= 0) {
131 return Math
.floor(Utils
.getRandomFloat(max
, min
));
133 return Math
.floor(Utils
.getRandomFloat(max
));
136 static roundTo(number
, scale
) {
137 return Utils
.convertToFloat(number
.toFixed(scale
));
140 static getRandomFloatRounded(max
, min
= 0, scale
= 2) {
142 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
144 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
147 static logPrefix(prefixString
= '') {
148 const date
= new Date();
149 return date
.toLocaleString() + prefixString
;
152 static objectHasOwnProperty(object
, property
) {
153 return Object
.prototype.hasOwnProperty
.call(object
, property
);
156 static cloneJSonDocument(jsonDocument
) {
157 return JSON
.parse(JSON
.stringify(jsonDocument
));
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
;