Move eta function to shared format utility

This commit is contained in:
John Furrow
2016-12-08 22:54:23 -08:00
parent 9ef19e7947
commit 6a8bb2a4b7
2 changed files with 30 additions and 24 deletions
+28 -2
View File
@@ -1,7 +1,33 @@
'use strict';
let moment = require('moment');
const FORMAT_UTIL = {
const formatUtil = {
secondsToDuration: cumSeconds => {
const years = Math.floor(cumSeconds / 31536000);
const weeks = Math.floor((cumSeconds % 31536000) / 604800);
const days = Math.floor(((cumSeconds % 31536000) % 604800) / 86400);
const hours = Math.floor((((cumSeconds % 31536000) % 604800) % 86400) / 3600);
const minutes = Math.floor(((((cumSeconds % 31536000) % 604800) % 86400) % 3600) / 60);
const seconds = Math.floor(cumSeconds - (minutes * 60));
let timeRemaining = null;
if (years > 0) {
timeRemaining = {years, weeks, cumSeconds};
} else if (weeks > 0) {
timeRemaining = {weeks, days, cumSeconds};
} else if (days > 0) {
timeRemaining = {days, hours, cumSeconds};
} else if (hours > 0) {
timeRemaining = {hours, minutes, cumSeconds};
} else if (minutes > 0) {
timeRemaining = {minutes, seconds, cumSeconds};
} else {
timeRemaining = {seconds, cumSeconds};
}
return timeRemaining;
},
minToHumanReadable: min => {
return moment.duration(min * 60 * 1000).humanize();
},
@@ -45,4 +71,4 @@ const FORMAT_UTIL = {
}
}
module.exports = FORMAT_UTIL;
module.exports = formatUtil;