2020-09-10 12:03:56 +00:00
|
|
|
/**
|
|
|
|
* Get the time between start and now in milliseconds
|
|
|
|
*/
|
2020-09-09 12:53:05 +00:00
|
|
|
function calculateElapsedTime(start) {
|
2020-09-09 12:25:00 +00:00
|
|
|
const diff = process.hrtime(start);
|
|
|
|
|
|
|
|
return Math.round((diff[0] * 1e9 + diff[1]) / 1e6); // msec
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:03:56 +00:00
|
|
|
/**
|
|
|
|
* Get the ISO string with yesterday's date set (- 24 hours)
|
|
|
|
*/
|
|
|
|
function getYesterdayISOString() {
|
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
date.setDate(date.getDate() - 1);
|
|
|
|
|
|
|
|
return date.toISOString();
|
|
|
|
}
|
|
|
|
|
2020-09-30 14:20:55 +00:00
|
|
|
/**
|
|
|
|
* Get response from response object if available
|
|
|
|
*/
|
|
|
|
function getResponseContent(response) {
|
|
|
|
try {
|
2021-04-27 16:30:51 +00:00
|
|
|
return JSON.parse(response?.body || response?.text);
|
2020-09-30 14:20:55 +00:00
|
|
|
} catch {
|
2021-04-27 16:30:51 +00:00
|
|
|
return response?.body || response?.text;
|
2020-09-30 14:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 14:04:13 +00:00
|
|
|
/**
|
|
|
|
* Ensures that the object serializes to JSON properly
|
|
|
|
*/
|
|
|
|
function ensureValidJSON(object) {
|
|
|
|
const replacer = (key, value) => (value === undefined ? "--undefined--" : value);
|
|
|
|
const stringified = JSON.stringify(object, replacer);
|
|
|
|
|
|
|
|
return JSON.parse(stringified);
|
|
|
|
}
|
|
|
|
|
2020-09-30 14:20:55 +00:00
|
|
|
module.exports = {
|
|
|
|
calculateElapsedTime,
|
|
|
|
getYesterdayISOString,
|
|
|
|
getResponseContent,
|
2020-10-10 14:04:13 +00:00
|
|
|
ensureValidJSON,
|
2020-09-30 14:20:55 +00:00
|
|
|
};
|