2017-08-03 16:31:53 -04:00
|
|
|
import _ from 'underscore';
|
|
|
|
|
2016-11-18 10:49:40 -05:00
|
|
|
(() => {
|
|
|
|
/*
|
2017-08-31 15:07:57 -04:00
|
|
|
* TODO: Make these methods more configurable (e.g. stringifyTime condensed or
|
|
|
|
* non-condensed, abbreviateTimelengths)
|
2016-11-18 10:49:40 -05:00
|
|
|
* */
|
|
|
|
|
2016-12-23 02:07:34 -05:00
|
|
|
const utils = window.gl.utils = gl.utils || {};
|
|
|
|
const prettyTime = utils.prettyTime = {
|
2016-11-18 10:49:40 -05:00
|
|
|
/*
|
|
|
|
* Accepts seconds and returns a timeObject { weeks: #, days: #, hours: #, minutes: # }
|
2017-08-31 15:07:57 -04:00
|
|
|
* Seconds can be negative or positive, zero or non-zero. Can be configured for any day
|
|
|
|
* or week length.
|
2016-11-18 10:49:40 -05:00
|
|
|
*/
|
2017-08-31 15:07:57 -04:00
|
|
|
parseSeconds(seconds, { daysPerWeek = 5, hoursPerDay = 8 } = {}) {
|
|
|
|
const DAYS_PER_WEEK = daysPerWeek;
|
|
|
|
const HOURS_PER_DAY = hoursPerDay;
|
2016-11-18 10:49:40 -05:00
|
|
|
const MINUTES_PER_HOUR = 60;
|
|
|
|
const MINUTES_PER_WEEK = DAYS_PER_WEEK * HOURS_PER_DAY * MINUTES_PER_HOUR;
|
|
|
|
const MINUTES_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR;
|
|
|
|
|
|
|
|
const timePeriodConstraints = {
|
|
|
|
weeks: MINUTES_PER_WEEK,
|
|
|
|
days: MINUTES_PER_DAY,
|
|
|
|
hours: MINUTES_PER_HOUR,
|
|
|
|
minutes: 1,
|
|
|
|
};
|
|
|
|
|
2016-12-23 02:07:34 -05:00
|
|
|
let unorderedMinutes = prettyTime.secondsToMinutes(seconds);
|
2016-11-18 10:49:40 -05:00
|
|
|
|
|
|
|
return _.mapObject(timePeriodConstraints, (minutesPerPeriod) => {
|
|
|
|
const periodCount = Math.floor(unorderedMinutes / minutesPerPeriod);
|
|
|
|
|
|
|
|
unorderedMinutes -= (periodCount * minutesPerPeriod);
|
|
|
|
|
|
|
|
return periodCount;
|
|
|
|
});
|
2016-12-23 02:07:34 -05:00
|
|
|
},
|
2016-11-18 10:49:40 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accepts a timeObject and returns a condensed string representation of it
|
|
|
|
* (e.g. '1w 2d 3h 1m' or '1h 30m'). Zero value units are not included.
|
|
|
|
*/
|
|
|
|
|
2016-12-23 02:07:34 -05:00
|
|
|
stringifyTime(timeObject) {
|
2016-11-18 10:49:40 -05:00
|
|
|
const reducedTime = _.reduce(timeObject, (memo, unitValue, unitName) => {
|
|
|
|
const isNonZero = !!unitValue;
|
|
|
|
return isNonZero ? `${memo} ${unitValue}${unitName.charAt(0)}` : memo;
|
|
|
|
}, '').trim();
|
|
|
|
return reducedTime.length ? reducedTime : '0m';
|
2016-12-23 02:07:34 -05:00
|
|
|
},
|
2016-11-18 10:49:40 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accepts a time string of any size (e.g. '1w 2d 3h 5m' or '1w 2d') and returns
|
|
|
|
* the first non-zero unit/value pair.
|
|
|
|
*/
|
|
|
|
|
2016-12-23 02:07:34 -05:00
|
|
|
abbreviateTime(timeStr) {
|
2016-11-18 10:49:40 -05:00
|
|
|
return timeStr.split(' ')
|
|
|
|
.filter(unitStr => unitStr.charAt(0) !== '0')[0];
|
2016-12-23 02:07:34 -05:00
|
|
|
},
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2016-12-23 02:07:34 -05:00
|
|
|
secondsToMinutes(seconds) {
|
2016-11-18 10:49:40 -05:00
|
|
|
return Math.abs(seconds / 60);
|
2016-12-23 02:07:34 -05:00
|
|
|
},
|
|
|
|
};
|
2016-11-18 10:49:40 -05:00
|
|
|
})(window.gl || (window.gl = {}));
|