1 import {v4 as uuid
} from 'uuid';
3 export default class Utils
{
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) {
103 return Math
.random() < 0.5 ? (1 - Math
.random()) * (max
- min
) + min
: Math
.random() * (max
- min
) + min
;
106 static getRandomInt(max
, min
= 0) {
108 return Math
.floor(Math
.random() * (max
- min
+ 1) + min
);
110 return Math
.floor(Math
.random() * max
+ 1);
113 static roundTo(number
, scale
) {
114 return Utils
.convertToFloat(number
.toFixed(scale
));
117 static getRandomFloatRounded(max
, min
= 0, scale
= 2) {
119 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
121 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
124 static logPrefix(prefixString
= '') {
125 const date
= new Date();
126 return date
.toLocaleString() + prefixString
;
129 static objectHasOwnProperty(object
, property
) {
130 return Object
.prototype.hasOwnProperty
.call(object
, property
);
133 static cloneObject(object
) {
134 return JSON
.parse(JSON
.stringify(object
));
137 static isIterable(obj
) {
139 return typeof obj
[Symbol
.iterator
] === 'function';
144 static isEmptyJSon(document
) {
150 if (typeof document
!== 'object') {
154 return Object
.keys(document
).length
=== 0;
157 static isString(value
) {
158 return typeof value
=== 'string';
161 static isUndefined(value
) {
162 return typeof value
=== 'undefined';
165 static isNullOrUndefined(value
) {
166 // eslint-disable-next-line eqeqeq
173 static isEmptyArray(object
) {
174 if (Array
.isArray(object
) && object
.length
> 0) {
180 static isEmptyObject(obj
) {
181 return !Object
.keys(obj
).length
;