2024-10-31 05:58:21 +00:00
|
|
|
const log_level = process.env.LOG_LEVEL || 'info';
|
|
|
|
|
|
|
|
const levels = ['none', 'error', 'warn', 'info', 'debug'];
|
|
|
|
const currentLevelIndex = levels.indexOf(log_level);
|
|
|
|
|
|
|
|
const buildLogMethod = (level) => (...args) => {
|
|
|
|
const levelIndex = levels.indexOf(level);
|
|
|
|
const shouldLog = levelIndex <= currentLevelIndex;
|
|
|
|
|
|
|
|
if (shouldLog) {
|
|
|
|
console[level](...args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-20 00:29:58 +00:00
|
|
|
module.exports = {
|
|
|
|
randomArray: (arr) => {
|
|
|
|
return arr[Math.floor(Math.random() * arr.length)]
|
|
|
|
},
|
2024-10-30 07:46:17 +00:00
|
|
|
toFixed: (num, digits = 2) => {
|
|
|
|
return parseFloat(Number(num).toFixed(digits))
|
2024-10-31 05:58:21 +00:00
|
|
|
},
|
|
|
|
logger: {
|
|
|
|
debug: buildLogMethod('debug'),
|
|
|
|
info: buildLogMethod('info'),
|
|
|
|
warn: buildLogMethod('warn'),
|
|
|
|
error: buildLogMethod('error')
|
2024-10-30 07:46:17 +00:00
|
|
|
}
|
2024-10-20 00:29:58 +00:00
|
|
|
}
|